Pack up the Zap, out of the box. :)
go get -u github.com/Flyingmn/gzap// High performance: Debug, Info, Warn, Error, DPanic, Panic, Fatal
gzap.Info("hello world", zap.String("name", "zhangsan"), zap.Int("age", 18))
/* Output the logs on the same line. Here, in order to demonstrate the formatting of JSON
{
"level":"info",
"time":"2024-10-01 12:00:00.000",
"line":"/gzap/zap_test.go:143",
"func":"github.com/Flyingmn/gzap_test.TestFunc",
"msg":"hello world",
"name":"zhangsan",
"age":18
}
*/// Usage in performance insensitive scenarios: Debugw, Infow, Warnw, Errorw, DPanicw, Panicw, Fatalw
gzap.Infow("hello world", "name", "zhangsan", "age", 18)
// {"level":"info","msg":"hello world","name":"zhangsan","age":18}// Debugf, Infof, Warnf, Errorf, DPanicf, Panicf, Fatalf
gzap.Infof("hello world; name:%s; age:%d", "zhangsan", 18)
// {"level":"info","msg":"hello world; name:zhangsan; age:18"}gzap.Info(
"hello world",
zap.Namespace("user1"),
zap.String("name", "zhangsan"),
zap.Int("age", 18),
zap.Namespace("user2"),
zap.String("name", "lisi"),
zap.Int("age", 19)
)
// {"level":"info","msg":"hello world","user1":{"name":"zhangsan","age":18}}// Default info level, if you want to customize the level (note that SetZapCfg needs to be set before using logs)
gzap.SetZapCfg(gzap.ZapLevel("info"))// (Note that SetZapCfg needs to be set before using zap)
gzap.SetZapCfg(gzap.SetPresetFields(map[string]any{"service": "myservice"}))
gzap.Info("hello world")
// {"level":"info","msg":"hello world","service":"myservice"}//(Note that SetZapCfg needs to be set before using zap)
gzap.SetZapCfg(
gzap.ZapOutFile(
"./log/test.log", // file location
gzap.ZapOutFileMaxSize(128), // the maximum size of the log file (in MB)
gzap.ZapOutFileMaxAge(7), // maximum number of days to retain old files
gzap.ZapOutFileMaxBackups(30), // maximum number of old files retained
),
)
gzap.Info("hello world", zap.String("name", "zhangsan"), zap.Int("age", 18))// 自定义配置后传入 (Note that SetZapCfg needs to be set before using zap)
config := zap.Config{
Level: zap.NewAtomicLevelAt(zap.InfoLevel), // Log Level
Development: true, // development mode, stack trace
Encoding: "json", // Output format console or JSON
EncoderConfig: zapcore.EncoderConfig{
TimeKey: "time",
LevelKey: "level",
NameKey: "name",
CallerKey: "line",
MessageKey: "msg",
FunctionKey: "func",
StacktraceKey: "stacktrace",
LineEnding: zapcore.DefaultLineEnding,
EncodeLevel: zapcore.LowercaseLevelEncoder, //Lowercase encoder
EncodeTime: zapcore.TimeEncoderOfLayout("2006-01-02 15:04:05.000"),//Define time format
EncodeDuration: zapcore.SecondsDurationEncoder,
EncodeCaller: zapcore.FullCallerEncoder, // Full path encoder
}, // Encoder configuration
InitialFields: map[string]interface{}{
"app": "test",
},
}
gzap.SetZapCfg(gzap.ZapConf(conf))
gzap.Info("hello world", zap.String("name", "zhangsan"), zap.Int("age", 18))
// {"level":"info","msg":"hello world","app":"test","name":"zhangsan","age":18}// Get logger:gzap.Zap();
gzap.Zap().Log(
zap.InfoLevel,
"hello world",
zap.String("name", "zhangsan"),
zap.Int("age", 18)
)
// {"level":"info","msg":"hello world","name":"zhangsan","age":18}
// Get sugaredLogger: gzap.Sap()
gzap.Sap().Infoln("hello world", "name", "zhangsan", "age", 18)
// {"level":"info","msg":"hello world name zhangsan age 18"}