Go:zap 自义定时间戳格式

Go:zap 自义定时间戳格式


1.背景

使得zap输出的日志时间戳形如:2021-05-25 22:36:23.107(毫秒)


2.Demo:
package main

import (
	"go.uber.org/zap"
	"go.uber.org/zap/zapcore"
	"gopkg.in/natefinch/lumberjack.v2"
)

func main() {
	// 日志级别
	logLevel := "DEBUG"

	atomicLevel := zap.NewAtomicLevel()
	switch logLevel {
		case "DEBUG":
			atomicLevel.SetLevel(zapcore.DebugLevel)
		case "INFO":
			atomicLevel.SetLevel(zapcore.InfoLevel)
		case "WARN":
			atomicLevel.SetLevel(zapcore.WarnLevel)
		case "ERROR":
			atomicLevel.SetLevel(zapcore.ErrorLevel)
		case "DPANIC":
			atomicLevel.SetLevel(zapcore.DPanicLevel)
		case "PANIC":
			atomicLevel.SetLevel(zapcore.PanicLevel)
		case "FATAL":
			atomicLevel.SetLevel(zapcore.FatalLevel)
	}
	encoderConfig := zapcore.EncoderConfig{
		TimeKey:        "time",
		LevelKey:       "level",
		NameKey:        "name",
		CallerKey:      "line",
		MessageKey:     "msg",
		FunctionKey:    "func",
		StacktraceKey:  "stacktrace",
		LineEnding:     zapcore.DefaultLineEnding,
		EncodeLevel:    zapcore.LowercaseLevelEncoder,
		EncodeTime:     zapcore.TimeEncoderOfLayout("2006-01-02 15:04:05.000"),
		EncodeDuration: zapcore.SecondsDurationEncoder,
		EncodeCaller:   zapcore.FullCallerEncoder,
		EncodeName:     zapcore.FullNameEncoder,
	}

	// 日志轮转
	writer := &lumberjack.Logger{
		// 日志名称
		Filename:   "test1280.log",
		// 日志大小限制,单位MB
		MaxSize:    100,
		// 历史日志文件保留天数
		MaxAge:     30,
		// 最大保留历史日志数量
		MaxBackups: 10,
		// 本地时区
		LocalTime:  true,
		// 历史日志文件压缩标识
		Compress:   false,
	}

	zapCore := zapcore.NewCore(
		zapcore.NewJSONEncoder(encoderConfig),
		zapcore.AddSync(writer),
		atomicLevel,
	)
	logger := zap.New(zapCore, zap.AddCaller())
	defer logger.Sync()

	logger.Sugar().Debug("test1280's debug")
	logger.Sugar().Infof("test1280's %s", "infof")
	logger.Sugar().Warnf("test1280's %s", "warnf")
	logger.Sugar().Error("test1280's error")
}

关键核心:

encoderConfig := zapcore.EncoderConfig{
	...
	EncodeTime:     zapcore.TimeEncoderOfLayout("2006-01-02 15:04:05.000"),
	...
}

输出效果:

EB@DESKTOP-K45IA6V MINGW64 ~/Desktop/test1280
$ cat test1280.log
{"level":"debug","time":"2021-05-25 22:36:23.091","line":"C:/Users/EB/Desktop/test1280/main.go:71","func":"main.main","msg":"tes
t1280's debug"}
{"level":"info","time":"2021-05-25 22:36:23.107","line":"C:/Users/EB/Desktop/test1280/main.go:72","func":"main.main","msg":"test
1280's infof"}
{"level":"warn","time":"2021-05-25 22:36:23.107","line":"C:/Users/EB/Desktop/test1280/main.go:73","func":"main.main","msg":"test
1280's warnf"}
{"level":"error","time":"2021-05-25 22:36:23.107","line":"C:/Users/EB/Desktop/test1280/main.go:74","func":"main.main","msg":"tes
t1280's error"}

3.EncodeTime

关于zapcore.EncoderConfig的EncodeTime值,可以选择:

1.zapcore.ISO8601TimeEncoder
2.zapcore.RFC3339TimeEncoder
3.zapcore.RFC3339NanoTimeEncoder
4.自定义:zapcore.TimeEncoderOfLayout(“2006-01-02 15:04:05.000”)

Go:zap 自义定时间戳格式


参考:
1.https://blog.csdn.net/test1280/article/details/117266163

上一篇:【BZOJ1106】【POI2007】立方体大作战tet(贪心+树状数组/线段树优化区间和)


下一篇:mica 1.1.7 发布 mica-http 组件毕业从 http 到轻量级爬虫