golang高性能日志库zap配置示例
zap是uber开源的Go高性能日志库,gitlab地址
安装
go get -u go.uber.org/zap
请注意,zap仅支持两个最新的Go版本。
示例
简单示例
格式化输出
package main
import (
"go.uber.org/zap"
"time"
)
func main() {
// zap.NewDevelopment 格式化输出
logger, _ := zap.NewDevelopment()
defer logger.Sync()
logger.Info("无法获取网址",
zap.String("url", "http://www.baidu.com"),
zap.Int("attempt", 3),
zap.Duration("backoff", time.Second),
)
}
格式化输出打印结果:
2019-01-02T15:01:13.923+0800 INFO spikeProxy/main.go:17 failed to fetch URL {"url": "http://www.baidu.com", "attempt": 3, "backoff": "1s"}
json 序列化输出
package main
import (
"go.uber.org/zap"
"time"
)
func main() {
// zap.NewProduction json序列化输出
logger, _ := zap.NewProduction()
defer logger.Sync()
logger.Info("无法获取网址",
zap.String("url", "http://www.baidu.com"),
zap.Int("attempt", 3),
zap.Duration("backoff", time.Second),
)
}
json序列化输出打印结果:
{"level":"info","ts":1546413239.1466308,"caller":"spikeProxy/main.go:16","msg":"无法获取网址","url":"http://www.baidu.com","attempt":3,"backoff":1}
自定义示例
选择一个日志库除了高性能是考量的一个标准,高扩展也非常重要,例如:json key 自定义、时间格式化、日志级别等。
package main
import (
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
"fmt"
"time"
)
func main() {
encoderConfig := zapcore.EncoderConfig{
TimeKey: "time",
LevelKey: "level",
NameKey: "logger",
CallerKey: "caller",
MessageKey: "msg",
StacktraceKey: "stacktrace",
LineEnding: zapcore.DefaultLineEnding,
EncodeLevel: zapcore.LowercaseLevelEncoder, // 小写编码器
EncodeTime: zapcore.ISO8601TimeEncoder, // ISO8601 UTC 时间格式
EncodeDuration: zapcore.SecondsDurationEncoder,
EncodeCaller: zapcore.FullCallerEncoder, // 全路径编码器
}
// 设置日志级别
atom := zap.NewAtomicLevelAt(zap.DebugLevel)
config := zap.Config{
Level: atom, // 日志级别
Development: true, // 开发模式,堆栈跟踪
Encoding: "json", // 输出格式 console 或 json
EncoderConfig: encoderConfig, // 编码器配置
InitialFields: map[string]interface{}{"serviceName": "spikeProxy"}, // 初始化字段,如:添加一个服务器名称
OutputPaths: []string{"stdout", "./logs/spikeProxy.log"}, // 输出到指定文件 stdout(标准输出,正常颜色) stderr(错误输出,红色)
ErrorOutputPaths: []string{"stderr"},
}
// 构建日志
logger, err := config.Build()
if err != nil {
panic(fmt.Sprintf("log 初始化失败: %v", err))
}
logger.Info("log 初始化成功")
logger.Info("无法获取网址",
zap.String("url", "http://www.baidu.com"),
zap.Int("attempt", 3),
zap.Duration("backoff", time.Second),
)
}
打印结果:
{"level":"info","time":"2019-01-02T15:38:33.778+0800","caller":"/Users/lcl/go/src/spikeProxy/main.go:54","msg":"log 初始化成功","serviceName":"spikeProxy"}
{"level":"info","time":"2019-01-02T15:38:33.778+0800","caller":"/Users/lcl/go/src/spikeProxy/main.go:56","msg":"无法获取网址","serviceName":"spikeProxy","url":"http://www.baidu.com","attempt":3,"backoff":1}
写入归档文件示例
安装 lumberjack
go get gopkg.in/natefinch/lumberjack.v2
lumberjack介绍
Lumberjack是一个Go包,用于将日志写入滚动文件。
zap 不支持文件归档,如果要支持文件按大小或者时间归档,需要使用lumberjack,lumberjack也是zap官方推荐的。
示例
package main
import (
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
"time"
"gopkg.in/natefinch/lumberjack.v2"
"os"
)
func main() {
hook := lumberjack.Logger{
Filename: "./logs/spikeProxy1.log", // 日志文件路径
MaxSize: 128, // 每个日志文件保存的最大尺寸 单位:M
MaxBackups: 30, // 日志文件最多保存多少个备份
MaxAge: 7, // 文件最多保存多少天
Compress: true, // 是否压缩
}
encoderConfig := zapcore.EncoderConfig{
TimeKey: "time",
LevelKey: "level",
NameKey: "logger",
CallerKey: "linenum",
MessageKey: "msg",
StacktraceKey: "stacktrace",
LineEnding: zapcore.DefaultLineEnding,
EncodeLevel: zapcore.LowercaseLevelEncoder, // 小写编码器
EncodeTime: zapcore.ISO8601TimeEncoder, // ISO8601 UTC 时间格式
EncodeDuration: zapcore.SecondsDurationEncoder, //
EncodeCaller: zapcore.FullCallerEncoder, // 全路径编码器
EncodeName: zapcore.FullNameEncoder,
}
// 设置日志级别
atomicLevel := zap.NewAtomicLevel()
atomicLevel.SetLevel(zap.InfoLevel)
core := zapcore.NewCore(
zapcore.NewJSONEncoder(encoderConfig), // 编码器配置
zapcore.NewMultiWriteSyncer(zapcore.AddSync(os.Stdout), zapcore.AddSync(&hook)), // 打印到控制台和文件
atomicLevel, // 日志级别
)
// 开启开发模式,堆栈跟踪
caller := zap.AddCaller()
// 开启文件及行号
development := zap.Development()
// 设置初始化字段
filed := zap.Fields(zap.String("serviceName", "serviceName"))
// 构造日志
logger := zap.New(core, caller, development, filed)
logger.Info("log 初始化成功")
logger.Info("无法获取网址",
zap.String("url", "http://www.baidu.com"),
zap.Int("attempt", 3),
zap.Duration("backoff", time.Second))
}
控制台打印结果:
{"level":"info","time":"2019-01-02T16:14:43.608+0800","linenum":"/Users/lcl/go/src/spikeProxy/main.go:56","msg":"log 初始化成功","serviceName":"serviceName"}
{"level":"info","time":"2019-01-02T16:14:43.608+0800","linenum":"/Users/lcl/go/src/spikeProxy/main.go:57","msg":"无法获取网址","serviceName":"serviceName","url":"http://www.baidu.com","attempt":3,"backoff":1}
文件打印结果:
{"level":"info","time":"2019-01-02T16:14:43.608+0800","linenum":"/Users/lcl/go/src/spikeProxy/main.go:56","msg":"log 初始化成功","serviceName":"serviceName"}
{"level":"info","time":"2019-01-02T16:14:43.608+0800","linenum":"/Users/lcl/go/src/spikeProxy/main.go:57","msg":"无法获取网址","serviceName":"serviceName","url":"http://www.baidu.com","attempt":3,"backoff":1}
结语
目前关于zap的资料很少,都需要自己动手总结,在使用过程中有更多的使用方式我也会持续更新,有问题请在下方留言,我会尽快回复的。
有疑问加站长微信联系(非本文作者)