Use multiple files for logs using glog

388 Views Asked by At

I'd like to use different directories for different connections. Let's say I have connections that are being handled in separate threads/goroutines. I want them to write to different log directories. I can specify -log_dir, but it will write to only one directory, so it's really hard to understand which log file is for.

Is there a way to do something like that using glog or another package?

1

There are 1 best solutions below

0
Zaid Afzal On

As per my knowledge, you may have to create separate logger instances (for each goroutine) to write logs to different directories within different goroutines. Lumberjack would help in this scenario. Its a log rolling package for Go.

// Do this for each goroutine
logger := glog.New()
// redirect logs to a different directory
logger.SetOutput(&lumberjack.Logger{
    Filename:   "/var/log/app/goroutine1.log",
    MaxSize:    1,  // megabytes
    MaxBackups: 3,
    MaxAge:     28, // days
})