natefinch / lumberjack

lumberjack is a log rolling package for Go
MIT License
4.76k stars 585 forks source link

time is not updated when using log.SetPrefix #145

Closed engineer-pjin closed 2 years ago

engineer-pjin commented 2 years ago

Hello. When I tried to change the time format with log.SetPrefix , I noticed that the time was not updated. The test code is below.

import (
    "log"
    "time"

    lumberjack "gopkg.in/natefinch/lumberjack.v2"
)

func main() {
    logRotate := &lumberjack.Logger{
        Filename:   "test_file.log",
        MaxSize:    1,
        MaxBackups: 3,
        MaxAge:     10,
        Compress:   true,
    }
    log.SetOutput(logRotate)

    log.SetFlags(log.Lshortfile)

    prefix := time.Now().Format("2006-01-02 15:04:05.000  ")
    log.SetPrefix(prefix)

    for i := 0; i < 5; i++ {
        log.Println("log test")
        time.Sleep(1 * time.Second)
    }

}

And the result is as below.

2021-11-24 17:24:45.216  logrotate.go:43: log test
2021-11-24 17:24:45.216  logrotate.go:43: log test
2021-11-24 17:24:45.216  logrotate.go:43: log test
2021-11-24 17:24:45.216  logrotate.go:43: log test
2021-11-24 17:24:45.216  logrotate.go:43: log test

And I confirmed that the log.SetPrefix setting works normally when using the default log package.

Aside from this issue, the package is very nice and contributions are always appreciated. Please help me to solve this problem.

engineer-pjin commented 2 years ago

I confirmed that the log.SetPrefix function itself works as described above. This inquiry is incorrect and I am looking for another way.