记录到多个输出 [英] Go logging to multiple output

查看:153
本文介绍了记录到多个输出的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我想要一个程序在Info级别中记录到标准输出,并且一个文件在调试级别与时间戳在同一时间。

像每次我代码:

  log.Debug(输入一些func)
res:= func()
log.Infof(Result was:%s,res)

我可以看到控制台打印:

 结果是:成功

以及一个文件:

  2015-03-26T01:27:38-04:00 [DEBU]:输入一些func 
2015-03-26T01:27:38 -04:00 [INFO]:结果是:成功

我使用了logrus和glog,找到这个功能。是否有另一个软件包或我可以编码的东西?

Go-logging 支持不同的日志后端,如文件,系统日志等。可以为每个后端和日志记录设置不同的日志级别。
此处的示例。



伐木工人也可以用于将日志写入滚动文件。以下是示例


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?

解决方案

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.

这篇关于记录到多个输出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆