Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Go logging to multiple output

Tags:

logging

go

Is there a way in go language to log to multiple output in different levels?.

I want to have a program that logs to stdout in Info level and to a file in debug level with timestamp at the same time.

Like every time I code:

log.Debug("Entering some func")
res := func()
log.Infof("Result was: %s", res)

I can see the console prints:

Result was: Successful

And a file with:

2015-03-26T01:27:38-04:00 [DEBU]: Entering some func
2015-03-26T01:27:38-04:00 [INFO]: Result was: Successful

I use logrus and glog, but can't find this functionallity. Is there another package or something I can code?

like image 479
gbriones.gdl Avatar asked Feb 07 '23 15:02

gbriones.gdl


2 Answers

Go-logging supports different logging backends like file, syslog etc. Multiple backends can be set with different log levels per backend and logger. Example over here.

Lumberjack can also be used with this for writing logs to rolling files. Here is an example.

like image 184
Apoorva Manjunath Avatar answered Feb 11 '23 13:02

Apoorva Manjunath


Found this while looking for a way to log to multiple destinations. If that is all you need and aren't worried about the log level the simplest way is to use an io.Multiwriter:

multi := io.MultiWriter(file, os.Stdout)
log.SetOutput(multi)
like image 28
jerboa-io Avatar answered Feb 11 '23 13:02

jerboa-io