go第三方日志库uber-go/zap、lumberjack
生活随笔
收集整理的這篇文章主要介紹了
go第三方日志库uber-go/zap、lumberjack
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
uber-go/zap、lumberjack
zap是uber開源的go語言高性能日志庫,
lumberjack是zap官方推薦的日志分割庫,
結(jié)合這兩個庫我們可以在項目中實現(xiàn)完整的日志機(jī)制,
例如:輸出日志到文件、根據(jù)文件大小或日期分割等。
uber-go/zap
安裝
在項目目錄下命令行執(zhí)行
go get -u go.uber.org/zapLogger
通過zap.NewDevelopment()或zap.NewProduction()都可以創(chuàng)建一個Logger,
它們的主要區(qū)別在于NewDevelopment會輸出時間和包/文件/代碼行信息,
而NewProduction不會,并且NewProduction不記錄debug日志,
我們看一下例子:
zap.NewDevelopment()
package mainimport "go.uber.org/zap"func main() {log, _ := zap.NewDevelopment()log.Debug("這是debug日志")log.Info("***************************")log.Info("這是info日志")log.Info("***************************")log.Warn("這是warn日志")log.Info("***************************")log.Error("這是error日志")log.Info("***************************")log.Panic("這是panic日志") }輸出
GOROOT=D:\Program Files\Go #gosetup GOPATH=D:\work\goWorkspace;D:\work\goPath #gosetup "D:\Program Files\Go\bin\go.exe" build -o C:\Users\smart\AppData\Local\Temp\___go_build_main_go__1_.exe D:/work/goWorkspace/src/helloGoMod/log/main.go #gosetup C:\Users\smart\AppData\Local\Temp\___go_build_main_go__1_.exe #gosetup 2021-04-26T14:19:13.059+0800 DEBUG log/main.go:7 這是debug日志 2021-04-26T14:19:13.090+0800 INFO log/main.go:8 *************************** 2021-04-26T14:19:13.090+0800 INFO log/main.go:9 這是info日志 2021-04-26T14:19:13.090+0800 INFO log/main.go:10 *************************** 2021-04-26T14:19:13.090+0800 WARN log/main.go:11 這是warn日志 main.mainD:/work/goWorkspace/src/helloGoMod/log/main.go:11 runtime.mainD:/Program Files/Go/src/runtime/proc.go:225 2021-04-26T14:19:13.090+0800 INFO log/main.go:12 *************************** 2021-04-26T14:19:13.090+0800 ERROR log/main.go:13 這是error日志 main.mainD:/work/goWorkspace/src/helloGoMod/log/main.go:13 runtime.mainD:/Program Files/Go/src/runtime/proc.go:225 2021-04-26T14:19:13.090+0800 INFO log/main.go:14 *************************** 2021-04-26T14:19:13.090+0800 PANIC log/main.go:15 這是panic日志 main.mainD:/work/goWorkspace/src/helloGoMod/log/main.go:15 runtime.mainD:/Program Files/Go/src/runtime/proc.go:225 panic: 這是panic日志goroutine 1 [running]: go.uber.org/zap/zapcore.(*CheckedEntry).Write(0xc00013c0c0, 0x0, 0x0, 0x0)D:/work/goWorkspace/pkg/mod/go.uber.org/zap@v1.16.0/zapcore/entry.go:234 +0x58d go.uber.org/zap.(*Logger).Panic(0xc0000482a0, 0x5b9d71, 0x11, 0x0, 0x0, 0x0)D:/work/goWorkspace/pkg/mod/go.uber.org/zap@v1.16.0/logger.go:226 +0x86 main.main()D:/work/goWorkspace/src/helloGoMod/log/main.go:15 +0x219Process finished with exit code 2zap.NewProduction()
package mainimport "go.uber.org/zap"func main() {log, _ := zap.NewProduction()log.Debug("這是debug日志")log.Info("***************************")log.Info("這是info日志")log.Info("***************************")log.Warn("這是warn日志")log.Info("***************************")log.Error("這是error日志")log.Info("***************************")log.Panic("這是panic日志") }輸出
GOROOT=D:\Program Files\Go #gosetup GOPATH=D:\work\goWorkspace;D:\work\goPath #gosetup "D:\Program Files\Go\bin\go.exe" build -o C:\Users\smart\AppData\Local\Temp\___go_build_main_go__1_.exe D:/work/goWorkspace/src/helloGoMod/log/main.go #gosetup C:\Users\smart\AppData\Local\Temp\___go_build_main_go__1_.exe #gosetup {"level":"info","ts":1619418048.7640002,"caller":"log/main.go:8","msg":"***************************"} {"level":"info","ts":1619418048.7640002,"caller":"log/main.go:9","msg":"這是info日志"} {"level":"info","ts":1619418048.7640002,"caller":"log/main.go:10","msg":"***************************"} {"level":"warn","ts":1619418048.7640002,"caller":"log/main.go:11","msg":"這是warn日志"} {"level":"info","ts":1619418048.7640002,"caller":"log/main.go:12","msg":"***************************"} {"level":"error","ts":1619418048.7640002,"caller":"log/main.go:13","msg":"這是error日志","stacktrace":"main.main\n\tD:/work/goWorkspace/src/helloGoMod/log/main.go:13\nruntime.main\n\tD:/Program Files/Go/src/runtime/proc.go:225"} {"level":"info","ts":1619418048.7640002,"caller":"log/main.go:14","msg":"***************************"} {"level":"panic","ts":1619418048.7640002,"caller":"log/main.go:15","msg":"這是panic日志","stacktrace":"main.main\n\tD:/work/goWorkspace/src/helloGoMod/log/main.go:15\nruntime.main\n\tD:/Program Files/Go/src/runtime/proc.go:225"} panic: 這是panic日志goroutine 1 [running]: go.uber.org/zap/zapcore.(*CheckedEntry).Write(0xc00013c0c0, 0x0, 0x0, 0x0)D:/work/goWorkspace/pkg/mod/go.uber.org/zap@v1.16.0/zapcore/entry.go:234 +0x58d go.uber.org/zap.(*Logger).Panic(0xc0000482a0, 0x5b9c50, 0x11, 0x0, 0x0, 0x0)D:/work/goWorkspace/pkg/mod/go.uber.org/zap@v1.16.0/logger.go:226 +0x86 main.main()D:/work/goWorkspace/src/helloGoMod/log/main.go:15 +0x219Process finished with exit code 2SugaredLogger
如果我們需要像使用fmt.Printf一樣輸出,可以使用SugaredLogger,
通過log.Sugar()/Desugar()切換,
例子:
輸出
日志輸出到文件
創(chuàng)建Logger的方式和之前略有不同
package mainimport ("go.uber.org/zap""go.uber.org/zap/zapcore""os" )func main() {// 編碼器配置config := zap.NewProductionEncoderConfig()// 指定時間編碼器config.EncodeTime = zapcore.ISO8601TimeEncoder// 日志級別用大寫config.EncodeLevel = zapcore.CapitalLevelEncoder// 編碼器encoder := zapcore.NewConsoleEncoder(config)// 日志文件file, _ := os.Create("./test.log")// 寫日志sync := zapcore.AddSync(file)// 創(chuàng)建Loggercore := zapcore.NewCore(encoder, sync, zapcore.DebugLevel)logger := zap.New(core, zap.AddCaller())// 打印日志logger.Info("hello " + "yw" + ", helloZap")// SugaredLoggersLog := logger.Sugar()// 類似fmt.PrintlnsLog.Info("hello ", "yw, ", "helloZap")// 類似fmt.PrintfsLog.Infof("hello %v, helloZap", "yw") }運行
lumberjack
因為zap不支持日志分割,這里引入lumberjack
安裝
在項目目錄下命令行執(zhí)行
go get -u github.com/natefinch/lumberjack整合lumberjack
在WriteSyncer中添加lumberjack支持
package mainimport ("github.com/natefinch/lumberjack""go.uber.org/zap""go.uber.org/zap/zapcore" )func main() {// 編碼器配置config := zap.NewProductionEncoderConfig()// 指定時間編碼器config.EncodeTime = zapcore.ISO8601TimeEncoder// 日志級別用大寫config.EncodeLevel = zapcore.CapitalLevelEncoder// 編碼器encoder := zapcore.NewConsoleEncoder(config)/*// 日志文件file, _ := os.Create("./test.log")// 寫日志sync := zapcore.AddSync(file)*/// 修改為添加lumberjack支持lj := &lumberjack.Logger{Filename: "./test.log",MaxSize: 1, // 日志文件最大1MMaxBackups: 5,MaxAge: 7, // 日志保留最長時間7天Compress: false,}sync := zapcore.AddSync(lj)// 創(chuàng)建Loggercore := zapcore.NewCore(encoder, sync, zapcore.DebugLevel)logger := zap.New(core, zap.AddCaller())// 打印日志logger.Info("hello " + "yw" + ", helloZap")// SugaredLoggersLog := logger.Sugar()// 類似fmt.PrintlnsLog.Info("hello ", "yw, ", "helloZap")// 類似fmt.PrintfsLog.Infof("hello %v, helloZap", "yw") }我首先修改test.log文件內(nèi)容超過1M,然后再運行代碼
可以看到lumberjack已為我們生成了一個新的日志文件
總結(jié)
以上是生活随笔為你收集整理的go第三方日志库uber-go/zap、lumberjack的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 英语六级之完形填空
- 下一篇: 加密市场中的14个心智模型