Skip to content

Commit

Permalink
fix: fixing logger issue on rotating log file (#859)
Browse files Browse the repository at this point in the history
  • Loading branch information
b00f authored and themantre committed Dec 13, 2023
1 parent ea44581 commit 5b2eb31
Showing 1 changed file with 24 additions and 25 deletions.
49 changes: 24 additions & 25 deletions util/logger/logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ var globalInst *logger
type logger struct {
config *Config
subs map[string]*SubLogger
writer io.Writer
}

type SubLogger struct {
Expand All @@ -38,7 +39,6 @@ type SubLogger struct {
func getLoggersInst() *logger {
if globalInst == nil {
// Only during tests the globalInst is nil

LogFilename = util.TempFilePath()

conf := &Config{
Expand All @@ -56,19 +56,40 @@ func getLoggersInst() *logger {
globalInst = &logger{
config: conf,
subs: make(map[string]*SubLogger),
writer: zerolog.ConsoleWriter{Out: os.Stderr, TimeFormat: "15:04:05"},
}
log.Logger = zerolog.New(globalInst.writer).With().Timestamp().Logger()
}

return globalInst
}

func InitGlobalLogger(conf *Config) {
if globalInst == nil {
writers := []io.Writer{}
// console writer
if conf.Colorful {
writers = append(writers, zerolog.ConsoleWriter{Out: os.Stderr, TimeFormat: "15:04:05"})
} else {
writers = append(writers, os.Stderr)
}

// file writer
fw := &lumberjack.Logger{
Filename: LogFilename,
MaxSize: MaxLogSize,
MaxBackups: conf.MaxBackups,
Compress: conf.Compress,
MaxAge: conf.RotateLogAfterDays,
}
writers = append(writers, fw)

globalInst = &logger{
config: conf,
subs: make(map[string]*SubLogger),
writer: io.MultiWriter(writers...),
}
log.Logger = zerolog.New(globalInst.writers()).With().Timestamp().Logger()
log.Logger = zerolog.New(globalInst.writer).With().Timestamp().Logger()

lvl, err := zerolog.ParseLevel(conf.Levels["default"])
if err != nil {
Expand Down Expand Up @@ -116,7 +137,7 @@ func addFields(event *zerolog.Event, keyvals ...interface{}) *zerolog.Event {
func NewSubLogger(name string, obj fmt.Stringer) *SubLogger {
inst := getLoggersInst()
sl := &SubLogger{
logger: zerolog.New(inst.writers()).With().Timestamp().Logger(),
logger: zerolog.New(inst.writer).With().Timestamp().Logger(),
name: name,
obj: obj,
}
Expand All @@ -136,28 +157,6 @@ func NewSubLogger(name string, obj fmt.Stringer) *SubLogger {
return sl
}

func (l *logger) writers() io.Writer {
writers := []io.Writer{}
// console writer
if l.config.Colorful {
writers = append(writers, zerolog.ConsoleWriter{Out: os.Stderr, TimeFormat: "15:04:05"})
} else {
writers = append(writers, os.Stderr)
}

// file writer
fw := &lumberjack.Logger{
Filename: LogFilename,
MaxSize: MaxLogSize,
MaxBackups: l.config.MaxBackups,
Compress: l.config.Compress,
MaxAge: l.config.RotateLogAfterDays,
}
writers = append(writers, fw)

return io.MultiWriter(writers...)
}

func (sl *SubLogger) logObj(event *zerolog.Event, msg string, keyvals ...interface{}) {
if sl.obj != nil {
addFields(event.Str(sl.name, sl.obj.String()), keyvals...).Msg(msg)
Expand Down

0 comments on commit 5b2eb31

Please sign in to comment.