natefinch / lumberjack

lumberjack is a log rolling package for Go
MIT License
4.79k stars 591 forks source link

Print Filename and number #110

Closed knarukulla closed 2 years ago

knarukulla commented 3 years ago

Currently logger is not outputing filename and number. It looks like it is limitation.

log.SetOutput(&lumberjack.Logger{ Filename: "/tmp/sample.log", MaxSize: 500, // megabytes MaxBackups: 3, MaxAge: 28, // days Compress: true, // disabled by default })

output: 2020/09/23 13:35:12 main started 2020/09/23 13:35:12 webserver2 started

natefinch commented 3 years ago

The lumberjack logger doesn't control what data it writes out. That's up to the thing writing to it. Think of lumberjack as just a wrapper for a file. In this case, it's the log.Logger that would need to output file and line numbers.

vcheruk2 commented 3 years ago

Try this out and see if it works.

Outside of main, declare your log variable.

var errLog *log.Logger

Inside main:

e, err := os.OpenFile("./foo.log", os.O_WRONLY|os.O_CREATE|os.O_APPEND, 0666)

if err != nil {
    fmt.Printf("error opening file: %v", err)
    os.Exit(1)
}
errLog = log.New(e, "", log.Ldate|log.Ltime)
errLog.SetOutput(&lumberjack.Logger{
    Filename:   "./foo.log",
    MaxSize:    1,  // megabytes after which new file is created
    MaxBackups: 3,  // number of backups
    MaxAge:     28, //days
})

if you do errLog.Print("Print something.")