From 54ef71ba71fafa33e47e0c2da3215522cd8402ea Mon Sep 17 00:00:00 2001 From: yedf2 <43027375+yedf2@users.noreply.github.com> Date: Mon, 17 Jan 2022 08:47:31 +0800 Subject: [PATCH] Revert "Write log to file" --- bench/main.go | 2 +- conf.sample.yml | 10 +-------- dtmcli/logger/log.go | 40 ++++-------------------------------- dtmcli/logger/logger_test.go | 20 ------------------ dtmsvr/config/config.go | 13 +----------- dtmsvr/svr.go | 11 ---------- go.mod | 1 - main.go | 2 +- 8 files changed, 8 insertions(+), 91 deletions(-) diff --git a/bench/main.go b/bench/main.go index 50e488b..34bd859 100644 --- a/bench/main.go +++ b/bench/main.go @@ -33,7 +33,7 @@ func main() { } logger.Infof("starting bench server") config.MustLoadConfig("") - logger.InitLog(conf.Log.Level) + logger.InitLog(conf.LogLevel) if busi.BusiConf.Driver != "" { dtmcli.SetCurrentDBType(busi.BusiConf.Driver) svr.PrepareBenchDB() diff --git a/conf.sample.yml b/conf.sample.yml index 5c046ed..cd66cb6 100644 --- a/conf.sample.yml +++ b/conf.sample.yml @@ -48,15 +48,7 @@ # RetryInterval: 10 # the subtrans branch will be retried after this interval # RequestTimeout: 3 # the timeout of HTTP/gRPC request in dtm -# Log: -# Level: 'info' # default: info. can be debug|info|warn|error -# Output: 'console' # default: console. can be console|file -# FileName: '/tmp/dtm.log' # default: /tmp/dtm.log. -# FileMaxSize: 10 # default: 10, unit: MB. -# FileMaxBackups: 5 # default: 5. -# FileMaxAge: 30 # default: 30, unit: days. -# FileCompress: 0 # default: 0. can by 0|1, means false|true - +# LogLevel: 'info' # default: info. can be debug|info|warn|error # HttpPort: 36789 # GrpcPort: 36790 diff --git a/dtmcli/logger/log.go b/dtmcli/logger/log.go index e2ef110..59b3d43 100644 --- a/dtmcli/logger/log.go +++ b/dtmcli/logger/log.go @@ -1,12 +1,9 @@ package logger import ( - "fmt" "log" - "net/url" "os" - "github.com/natefinch/lumberjack" "go.uber.org/zap" "go.uber.org/zap/zapcore" ) @@ -27,14 +24,6 @@ type Logger interface { Errorf(format string, args ...interface{}) } -type lumberjackSink struct { - *lumberjack.Logger -} - -func (lumberjackSink) Sync() error { - return nil -} - // WithLogger replaces default logger func WithLogger(log Logger) { logger = log @@ -43,38 +32,17 @@ func WithLogger(log Logger) { // InitLog is an initialization for a logger // level can be: debug info warn error func InitLog(level string) { - 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, 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)) + 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 } - return config + p, err := config.Build(zap.AddCallerSkip(1)) + FatalIfError(err) + logger = p.Sugar() } // Debugf log to level debug diff --git a/dtmcli/logger/logger_test.go b/dtmcli/logger/logger_test.go index bcba763..26797e0 100644 --- a/dtmcli/logger/logger_test.go +++ b/dtmcli/logger/logger_test.go @@ -4,7 +4,6 @@ import ( "os" "testing" - "github.com/natefinch/lumberjack" "go.uber.org/zap" ) @@ -29,22 +28,3 @@ 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/config/config.go b/dtmsvr/config/config.go index cc74975..2e84887 100644 --- a/dtmsvr/config/config.go +++ b/dtmsvr/config/config.go @@ -29,17 +29,6 @@ type MicroService struct { EndPoint string `yaml:"EndPoint"` } -// Log config customize log -type Log struct { - Level string `yaml:"Level" default:"info"` - Output string `yaml:"Output" default:"console"` - FileName string `yaml:"FileName" default:"/tmp/dtm.log"` - FileMaxSize int64 `yaml:"FileMaxSize" default:"10"` - FileMaxBackups int64 `yaml:"FileMaxBackups" default:"5"` - FileMaxAge int64 `yaml:"FileMaxAge" default:"30"` - FileCompress int64 `yaml:"FileCompress" default:"0"` -} - // Store defines storage relevant info type Store struct { Driver string `yaml:"Driver" default:"boltdb"` @@ -83,7 +72,7 @@ type configType struct { MicroService MicroService `yaml:"MicroService"` UpdateBranchSync int64 `yaml:"UpdateBranchSync"` UpdateBranchAsyncGoroutineNum int64 `yaml:"UpdateBranchAsyncGoroutineNum" default:"1"` - Log Log `yaml:"Log"` + LogLevel string `yaml:"LogLevel" default:"info"` } // Config 配置 diff --git a/dtmsvr/svr.go b/dtmsvr/svr.go index 8f25501..56f288c 100644 --- a/dtmsvr/svr.go +++ b/dtmsvr/svr.go @@ -20,23 +20,12 @@ import ( "github.com/dtm-labs/dtm/dtmutil" "github.com/dtm-labs/dtmdriver" grpc_middleware "github.com/grpc-ecosystem/go-grpc-middleware" - "github.com/natefinch/lumberjack" "google.golang.org/grpc" ) // StartSvr StartSvr func StartSvr() { logger.Infof("start dtmsvr") - if conf.Log.Output == "file" { - ll := lumberjack.Logger{ - Filename: conf.Log.FileName, - MaxSize: int(conf.Log.FileMaxSize), - MaxBackups: int(conf.Log.FileMaxBackups), - MaxAge: int(conf.Log.FileMaxAge), - Compress: conf.Log.FileCompress != 0, - } - 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 { ctx2, cancel := context.WithTimeout(ctx, time.Duration(conf.RequestTimeout)*time.Second) diff --git a/go.mod b/go.mod index baf57ea..ef2be14 100644 --- a/go.mod +++ b/go.mod @@ -16,7 +16,6 @@ require ( github.com/lib/pq v1.10.3 github.com/lithammer/shortuuid v2.0.3+incompatible github.com/lithammer/shortuuid/v3 v3.0.7 - github.com/natefinch/lumberjack v2.0.0+incompatible // indirect github.com/onsi/gomega v1.16.0 github.com/prometheus/client_golang v1.11.0 github.com/stretchr/testify v1.7.0 diff --git a/main.go b/main.go index 43ce895..1b088e1 100644 --- a/main.go +++ b/main.go @@ -59,7 +59,7 @@ func main() { } config.MustLoadConfig(*confFile) if *isDebug { - config.Config.Log.Level = "debug" + config.Config.LogLevel = "debug" } if *isReset { dtmsvr.PopulateDB(false)