網站首頁 編程語言 正文
安裝簡介
Logrus是Go的結構化日志記錄器,與標準的日志記錄器庫完全API兼容。
go get安裝的logrus庫
go get github.com/sirupsen/logrus
快速使用
package main import ( log "github.com/sirupsen/logrus" ) func main() { log.SetLevel(log.TraceLevel) log.Trace("trace") log.Debug("debug") log.Info("info") log.Warn("warn") log.Error("error") log.Fatal("fatal") log.Panic("panic") }
輸出:
TRAC[0000] trace ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?
DEBU[0000] debug ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?
INFO[0000] info ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ??
WARN[0000] warn ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ??
ERRO[0000] error ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?
FATA[0000] fatal ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?
exit status 1
可以看到panic沒有輸出,因為fatal會導致goroutine直接退出。
支持的日志級別
logrus支持多種日志級別,下面從小到大:
- Panic:記錄日志,然后panic
- Fatal:致命錯誤,輸出日志后,程序退出
- Error:錯誤
- Warn:警告
- Info:關鍵信息
- Debug:調試信息
- Trace:很細粒度的信息,一般用不到。
可以看到Trace級別最大,Panic最小。默認的級別為Info,高于這個級別的日志不會輸出。
日期
可以看到上面的日志沒有時間,可以指定日志格式:
package main import ( log "github.com/sirupsen/logrus" ) func main() { log.SetLevel(log.TraceLevel) log.SetFormatter(&log.TextFormatter{ FullTimestamp: true, TimestampFormat: "2022-07-17 00:00:00.000", }) log.Trace("trace") log.Debug("debug") log.Info("info") log.Warn("warn") log.Error("error") log.Fatal("fatal") log.Panic("panic") }
精確到毫秒。
輸出:
TRAC[171717+08-77 00:00:00.628] trace ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?
DEBU[171717+08-77 00:00:00.629] debug ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?
INFO[171717+08-77 00:00:00.629] info ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ??
WARN[171717+08-77 00:00:00.629] warn ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ??
ERRO[171717+08-77 00:00:00.629] error ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?
FATA[171717+08-77 00:00:00.629] fatal ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?
exit status 1
打印調用位置
在進行定位的時候需要知道是那行代碼調用的log.SetReportCaller(true)
:
package main import ( log "github.com/sirupsen/logrus" ) func main() { log.SetLevel(log.TraceLevel) log.SetReportCaller(true) log.SetFormatter(&log.TextFormatter{ FullTimestamp: true, TimestampFormat: "2022-07-17 00:00:00.000", }) log.Trace("trace") log.Debug("debug") log.Info("info") log.Warn("warn") log.Error("error") log.Fatal("fatal") log.Panic("panic") }
輸出:
TRAC[171717+08-77 00:00:00.019]/Users/wanghaifeng/GolandProjects/StudyProject/src/log/my_log.go:14 main.main() trace ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?
DEBU[171717+08-77 00:00:00.019]/Users/wanghaifeng/GolandProjects/StudyProject/src/log/my_log.go:15 main.main() debug ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?
INFO[171717+08-77 00:00:00.019]/Users/wanghaifeng/GolandProjects/StudyProject/src/log/my_log.go:16 main.main() info ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ??
WARN[171717+08-77 00:00:00.019]/Users/wanghaifeng/GolandProjects/StudyProject/src/log/my_log.go:17 main.main() warn ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ??
ERRO[171717+08-77 00:00:00.019]/Users/wanghaifeng/GolandProjects/StudyProject/src/log/my_log.go:18 main.main() error ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?
FATA[171717+08-77 00:00:00.019]/Users/wanghaifeng/GolandProjects/StudyProject/src/log/my_log.go:19 main.main() fatal ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?
exit status 1
添加字段
定位問題需要知道具體是那個數據調用的,可以借助于log.WithField
和log.WithFields
,log.WithField
內部調用的也是log.WithFields
,參數底層使用map[string]interface{}數據結構保存:
package main import ( log "github.com/sirupsen/logrus" "os" ) func main() { log.SetLevel(log.TraceLevel) log.SetReportCaller(true) log.SetFormatter(&log.TextFormatter{ FullTimestamp: true, TimestampFormat: "2022-07-17 00:00:00.000", }) id := os.Args[1] mylog := log.WithField("id", id) mylog.Trace("trace") mylog.Debug("debug") mylog.Info("info") mylog.Warn("warn") mylog.Error("error") mylog.Fatal("fatal") mylog.Panic("panic") }
輸出:
? ?StudyProject go run ?src/log/my_log.go 123
TRAC[171717+08-77 00:00:00.665]/Users/wanghaifeng/GolandProjects/StudyProject/src/log/my_log.go:22 main.main() trace ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? id=123
DEBU[171717+08-77 00:00:00.665]/Users/wanghaifeng/GolandProjects/StudyProject/src/log/my_log.go:23 main.main() debug ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? id=123
INFO[171717+08-77 00:00:00.665]/Users/wanghaifeng/GolandProjects/StudyProject/src/log/my_log.go:24 main.main() info ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?id=123
WARN[171717+08-77 00:00:00.665]/Users/wanghaifeng/GolandProjects/StudyProject/src/log/my_log.go:25 main.main() warn ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?id=123
ERRO[171717+08-77 00:00:00.665]/Users/wanghaifeng/GolandProjects/StudyProject/src/log/my_log.go:26 main.main() error ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? id=123
FATA[171717+08-77 00:00:00.665]/Users/wanghaifeng/GolandProjects/StudyProject/src/log/my_log.go:27 main.main() fatal ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? id=123
exit status 1
給字段值加引號
配置ForceQuote
為true
package main import ( log "github.com/sirupsen/logrus" "os" ) func main() { log.SetLevel(log.TraceLevel) log.SetReportCaller(true) log.SetFormatter(&log.TextFormatter{ FullTimestamp: true, TimestampFormat: "2022-07-17 00:00:00.000", ForceQuote: true, }) id := os.Args[1] mylog := log.WithField("id", id) mylog.Trace("trace") mylog.Debug("debug") mylog.Info("info") mylog.Warn("warn") mylog.Error("error") mylog.Fatal("fatal") mylog.Panic("panic") }
輸出:
? ?StudyProject go run ?src/log/my_log.go 123
TRAC[171717+08-77 00:00:00.427]/Users/wanghaifeng/GolandProjects/StudyProject/src/log/my_log.go:22 main.main() trace ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? id="123"
DEBU[171717+08-77 00:00:00.427]/Users/wanghaifeng/GolandProjects/StudyProject/src/log/my_log.go:23 main.main() debug ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? id="123"
INFO[171717+08-77 00:00:00.427]/Users/wanghaifeng/GolandProjects/StudyProject/src/log/my_log.go:24 main.main() info ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?id="123"
WARN[171717+08-77 00:00:00.427]/Users/wanghaifeng/GolandProjects/StudyProject/src/log/my_log.go:25 main.main() warn ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?id="123"
ERRO[171717+08-77 00:00:00.427]/Users/wanghaifeng/GolandProjects/StudyProject/src/log/my_log.go:26 main.main() error ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? id="123"
FATA[171717+08-77 00:00:00.427]/Users/wanghaifeng/GolandProjects/StudyProject/src/log/my_log.go:27 main.main() fatal ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? id="123"
exit status 1
設置鉤子
每條日志在輸出前都會執行鉤子的特定方法,相當于全局攔截,可以方便做一些擴展,比如添加字段channel表明日志是那個應用輸出的、增加tranceid方便對問題的跟蹤、輸出日志文件等。
設置channel
package hooks import log "github.com/sirupsen/logrus" type ChannelHook struct { ChannelName string } func (h ChannelHook) Levels() []log.Level { return log.AllLevels } func (h ChannelHook) Fire(entry *log.Entry) error { entry.Data["channel"] = h.ChannelName return nil }
package main import ( logs "StudyProject/src/log/hooks" log "github.com/sirupsen/logrus" ) func main() { log.AddHook(logs.ChannelHook{ ChannelName: "web", }) log.Info("info") }
輸出:
INFO[0000] info channel=web
輸出日志
可以利用logrus的hook自己寫入文件,但是指定單個文件收集的時間范圍、保存的時間logrus并不支持,筆者目前的項目使用的是file-rotatelogs,但是作者已經不更新和維護,所以這里就不使用它來展示,有在考慮使用goframe的glog替換。
筆者這里就寫個簡單例子演示如何利用logrus的hook去寫文件并且把控制臺的打印干掉:
package hooks import ( log "github.com/sirupsen/logrus" "io/ioutil" ) // //FileHook // @Description: 文件hook // type FileHook struct { // // FileName // @Description: 文件名 // FileName string } func (h FileHook) Levels() []log.Level { return log.AllLevels } func (h FileHook) Fire(entry *log.Entry) error { fomat := &log.TextFormatter{ FullTimestamp: true, TimestampFormat: "2022-07-17 00:00:00.000", } // 從entry從獲得日志內容 msg, err := fomat.Format(entry) if err != nil { return err } err = ioutil.WriteFile(h.FileName, msg, 0644) if err != nil { return err } return nil }
package main import ( logs "StudyProject/src/log/hooks" log "github.com/sirupsen/logrus" "io/ioutil" ) func main() { log.AddHook(logs.FileHook{"log"}) // 關閉控制臺打印 log.SetOutput(ioutil.Discard) log.Info("test log write file") }
很顯然這只是個例子,不可直接應用于生產。
原文鏈接:https://juejin.cn/post/7126102527459721246
相關推薦
- 2022-04-16 C語言實現順序循環隊列實例_C 語言
- 2021-12-15 Linux下Makefile的編寫與使用詳解_Linux
- 2022-03-12 使用xshell連接linux服務器_Linux
- 2022-05-19 C#?漢字與拼音互轉的實現示例_C#教程
- 2022-04-10 微服務架構之服務注冊與發現實踐示例詳解_服務器其它
- 2022-12-25 Redis過期鍵與內存淘汰策略深入分析講解_Redis
- 2022-07-02 一個Python優雅的數據分塊方法詳解_python
- 2022-07-18 Nacos + OpenFeign 的使用方式
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支