Go实现整合Logrus实现日志打印
更新时间:2022年07月04日 10:04:57 作者: BarryYan
这篇文章主要介绍了Go实现整合Logrus实现日志打印,文章围绕主题展开详细的内容介绍,具有一定的参考价值,需要的小伙伴可以参考一下
Github:github.com/sirupsen/lo…
1 初步使用
package main import ( "context" "github.com/sirupsen/logrus" ) func main() { method0() } func method0() { logger:= logrus.New() logger.Warning("This is a first log.") ctx := context.WithValue(context.Background(),"key","value") logger.Warning(ctx,"This is a second log.") }
2 增加标签WithFields
package main import ( "context" "github.com/sirupsen/logrus" ) func main() { method1() } func method1() { log.WithFields(log.Fields{ "fieldKey": "fieldValue", }).Warning("This is a first field log.") log.WithFields(log.Fields{ "fieldKey": "fieldValue", "fieldKey2": "fieldValue2", }).Warning("This is a second field log.") }
3 配置常见参数
package main import ( "context" "github.com/sirupsen/logrus" log "github.com/sirupsen/logrus" "os" ) func main() { method2() } func init() { // 日志作为JSON而不是默认的ASCII格式器. log.SetFormatter(&log.JSONFormatter{}) // 输出到标准输出,可以是任何io.Writer log.SetOutput(os.Stdout) // 只记录xx级别或以上的日志 log.SetLevel(log.TraceLevel) } func method2() { log.WithFields(log.Fields{ "animal": "walrus", "size": 10, }).Info("A group of walrus emerges from the ocean") log.WithFields(log.Fields{ "omg": true, "number": 122, }).Warn("The group's number increased tremendously!") log.WithFields(log.Fields{ "omg": true, "number": 100, }).Fatal("The ice breaks!") }
Formatter一般分为两种:
- &log.JSONFormatter{}
- &log.TextFormatter{}
日志级别一共七种:
- log.Trace()
- log.Debug()
- log.Info()
- log.Warn()
- log.Error()
- log.Fatal()
- log.Panic()
4 输出日志到文件
package main import ( "context" "github.com/sirupsen/logrus" "os" ) func main() { method4() } func method4() { var log = logrus.New() file ,err := os.OpenFile("logrus.log", os.O_CREATE|os.O_WRONLY, 0666) if err == nil{ log.Out = file }else{ log.Info("Failed to log to file") } log.WithFields(logrus.Fields{ "filename": "123.txt", }).Info("This is a file log") }
logrus.log文件的内容:
time="2022-01-06T13:04:25+08:00" level=info msg="This is a file log" filename=123.txt\
5 利用Hooks将日志输出到其他地方
import ( log "github.com/sirupsen/logrus" "gopkg.in/gemnasium/logrus-airbrake-hook.v2" // the package is named "airbrake" logrus_syslog "github.com/sirupsen/logrus/hooks/syslog" "log/syslog" ) func init() { // 使用气闸挂钩来报告错误严重程度或以上的错误一个异常追踪。您可以创建自定义钩子,请参见钩子部分。 log.AddHook(airbrake.NewHook(123, "xyz", "production")) hook, err := logrus_syslog.NewSyslogHook("udp", "localhost:514", syslog.LOG_INFO, "") if err != nil { log.Error("Unable to connect to local syslog daemon") } else { log.AddHook(hook) } }
只需要在AddHook是添加相应的Hook就可以了
到此这篇关于Go实现整合Logrus实现日志打印的文章就介绍到这了,更多相关Go Logrus日志打印内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!
相关文章
go语言开发环境配置(sublime text3+gosublime)
网上google了下go的开发工具,大都推荐sublime text3+gosublime,本文就介绍了go语言开发环境配置(sublime text3+gosublime),具有一定的参考价值,感兴趣的可以了解一下2022-01-01详解go语言 make(chan int, 1) 和 make (chan int) 的区别
这篇文章主要介绍了go语言 make(chan int, 1) 和 make (chan int) 的区别,本文给大家介绍的非常详细,具有一定的参考借鉴价值,需要的朋友可以参考下2020-01-01GO Cobra Termui库开发终端命令行小工具轻松上手
这篇文章主要为大家介绍了GO语言开发终端命令行小工具,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪2024-01-01
最新评论