Browse Source

add unit test for log.

topic
tian 5 years ago
parent
commit
5d280c31db
  1. 43
      dtmcli/logger/log.go
  2. 20
      dtmcli/logger/logger_test.go
  3. 2
      dtmsvr/svr.go

43
dtmcli/logger/log.go

@ -43,39 +43,38 @@ func WithLogger(log Logger) {
// InitLog is an initialization for a logger // InitLog is an initialization for a logger
// level can be: debug info warn error // level can be: debug info warn error
func InitLog(level string) { func InitLog(level string) {
config := zap.NewProductionConfig() config := loadConfig(level)
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
}
p, err := config.Build(zap.AddCallerSkip(1)) p, err := config.Build(zap.AddCallerSkip(1))
FatalIfError(err) FatalIfError(err)
logger = p.Sugar() logger = p.Sugar()
} }
// InitRotateLog is an initialization for a rotated logger by lumberjack // 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() config := zap.NewProductionConfig()
err := config.Level.UnmarshalText([]byte(logLevel)) err := config.Level.UnmarshalText([]byte(logLevel))
FatalIfError(err) FatalIfError(err)
config.EncoderConfig.EncodeTime = zapcore.ISO8601TimeEncoder config.EncoderConfig.EncodeTime = zapcore.ISO8601TimeEncoder
config.Encoding = "console" if os.Getenv("DTM_DEBUG") != "" {
config.EncoderConfig.EncodeLevel = zapcore.CapitalColorLevelEncoder config.Encoding = "console"
config.OutputPaths = []string{fmt.Sprintf("lumberjack:%s", logFile), "stdout"} config.EncoderConfig.EncodeLevel = zapcore.CapitalColorLevelEncoder
if output == "file" {
err := zap.RegisterSink("lumberjack", func(*url.URL) (zap.Sink, error) {
return lumberjackSink{
Logger: ll,
}, nil
})
FatalIfError(err)
} }
p, err := config.Build(zap.AddCallerSkip(1)) return config
FatalIfError(err)
logger = p.Sugar()
} }
// Debugf log to level debug // Debugf log to level debug

20
dtmcli/logger/logger_test.go

@ -4,6 +4,7 @@ import (
"os" "os"
"testing" "testing"
"github.com/natefinch/lumberjack"
"go.uber.org/zap" "go.uber.org/zap"
) )
@ -28,3 +29,22 @@ func TestWithLogger(t *testing.T) {
FatalfIf(false, "nothing") FatalfIf(false, "nothing")
FatalIfError(nil) 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")
}

2
dtmsvr/svr.go

@ -35,7 +35,7 @@ func StartSvr() {
MaxAge: int(conf.Log.FileMaxAge), MaxAge: int(conf.Log.FileMaxAge),
Compress: conf.Log.FileCompress != 0, 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) 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 { dtmgrpc.AddUnaryInterceptor(func(ctx context.Context, method string, req, reply interface{}, cc *grpc.ClientConn, invoker grpc.UnaryInvoker, opts ...grpc.CallOption) error {

Loading…
Cancel
Save