1. 简要说明
zapuberGolumberjackzap
DEBUGINFOWARNERROR
2. 下载安装
使用下面命令安装
go get github.com/uber-go/zap
3.配置 zap Logger
zapLogger Sugared Logger
区别是:
SugaredLoggerprintf
3.1 Logger
zap.NewProduction()zap.NewDevelopment()zap.NewExample()Loggerproduction loggerLogger INFOERROR
3.1.1 NewExample
代码示例:
package main
import (
"go.uber.org/zap"
)
func main() {
log := zap.NewExample()
log.Debug("this is debug message")
log.Info("this is info message")
log.Info("this is info message with fileds",
zap.Int("age", 24), zap.String("agender", "man"))
log.Warn("this is warn message")
log.Error("this is error message")
log.Panic("this is panic message")
}
输出结果:
{"level":"debug","msg":"this is debug message"}
{"level":"info","msg":"this is info message"}
{"level":"info","msg":"this is info message with fileds","age":24,"agender":"man"}
{"level":"warn","msg":"this is warn message"}
{"level":"error","msg":"this is error message"}
{"level":"panic","msg":"this is panic message"}
panic: this is panic message
3.1.2 NewDevelopment
代码示例:
func main() {
log, _ := zap.NewDevelopment()
log.Debug("this is debug message")
log.Info("this is info message")
log.Info("this is info message with fileds",
zap.Int("age", 24), zap.String("agender", "man"))
log.Warn("this is warn message")
log.Error("this is error message")
// log.DPanic("This is a DPANIC message")
// log.Panic("this is panic message")
// log.Fatal("This is a FATAL message")
}
输出结果:
2020-06-12T18:51:11.457+0800 DEBUG task/main.go:9 this is debug message
2020-06-12T18:51:11.457+0800 INFO task/main.go:10 this is info message
2020-06-12T18:51:11.457+0800 INFO task/main.go:11 this is info message with fileds {"age": 24, "agender": "man"}
2020-06-12T18:51:11.457+0800 WARN task/main.go:13 this is warn message
main.main
/home/wohu/GoCode/src/task/main.go:13
runtime.main
/usr/local/go/src/runtime/proc.go:200
2020-06-12T18:51:11.457+0800 ERROR task/main.go:14 this is error message
main.main
/home/wohu/GoCode/src/task/main.go:14
runtime.main
/usr/local/go/src/runtime/proc.go:200
3.1.3 NewProduction
代码示例:
func main() {
log, _ := zap.NewProduction()
log.Debug("this is debug message")
log.Info("this is info message")
log.Info("this is info message with fileds",
zap.Int("age", 24), zap.String("agender", "man"))
log.Warn("this is warn message")
log.Error("this is error message")
// log.DPanic("This is a DPANIC message")
// log.Panic("this is panic message")
// log.Fatal("This is a FATAL message")
}
输出结果:
{"level":"info","ts":1591959367.316352,"caller":"task/main.go:10","msg":"this is info message"}
{"level":"info","ts":1591959367.3163702,"caller":"task/main.go:11","msg":"this is info message with fileds","age":24,"agender":"man"}
{"level":"warn","ts":1591959367.3163917,"caller":"task/main.go:13","msg":"this is warn message"}
{"level":"error","ts":1591959367.3163974,"caller":"task/main.go:14","msg":"this is error message","stacktrace":"main.main\n\t/home/wohu/GoCode/src/task/main.go:14\nruntime.main\n\t/usr/local/go/src/runtime/proc.go:200"}
3.1.4 对比总结
ExampleProductionjsondevelopmentDevelopmentjsonProduction
LoggerInfoErrorLogger
日志记录器方法的语法是这样的:
func (log *Logger) MethodXXX(msg string,fields ...Field)
其中MethodXXX是一个可变参数函数,可以是Info/Error/Debug/Panic等。每个方法都接受一个消息字符串和任意数量的zapcore.Field长参数。
每个zapcore.Field其实就是一组键值对参数。
3.2 Sugared Logger
zap
log.Info("this is info message with fields",
zap.Int("age",24),zap.String("agender","man"))
Sugared Loggerprintf
我们使用Sugared Logger来实现相同的功能。
logger.Sugar()SugaredLoggerSugaredLoggerprintf
func main(){
logger,_:=zap.NewDevelopment()
slogger:=logger.Sugar()
slogger.Debugf("debug message age is %d,agender is %s",19,"man")
slogger.Info("Info() uses sprint")
slogger.Infof("Infof() uses %s","sprintf")
slogger.Infow("Infow() allows tags","name","Legolas","type",1)
}
输出结果:
2020-06-12T19:23:54.184+0800 DEBUG task/main.go:11 debug message age is 19, agender is man
2020-06-12T19:23:54.185+0800 INFO task/main.go:12 Info() uses sprint
2020-06-12T19:23:54.185+0800 INFO task/main.go:13 Infof() uses sprintf
2020-06-12T19:23:54.185+0800 INFO task/main.go:14 Infow() allows tags {"name": "Legolas", "type": 1}
如果需要,可以随时使用记录器上的.Desugar()方法从sugar logger切换到标准记录器。
log := slogger.Desugar()
log.Info("After Desugar; INFO message")
log.Warn("After Desugar; WARN message")
log.Error("After Desugar; ERROR message")
4.将日志写入文件
zap.New()zap.NewProduction()logger
func New(core zapcore.Core,options ...Option) *Logger
zapcore.Core需要三个配置—Encoder,WriteSyncer,LogLevel。
EncoderNewConsoleEncoder()ProductionEncoderConfig()
zapcore.NewConsoleEncoder(zap.NewProductionEncoderConfig())
writeSyncerzapcore.AddSync()
file,_:=os.Create("./test.log")
writeSyncer:=zapcore.AddSync(file)
- Log Level: 哪种级别的日志将被写入。
代码示例:
package main
import (
"os"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
)
var sugarLogger *zap.SugaredLogger
func InitLogger() {
encoder := getEncoder()
writeSyncer := getLogWriter()
core := zapcore.NewCore(encoder, writeSyncer, zapcore.DebugLevel)
// zap.AddCaller() 添加将调用函数信息记录到日志中的功能。
logger := zap.New(core, zap.AddCaller())
sugarLogger = logger.Sugar()
}
func getEncoder() zapcore.Encoder {
encoderConfig := zap.NewProductionEncoderConfig()
encoderConfig.EncodeTime = zapcore.ISO8601TimeEncoder // 修改时间编码器
// 在日志文件中使用大写字母记录日志级别
encoderConfig.EncodeLevel = zapcore.CapitalLevelEncoder
// NewConsoleEncoder 打印更符合人们观察的方式
return zapcore.NewConsoleEncoder(encoderConfig)
}
func getLogWriter() zapcore.WriteSyncer {
file, _ := os.Create("./test.log")
return zapcore.AddSync(file)
}
func main() {
InitLogger()
sugarLogger.Info("this is info message")
sugarLogger.Infof("this is %s, %d", "aaa", 1234)
sugarLogger.Error("this is error message")
sugarLogger.Info("this is info message")
}
输出日志文件:
2020-06-16T09:01:06.192+0800 INFO task/main.go:40 this is info message
2020-06-16T09:01:06.192+0800 INFO task/main.go:41 this is aaa, 1234
2020-06-16T09:01:06.192+0800 ERROR task/main.go:42 this is error message
2020-06-16T09:01:06.192+0800 INFO task/main.go:43 this is info message
5.使用lumberjack进行日志切割归档
zaplumberjack
5.1安装lumberjack
go get github.com/natefinch/lumberjack
5.2 将lumberjack加入zap logger
要在zap中加入lumberjack支持,我们需要修改WriteSyncer代码。我们将按照下面的代码修改getLogWriter()函数:
func getLogWriter() zapcore.WriteSyncer{
lumberJackLogger:=&lumberjack.Logger{
Filename: "./test.log",
MaxSize: 10,
MaxBackups:5,
MaxAge: 30,
Compress: false,
}
return zapcore.AddSync(lumberJackLogger)
}
Lumberjack Logger
FilenameMaxSizeMaxBackupsMaxAgesCompress
完整代码:
package main
import (
"github.com/natefinch/lumberjack"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
)
var sugarLogger *zap.SugaredLogger
func InitLogger() {
encoder := getEncoder()
writeSyncer := getLogWriter()
core := zapcore.NewCore(encoder, writeSyncer, zapcore.DebugLevel)
// zap.AddCaller() 添加将调用函数信息记录到日志中的功能。
logger := zap.New(core, zap.AddCaller())
sugarLogger = logger.Sugar()
}
func getEncoder() zapcore.Encoder {
encoderConfig := zap.NewProductionEncoderConfig()
encoderConfig.EncodeTime = zapcore.ISO8601TimeEncoder // 修改时间编码器
// 在日志文件中使用大写字母记录日志级别
encoderConfig.EncodeLevel = zapcore.CapitalLevelEncoder
// NewConsoleEncoder 打印更符合人们观察的方式
return zapcore.NewConsoleEncoder(encoderConfig)
}
func getLogWriter() zapcore.WriteSyncer {
lumberJackLogger := &lumberjack.Logger{
Filename: "./test.log",
MaxSize: 10,
MaxBackups: 5,
MaxAge: 30,
Compress: false,
}
return zapcore.AddSync(lumberJackLogger)
}
func main() {
InitLogger()
sugarLogger.Info("this is info message")
sugarLogger.Infof("this is %s, %d", "aaa", 1234)
sugarLogger.Error("this is error message")
sugarLogger.Info("this is info message")
}
6. Log 第三方库 uber-zap 使用
package main
import (
"time"
"github.com/natefinch/lumberjack"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
)
var logger *zap.Logger
// logpath 日志文件路径
// loglevel 日志级别
func InitLogger(logpath string, loglevel string) {
// 日志分割
hook := lumberjack.Logger{
Filename: logpath, // 日志文件路径,默认 os.TempDir()
MaxSize: 10, // 每个日志文件保存10M,默认 100M
MaxBackups: 30, // 保留30个备份,默认不限
MaxAge: 7, // 保留7天,默认不限
Compress: true, // 是否压缩,默认不压缩
}
write := zapcore.AddSync(&hook)
// 设置日志级别
// debug 可以打印出 info debug warn
// info 级别可以打印 warn info
// warn 只能打印 warn
// debug->info->warn->error
var level zapcore.Level
switch loglevel {
case "debug":
level = zap.DebugLevel
case "info":
level = zap.InfoLevel
case "error":
level = zap.ErrorLevel
default:
level = zap.InfoLevel
}
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(level)
core := zapcore.NewCore(
// zapcore.NewConsoleEncoder(encoderConfig),
zapcore.NewJSONEncoder(encoderConfig),
// zapcore.NewMultiWriteSyncer(zapcore.AddSync(os.Stdout), zapcore.AddSync(&write)), // 打印到控制台和文件
write,
level,
)
// 开启开发模式,堆栈跟踪
caller := zap.AddCaller()
// 开启文件及行号
development := zap.Development()
// 设置初始化字段,如:添加一个服务器名称
filed := zap.Fields(zap.String("serviceName", "serviceName"))
// 构造日志
logger = zap.New(core, caller, development, filed)
logger.Info("DefaultLogger init success")
}
func main() {
// 历史记录日志名字为:all.log,服务重新启动,日志会追加,不会删除
InitLogger("./all.log", "debug")
// 强结构形式
logger.Info("test",
zap.String("string", "string"),
zap.Int("int", 3),
zap.Duration("time", time.Second),
)
// 必须 key-value 结构形式 性能下降一点
logger.Sugar().Infow("test-",
"string", "string",
"int", 1,
"time", time.Second,
)
}
从例子看出:
- 它同时提供了结构化日志记录和 printf 风格的日志记录
- 先初始化 lumberjack 后初始化 zap