From 2dc08749e2de8f19ef869e7f89c9979edbbc71ff Mon Sep 17 00:00:00 2001 From: sbwml Date: Wed, 20 Sep 2023 21:05:18 +0800 Subject: [PATCH 5/5] format logtime --- mlog/logger.go | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-) diff --git a/mlog/logger.go b/mlog/logger.go index 861f091..1508db0 100644 --- a/mlog/logger.go +++ b/mlog/logger.go @@ -21,9 +21,11 @@ package mlog import ( "fmt" + "os" + "time" + "go.uber.org/zap" "go.uber.org/zap/zapcore" - "os" ) type LogConfig struct { @@ -64,10 +66,18 @@ func NewLogger(lc LogConfig) (*zap.Logger, error) { out = stderr } - if lc.Production { - return zap.New(zapcore.NewCore(zapcore.NewJSONEncoder(zap.NewProductionEncoderConfig()), out, lvl)), nil + encoderConfig := zap.NewDevelopmentEncoderConfig() + encoderConfig.EncodeTime = func(t time.Time, enc zapcore.PrimitiveArrayEncoder) { + enc.AppendString(t.Format("2006-01-02 15:04:05")) } - return zap.New(zapcore.NewCore(zapcore.NewConsoleEncoder(zap.NewDevelopmentEncoderConfig()), out, lvl)), nil + + core := zapcore.NewCore( + zapcore.NewConsoleEncoder(encoderConfig), + out, + lvl, + ) + + return zap.New(core), nil } // L is a global logger. -- 2.42.0