k1r1t0
k1r1t0

Reputation: 767

Use multiple files for logs using glog

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?

Upvotes: 1

Views: 442

Answers (1)

Zaid Afzal
Zaid Afzal

Reputation: 356

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
})

Upvotes: 0

Related Questions