diff --git a/client/dtmcli/dtmimp/vars.go b/client/dtmcli/dtmimp/vars.go index 5ea4b02..b0f70da 100644 --- a/client/dtmcli/dtmimp/vars.go +++ b/client/dtmcli/dtmimp/vars.go @@ -42,13 +42,14 @@ var BarrierTableName = "dtm_barrier.barrier" // AddRestyMiddlewares will add the middlewares used by dtm func AddRestyMiddlewares(client *resty.Client) { client.OnBeforeRequest(func(c *resty.Client, r *resty.Request) error { - logger.Debugf("requesting: %s %s %s resolved: %s", r.Method, r.URL, MustMarshalString(r.Body), r.URL) + old := r.URL r.URL = MayReplaceLocalhost(r.URL) ms := dtmdriver.Middlewares.HTTP var err error for i := 0; i < len(ms) && err == nil; i++ { err = ms[i](c, r) } + logger.Debugf("requesting: %s %s %s resolved: %s err: %v", r.Method, old, MustMarshalString(r.Body), r.URL, err) return err }) client.OnAfterResponse(func(c *resty.Client, resp *resty.Response) error { diff --git a/client/dtmcli/logger/log.go b/client/dtmcli/logger/log.go deleted file mode 100644 index c53530c..0000000 --- a/client/dtmcli/logger/log.go +++ /dev/null @@ -1,135 +0,0 @@ -package logger - -import ( - "encoding/json" - "fmt" - "log" - "net/url" - "os" - "runtime/debug" - "strings" - - "github.com/natefinch/lumberjack" - "go.uber.org/zap" - "go.uber.org/zap/zapcore" -) - -//var logger *zap.SugaredLogger = nil - -var logger Logger - -const ( - // StdErr is the default configuration for log output. - StdErr = "stderr" - // StdOut configuration for log output - StdOut = "stdout" -) - -func init() { - InitLog(os.Getenv("LOG_LEVEL")) -} - -// Logger logger interface -type Logger interface { - Debugf(format string, args ...interface{}) - Infof(format string, args ...interface{}) - Warnf(format string, args ...interface{}) - Errorf(format string, args ...interface{}) -} - -// WithLogger replaces default logger -func WithLogger(log Logger) { - logger = log -} - -// InitLog is an initialization for a logger -// level can be: debug info warn error -func InitLog(level string) { - InitLog2(level, StdOut, 0, "") -} - -// InitLog2 specify advanced log config -func InitLog2(level string, outputs string, logRotationEnable int64, logRotateConfigJSON string) { - outputPaths := strings.Split(outputs, ",") - for i, v := range outputPaths { - if logRotationEnable != 0 && v != StdErr && v != StdOut { - outputPaths[i] = fmt.Sprintf("lumberjack://%s", v) - } - } - - if logRotationEnable != 0 { - setupLogRotation(logRotateConfigJSON) - } - - config := loadConfig(level) - config.OutputPaths = outputPaths - p, err := config.Build(zap.AddCallerSkip(1)) - FatalIfError(err) - logger = p.Sugar() -} - -type lumberjackSink struct { - lumberjack.Logger -} - -func (*lumberjackSink) Sync() error { - return nil -} - -// setupLogRotation initializes log rotation for a single file path target. -func setupLogRotation(logRotateConfigJSON string) { - err := zap.RegisterSink("lumberjack", func(u *url.URL) (zap.Sink, error) { - var conf lumberjackSink - err := json.Unmarshal([]byte(logRotateConfigJSON), &conf) - FatalfIf(err != nil, "bad config LogRotateConfigJSON: %v", err) - conf.Filename = u.Host + u.Path - return &conf, nil - }) - FatalIfError(err) -} - -func loadConfig(logLevel string) zap.Config { - config := zap.NewProductionConfig() - err := config.Level.UnmarshalText([]byte(logLevel)) - FatalIfError(err) - config.EncoderConfig.EncodeTime = zapcore.ISO8601TimeEncoder - if os.Getenv("DTM_DEBUG") != "" { - config.Encoding = "console" - config.EncoderConfig.EncodeLevel = zapcore.CapitalColorLevelEncoder - } - return config -} - -// Debugf log to level debug -func Debugf(fmt string, args ...interface{}) { - logger.Debugf(fmt, args...) -} - -// Infof log to level info -func Infof(fmt string, args ...interface{}) { - logger.Infof(fmt, args...) -} - -// Warnf log to level warn -func Warnf(fmt string, args ...interface{}) { - logger.Warnf(fmt, args...) -} - -// Errorf log to level error -func Errorf(fmt string, args ...interface{}) { - logger.Errorf(fmt, args...) -} - -// FatalfIf log to level error -func FatalfIf(cond bool, fmt string, args ...interface{}) { - if !cond { - return - } - debug.PrintStack() - log.Fatalf(fmt, args...) -} - -// FatalIfError if err is not nil, then log to level fatal and call os.Exit -func FatalIfError(err error) { - FatalfIf(err != nil, "fatal error: %v", err) -} diff --git a/client/dtmcli/logger/logger_test.go b/client/dtmcli/logger/logger_test.go deleted file mode 100644 index e69ef02..0000000 --- a/client/dtmcli/logger/logger_test.go +++ /dev/null @@ -1,46 +0,0 @@ -package logger - -import ( - "os" - "testing" - - "github.com/stretchr/testify/assert" - "go.uber.org/zap" -) - -func TestInitLog(t *testing.T) { - os.Setenv("DTM_DEBUG", "1") - InitLog("debug") - Debugf("a debug msg") - Infof("a info msg") - Warnf("a warn msg") - Errorf("a error msg") - FatalfIf(false, "nothing") - FatalIfError(nil) - - InitLog2("debug", "test.log,stderr", 0, "") - Debugf("a debug msg to console and file") - - InitLog2("debug", "test2.log,/tmp/dtm-test1.log,/tmp/dtm-test.log,stdout,stderr", 1, - "{\"maxsize\": 1, \"maxage\": 1, \"maxbackups\": 1, \"compress\": false}") - Debugf("a debug msg to /tmp/dtm-test.log and test2.log and stdout and stderr") - - // _ = os.Remove("test.log") -} - -func TestWithLogger(t *testing.T) { - logger := zap.NewExample().Sugar() - WithLogger(logger) - Debugf("a debug msg") - Infof("a info msg") - Warnf("a warn msg") - Errorf("a error msg") - FatalfIf(false, "nothing") - FatalIfError(nil) -} - -func TestCover(t *testing.T) { - sin := lumberjackSink{} - err := sin.Sync() - assert.Nil(t, err) -} diff --git a/dtmsvr/microservices/drivers.go b/dtmsvr/microservices/drivers.go index 34b8536..053f99f 100644 --- a/dtmsvr/microservices/drivers.go +++ b/dtmsvr/microservices/drivers.go @@ -2,6 +2,7 @@ package microservices import ( // load the microserver drivers + _ "github.com/dtm-labs/dtmdriver-dapr" _ "github.com/dtm-labs/dtmdriver-gozero" _ "github.com/dtm-labs/dtmdriver-kratos" _ "github.com/dtm-labs/dtmdriver-polaris" diff --git a/go.mod b/go.mod index 67cd469..c3d8966 100644 --- a/go.mod +++ b/go.mod @@ -4,12 +4,12 @@ go 1.16 require ( bou.ke/monkey v1.0.2 - github.com/BurntSushi/toml v0.4.1 // indirect github.com/dtm-labs/dtmdriver v0.0.6 github.com/dtm-labs/dtmdriver-gozero v0.0.7 github.com/dtm-labs/dtmdriver-kratos v0.0.9 github.com/dtm-labs/dtmdriver-polaris v0.0.5 github.com/dtm-labs/dtmdriver-springcloud v1.2.3 + github.com/dtm-labs/logger v0.0.1 // indirect github.com/gin-gonic/gin v1.7.7 github.com/go-playground/validator/v10 v10.11.0 // indirect github.com/go-redis/redis/v8 v8.11.5 diff --git a/go.sum b/go.sum index 677e4e9..9d385bb 100644 --- a/go.sum +++ b/go.sum @@ -48,6 +48,7 @@ github.com/Azure/go-autorest/tracing v0.6.0/go.mod h1:+vhtPC754Xsa23ID7GlGsrdKBp github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU= github.com/BurntSushi/toml v0.4.1 h1:GaI7EiDXDRfa8VshkTj7Fym7ha+y8/XxIgD2okUIjLw= github.com/BurntSushi/toml v0.4.1/go.mod h1:CxXYINrC8qIiEnFrOxCa7Jy5BFHlXnUU2pbicEuybxQ= +github.com/BurntSushi/toml v1.2.0/go.mod h1:CxXYINrC8qIiEnFrOxCa7Jy5BFHlXnUU2pbicEuybxQ= github.com/BurntSushi/xgb v0.0.0-20160522181843-27f122750802/go.mod h1:IVnqGOEym/WlBOVXweHU+Q+/VP0lqqI8lqeDx9IjBqo= github.com/ClickHouse/clickhouse-go v1.5.1/go.mod h1:EaI/sW7Azgz9UATzd5ZdZHRUhHgv5+JMS9NSr2smCJI= github.com/ClickHouse/clickhouse-go v1.5.4/go.mod h1:EaI/sW7Azgz9UATzd5ZdZHRUhHgv5+JMS9NSr2smCJI= @@ -152,6 +153,8 @@ github.com/dtm-labs/dtmdriver-polaris v0.0.5 h1:vlM3mvkgYv6GkgK49Jx1ESvYTi2Os5Od github.com/dtm-labs/dtmdriver-polaris v0.0.5/go.mod h1:FYF5ot7LCri5oA0qyvGzDRBZiMw08WlxjmFgzFQhIvo= github.com/dtm-labs/dtmdriver-springcloud v1.2.3 h1:AutSnngy+inr0PYoAT6pY/4Cw4aUZNq1pX7VN4j7tD8= github.com/dtm-labs/dtmdriver-springcloud v1.2.3/go.mod h1:sswcxoTofararER63EhBu9O0Ab55w20fYp1KsE1HXww= +github.com/dtm-labs/logger v0.0.1 h1:187UPkYviyOXelmkbew+Q94mg/BFjxJEsHfyHawu5YQ= +github.com/dtm-labs/logger v0.0.1/go.mod h1:0woMQZ6ljx9wZIl7hW8cuV2PRQmwEKxhqYtab7zVNWg= github.com/dustin/go-humanize v1.0.0/go.mod h1:HtrtbFcZ19U5GC7JDqmcUSB87Iq5E25KnS6fMYU6eOk= github.com/eapache/go-resiliency v1.2.0/go.mod h1:kFI+JgMyC7bLPUVY133qvEBtVayf5mFgVsvEsIPBvNs= github.com/eapache/go-xerial-snappy v0.0.0-20180814174437-776d5712da21/go.mod h1:+020luEh2TKB4/GOp8oxxtq0Daoen/Cii55CzbTV6DU=