From 048abd90e2bb9fa893cb53adf6b296d48a8505c0 Mon Sep 17 00:00:00 2001 From: yedf2 <120050102@qq.com> Date: Fri, 24 Dec 2021 20:11:02 +0800 Subject: [PATCH] remove gin log --- app/main.go | 2 +- common/config.go | 1 + common/utils.go | 12 ++++++------ conf.sample.yml | 2 ++ dtmcli/logger/log.go | 13 +++++++------ dtmcli/logger/logger_test.go | 2 +- dtmsvr/svr.go | 6 +++--- 7 files changed, 21 insertions(+), 17 deletions(-) diff --git a/app/main.go b/app/main.go index 68a8d57..edd4b9a 100644 --- a/app/main.go +++ b/app/main.go @@ -50,7 +50,7 @@ func main() { fmt.Printf("version: %s commit: %s built at: %s\n", Version, Commit, Date) return } - logger.Debugf("starting dtm....") + logger.Infof("starting dtm....") common.MustLoadConfig() if common.Config.ExamplesDB.Driver != "" { dtmcli.SetCurrentDBType(common.Config.ExamplesDB.Driver) diff --git a/common/config.go b/common/config.go index 940dd1a..6044305 100644 --- a/common/config.go +++ b/common/config.go @@ -61,6 +61,7 @@ type configType struct { GrpcPort int64 `yaml:"GrpcPort" default:"36790"` MicroService MicroService `yaml:"MicroService"` UpdateBranchSync int64 `yaml:"UpdateBranchSync"` + LogLevel string `yaml:"LogLevel" default:"info"` ExamplesDB dtmcli.DBConf `yaml:"ExamplesDB"` } diff --git a/common/utils.go b/common/utils.go index c121733..a712f6d 100644 --- a/common/utils.go +++ b/common/utils.go @@ -26,7 +26,8 @@ import ( // GetGinApp init and return gin func GetGinApp() *gin.Engine { gin.SetMode(gin.ReleaseMode) - app := gin.Default() + app := gin.New() + app.Use(gin.Recovery()) app.Use(func(c *gin.Context) { body := "" if c.Request.Body != nil { @@ -37,11 +38,8 @@ func GetGinApp() *gin.Engine { c.Request.Body = ioutil.NopCloser(bytes.NewBuffer(rb)) } } - began := time.Now() logger.Debugf("begin %s %s query: %s body: %s", c.Request.Method, c.FullPath(), c.Request.URL.RawQuery, body) c.Next() - logger.Debugf("used %d ms %s %s query: %s body: %s", time.Since(began).Milliseconds(), c.Request.Method, c.FullPath(), c.Request.URL.RawQuery, body) - }) app.Any("/api/ping", func(c *gin.Context) { c.JSON(200, map[string]interface{}{"msg": "pong"}) }) return app @@ -50,6 +48,7 @@ func GetGinApp() *gin.Engine { // WrapHandler name is clear func WrapHandler(fn func(*gin.Context) (interface{}, error)) gin.HandlerFunc { return func(c *gin.Context) { + began := time.Now() r, err := func() (r interface{}, rerr error) { defer dtmimp.P2E(&rerr) return fn(c) @@ -60,11 +59,12 @@ func WrapHandler(fn func(*gin.Context) (interface{}, error)) gin.HandlerFunc { } else if err == nil { b, err = json.Marshal(r) } + if err != nil { - logger.Debugf("status: 500, code: 500 message: %s", err.Error()) + logger.Errorf("%2dms 500 %s %s %s %s", time.Since(began).Milliseconds(), err.Error(), c.Request.Method, c.Request.RequestURI, string(b)) c.JSON(500, map[string]interface{}{"code": 500, "message": err.Error()}) } else { - logger.Debugf("status: 200, content: %s", string(b)) + logger.Infof("%2dms 200 %s %s %s", time.Since(began).Milliseconds(), c.Request.Method, c.Request.RequestURI, string(b)) c.Status(200) c.Writer.Header().Add("Content-Type", "application/json") _, err = c.Writer.Write(b) diff --git a/conf.sample.yml b/conf.sample.yml index 3d698c0..cc7f447 100644 --- a/conf.sample.yml +++ b/conf.sample.yml @@ -39,6 +39,8 @@ Store: # specify which engine to store trans status # TimeoutToFail: 35 # timeout for XA, TCC to fail. saga's timeout default to infinite, which can be overwritten in saga options # RetryInterval: 10 # the subtrans branch will be retried after this interval +# LogLevel: 'info' # default: info. can be debug|info|warn|error + ### dtm can run examples, and examples will use following config to connect db ExamplesDB: Driver: 'mysql' diff --git a/dtmcli/logger/log.go b/dtmcli/logger/log.go index a1ff613..7d3650a 100644 --- a/dtmcli/logger/log.go +++ b/dtmcli/logger/log.go @@ -11,21 +11,22 @@ import ( var logger *zap.SugaredLogger = nil func init() { - InitLog() + InitLog("info") } // InitLog is a initialization for a logger -func InitLog() { +// 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 } p, err := config.Build(zap.AddCallerSkip(1)) - if err != nil { - log.Fatal("create logger failed: ", err) - } + FatalIfError(err) logger = p.Sugar() } @@ -54,7 +55,7 @@ func FatalfIf(cond bool, fmt string, args ...interface{}) { if !cond { return } - logger.Fatalf(fmt, args...) + log.Fatalf(fmt, args...) } // FatalIfError if err is not nil, then log to level fatal and call os.Exit diff --git a/dtmcli/logger/logger_test.go b/dtmcli/logger/logger_test.go index 159e381..669a39f 100644 --- a/dtmcli/logger/logger_test.go +++ b/dtmcli/logger/logger_test.go @@ -7,7 +7,7 @@ import ( func TestInitLog(t *testing.T) { os.Setenv("DTM_DEBUG", "1") - InitLog() + InitLog("debug") Debugf("a debug msg") Infof("a info msg") Warnf("a warn msg") diff --git a/dtmsvr/svr.go b/dtmsvr/svr.go index 283c442..2aa8950 100644 --- a/dtmsvr/svr.go +++ b/dtmsvr/svr.go @@ -22,11 +22,11 @@ import ( // StartSvr StartSvr func StartSvr() { - logger.Debugf("start dtmsvr") + logger.Infof("start dtmsvr") app := common.GetGinApp() app = httpMetrics(app) addRoute(app) - logger.Debugf("dtmsvr listen at: %d", config.HttpPort) + logger.Infof("dtmsvr listen at: %d", config.HttpPort) go app.Run(fmt.Sprintf(":%d", config.HttpPort)) lis, err := net.Listen("tcp", fmt.Sprintf(":%d", config.GrpcPort)) @@ -36,7 +36,7 @@ func StartSvr() { grpc.UnaryServerInterceptor(grpcMetrics), grpc.UnaryServerInterceptor(dtmgimp.GrpcServerLog)), )) dtmgpb.RegisterDtmServer(s, &dtmServer{}) - logger.Debugf("grpc listening at %v", lis.Addr()) + logger.Infof("grpc listening at %v", lis.Addr()) go func() { err := s.Serve(lis) logger.FatalIfError(err)