From 5d280c31db822a0a189cc711c571ff1453521944 Mon Sep 17 00:00:00 2001 From: tian Date: Mon, 17 Jan 2022 00:41:22 +0800 Subject: [PATCH] add unit test for log. --- dtmcli/logger/log.go | 43 ++++++++++++++++++------------------ dtmcli/logger/logger_test.go | 20 +++++++++++++++++ dtmsvr/svr.go | 2 +- 3 files changed, 42 insertions(+), 23 deletions(-) diff --git a/dtmcli/logger/log.go b/dtmcli/logger/log.go index 54237df..e2ef110 100644 --- a/dtmcli/logger/log.go +++ b/dtmcli/logger/log.go @@ -43,39 +43,38 @@ func WithLogger(log Logger) { // InitLog is an initialization for a logger // level can be: debug info warn error func InitLog(level string) { - config := zap.NewProductionConfig() - err := config.Level.UnmarshalText([]byte(level)) - FatalIfError(err) - config.EncoderConfig.EncodeTime = zapcore.ISO8601TimeEncoder - if os.Getenv("DTM_DEBUG") != "" { - config.Encoding = "console" - config.EncoderConfig.EncodeLevel = zapcore.CapitalColorLevelEncoder - } + config := loadConfig(level) p, err := config.Build(zap.AddCallerSkip(1)) FatalIfError(err) logger = p.Sugar() } // InitRotateLog is an initialization for a rotated logger by lumberjack -func InitRotateLog(logLevel string, output, logFile string, ll *lumberjack.Logger) { +func InitRotateLog(logLevel string, ll *lumberjack.Logger) { + config := loadConfig(logLevel) + config.OutputPaths = []string{fmt.Sprintf("lumberjack:%s", ll.Filename), "stdout"} + err := zap.RegisterSink("lumberjack", func(*url.URL) (zap.Sink, error) { + return lumberjackSink{ + Logger: ll, + }, nil + }) + FatalIfError(err) + + p, err := config.Build(zap.AddCallerSkip(1)) + FatalIfError(err) + logger = p.Sugar() +} + +func loadConfig(logLevel string) zap.Config { config := zap.NewProductionConfig() err := config.Level.UnmarshalText([]byte(logLevel)) FatalIfError(err) config.EncoderConfig.EncodeTime = zapcore.ISO8601TimeEncoder - config.Encoding = "console" - config.EncoderConfig.EncodeLevel = zapcore.CapitalColorLevelEncoder - config.OutputPaths = []string{fmt.Sprintf("lumberjack:%s", logFile), "stdout"} - if output == "file" { - err := zap.RegisterSink("lumberjack", func(*url.URL) (zap.Sink, error) { - return lumberjackSink{ - Logger: ll, - }, nil - }) - FatalIfError(err) + if os.Getenv("DTM_DEBUG") != "" { + config.Encoding = "console" + config.EncoderConfig.EncodeLevel = zapcore.CapitalColorLevelEncoder } - p, err := config.Build(zap.AddCallerSkip(1)) - FatalIfError(err) - logger = p.Sugar() + return config } // Debugf log to level debug diff --git a/dtmcli/logger/logger_test.go b/dtmcli/logger/logger_test.go index 26797e0..bcba763 100644 --- a/dtmcli/logger/logger_test.go +++ b/dtmcli/logger/logger_test.go @@ -4,6 +4,7 @@ import ( "os" "testing" + "github.com/natefinch/lumberjack" "go.uber.org/zap" ) @@ -28,3 +29,22 @@ func TestWithLogger(t *testing.T) { FatalfIf(false, "nothing") FatalIfError(nil) } + +func TestInitRotateLog(t *testing.T) { + os.Setenv("DTM_DEBUG", "1") + ll := lumberjack.Logger{ + Filename: "test.log", + MaxSize: 1, + MaxBackups: 1, + MaxAge: 1, + Compress: false, + } + InitRotateLog("debug", &ll) + Debugf("a debug msg") + Infof("a info msg") + Warnf("a warn msg") + Errorf("a error msg") + FatalfIf(false, "nothing") + FatalIfError(nil) + _ = os.Remove("test.log") +} diff --git a/dtmsvr/svr.go b/dtmsvr/svr.go index 0470f0a..8f25501 100644 --- a/dtmsvr/svr.go +++ b/dtmsvr/svr.go @@ -35,7 +35,7 @@ func StartSvr() { MaxAge: int(conf.Log.FileMaxAge), Compress: conf.Log.FileCompress != 0, } - logger.InitRotateLog(conf.Log.Level, conf.Log.Output, conf.Log.FileName, &ll) + logger.InitRotateLog(conf.Log.Level, &ll) } dtmcli.GetRestyClient().SetTimeout(time.Duration(conf.RequestTimeout) * time.Second) dtmgrpc.AddUnaryInterceptor(func(ctx context.Context, method string, req, reply interface{}, cc *grpc.ClientConn, invoker grpc.UnaryInvoker, opts ...grpc.CallOption) error {