diff --git a/controller/ws/query/ws_test.go b/controller/ws/query/ws_test.go index 9bd0de8..baa99ef 100644 --- a/controller/ws/query/ws_test.go +++ b/controller/ws/query/ws_test.go @@ -33,8 +33,8 @@ func TestMain(m *testing.M) { viper.Set("logLevel", "trace") viper.Set("uploadKeeper.enable", false) config.Init() - db.PrepareConnection() log.ConfigLog() + db.PrepareConnection() gin.SetMode(gin.ReleaseMode) router = gin.New() controllers := controller.GetControllers() diff --git a/controller/ws/schemaless/schemaless_test.go b/controller/ws/schemaless/schemaless_test.go index a6f4fe9..c2f4e25 100644 --- a/controller/ws/schemaless/schemaless_test.go +++ b/controller/ws/schemaless/schemaless_test.go @@ -31,8 +31,8 @@ func TestMain(m *testing.M) { viper.Set("logLevel", "trace") viper.Set("uploadKeeper.enable", false) config.Init() - db.PrepareConnection() log.ConfigLog() + db.PrepareConnection() gin.SetMode(gin.ReleaseMode) router = gin.New() controllers := controller.GetControllers() diff --git a/controller/ws/stmt/stmt_test.go b/controller/ws/stmt/stmt_test.go index e2b53b2..445a2fb 100644 --- a/controller/ws/stmt/stmt_test.go +++ b/controller/ws/stmt/stmt_test.go @@ -34,8 +34,8 @@ func TestMain(m *testing.M) { viper.Set("logLevel", "trace") viper.Set("uploadKeeper.enable", false) config.Init() - db.PrepareConnection() log.ConfigLog() + db.PrepareConnection() gin.SetMode(gin.ReleaseMode) router = gin.New() controllers := controller.GetControllers() diff --git a/controller/ws/tmq/tmq_test.go b/controller/ws/tmq/tmq_test.go index 1a7c208..4e50128 100644 --- a/controller/ws/tmq/tmq_test.go +++ b/controller/ws/tmq/tmq_test.go @@ -42,8 +42,8 @@ func TestMain(m *testing.M) { viper.Set("logLevel", "trace") viper.Set("uploadKeeper.enable", false) config.Init() - db.PrepareConnection() log.ConfigLog() + db.PrepareConnection() gin.SetMode(gin.ReleaseMode) router = gin.New() controllers := controller.GetControllers() diff --git a/controller/ws/ws/ws_test.go b/controller/ws/ws/ws_test.go index 54023fb..cf67435 100644 --- a/controller/ws/ws/ws_test.go +++ b/controller/ws/ws/ws_test.go @@ -31,8 +31,8 @@ func TestMain(m *testing.M) { viper.Set("logLevel", "trace") viper.Set("uploadKeeper.enable", false) config.Init() - db.PrepareConnection() log.ConfigLog() + db.PrepareConnection() gin.SetMode(gin.ReleaseMode) router = gin.New() controllers := controller.GetControllers() diff --git a/controller/ws/wstool/error_test.go b/controller/ws/wstool/error_test.go index 9cd7564..fd85f96 100644 --- a/controller/ws/wstool/error_test.go +++ b/controller/ws/wstool/error_test.go @@ -11,9 +11,9 @@ import ( "github.com/gin-gonic/gin" "github.com/gorilla/websocket" - "github.com/sirupsen/logrus" "github.com/stretchr/testify/assert" tErrors "github.com/taosdata/driver-go/v3/errors" + "github.com/taosdata/taosadapter/v3/log" "github.com/taosdata/taosadapter/v3/tools/melody" ) @@ -27,7 +27,7 @@ func TestWSError(t *testing.T) { ErrStr: "test error", } commonErr := errors.New("test common error") - logger := logrus.New().WithField("test", "TestWSError") + logger := log.GetLogger("test").WithField("test", "TestWSError") m.HandleMessage(func(session *melody.Session, data []byte) { switch data[0] { case '1': diff --git a/controller/ws/wstool/log_test.go b/controller/ws/wstool/log_test.go index 6b0ccdb..09d527d 100644 --- a/controller/ws/wstool/log_test.go +++ b/controller/ws/wstool/log_test.go @@ -7,8 +7,8 @@ import ( "time" "github.com/gorilla/websocket" - "github.com/sirupsen/logrus" "github.com/stretchr/testify/assert" + "github.com/taosdata/taosadapter/v3/log" "github.com/taosdata/taosadapter/v3/tools/melody" ) @@ -21,7 +21,7 @@ func TestGetDuration(t *testing.T) { } func TestGetLogger(t *testing.T) { - logger := logrus.New() + logger := log.GetLogger("test") session := &melody.Session{} session.Set("logger", logger.WithField("test_field", "test_value")) entry := GetLogger(session) @@ -29,7 +29,7 @@ func TestGetLogger(t *testing.T) { } func TestLogWSError(t *testing.T) { - logger := logrus.New() + logger := log.GetLogger("test") session := &melody.Session{} session.Set("logger", logger.WithField("test_field", "test_value")) LogWSError(session, nil) diff --git a/controller/ws/wstool/main_test.go b/controller/ws/wstool/main_test.go new file mode 100644 index 0000000..5fc3352 --- /dev/null +++ b/controller/ws/wstool/main_test.go @@ -0,0 +1,17 @@ +package wstool + +import ( + "testing" + + "github.com/spf13/viper" + "github.com/taosdata/taosadapter/v3/config" + "github.com/taosdata/taosadapter/v3/log" +) + +func TestMain(m *testing.M) { + viper.Set("logLevel", "trace") + viper.Set("uploadKeeper.enable", false) + config.Init() + log.ConfigLog() + m.Run() +} diff --git a/controller/ws/wstool/resp_test.go b/controller/ws/wstool/resp_test.go index 1680b1e..60e1eca 100644 --- a/controller/ws/wstool/resp_test.go +++ b/controller/ws/wstool/resp_test.go @@ -8,8 +8,8 @@ import ( "github.com/gin-gonic/gin" "github.com/gorilla/websocket" - "github.com/sirupsen/logrus" "github.com/stretchr/testify/assert" + "github.com/taosdata/taosadapter/v3/log" "github.com/taosdata/taosadapter/v3/tools/melody" ) @@ -23,7 +23,7 @@ func TestWSWriteJson(t *testing.T) { Version: "1.0.0", } m.HandleMessage(func(session *melody.Session, _ []byte) { - logger := logrus.New().WithField("test", "TestWSWriteJson") + logger := log.GetLogger("test").WithField("test", "TestWSWriteJson") session.Set("logger", logger) WSWriteJson(session, logger, data) }) diff --git a/db/async/row_test.go b/db/async/row_test.go index 6578900..548a286 100644 --- a/db/async/row_test.go +++ b/db/async/row_test.go @@ -98,7 +98,7 @@ func TestAsync_TaosExec(t *testing.T) { wantErr: true, }, } - var logger = logrus.New().WithField("test", "async_test") + var logger = log.GetLogger("test").WithField("test", "async_test") for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { a := &Async{ diff --git a/db/syncinterface/wrapper_test.go b/db/syncinterface/wrapper_test.go index c37a3b5..a22ca7e 100644 --- a/db/syncinterface/wrapper_test.go +++ b/db/syncinterface/wrapper_test.go @@ -26,6 +26,7 @@ const isDebug = true func TestMain(m *testing.M) { config.Init() + log.ConfigLog() _ = log.SetLevel("trace") db.PrepareConnection() m.Run() diff --git a/db/tool/createdb_test.go b/db/tool/createdb_test.go index 6ff2002..f246576 100644 --- a/db/tool/createdb_test.go +++ b/db/tool/createdb_test.go @@ -5,18 +5,20 @@ import ( "testing" "unsafe" - "github.com/sirupsen/logrus" "github.com/spf13/viper" "github.com/stretchr/testify/assert" tErrors "github.com/taosdata/driver-go/v3/errors" "github.com/taosdata/driver-go/v3/wrapper" "github.com/taosdata/taosadapter/v3/config" "github.com/taosdata/taosadapter/v3/db" + "github.com/taosdata/taosadapter/v3/log" ) func TestMain(m *testing.M) { viper.Set("smlAutoCreateDB", true) + viper.Set("logLevel", "trace") config.Init() + log.ConfigLog() db.PrepareConnection() m.Run() viper.Set("smlAutoCreateDB", false) @@ -26,7 +28,6 @@ func TestMain(m *testing.M) { // @date: 2021/12/14 15:05 // @description: test creat database with connection func TestCreateDBWithConnection(t *testing.T) { - db.PrepareConnection() conn, err := wrapper.TaosConnect("", "root", "taosdata", "", 0) if err != nil { t.Error(err) @@ -52,7 +53,7 @@ func TestCreateDBWithConnection(t *testing.T) { } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { - if err := CreateDBWithConnection(tt.args.connection, logrus.New().WithField("test", "TestCreateDBWithConnection"), false, tt.args.db, 0); (err != nil) != tt.wantErr { + if err := CreateDBWithConnection(tt.args.connection, log.GetLogger("test").WithField("test", "TestCreateDBWithConnection"), false, tt.args.db, 0); (err != nil) != tt.wantErr { t.Errorf("CreateDBWithConnection() error = %v, wantErr %v", err, tt.wantErr) } code := wrapper.TaosSelectDB(tt.args.connection, tt.args.db) @@ -107,7 +108,7 @@ func TestSchemalessSelectDB(t *testing.T) { return } wrapper.TaosFreeResult(result) - if err := SchemalessSelectDB(tt.args.taosConnect, logrus.New().WithField("test", "TestSchemalessSelectDB"), false, tt.args.db, 0); (err != nil) != tt.wantErr { + if err := SchemalessSelectDB(tt.args.taosConnect, log.GetLogger("test").WithField("test", "TestSchemalessSelectDB"), false, tt.args.db, 0); (err != nil) != tt.wantErr { t.Errorf("selectDB() error = %v, wantErr %v", err, tt.wantErr) } r := wrapper.TaosQuery(tt.args.taosConnect, fmt.Sprintf("drop database if exists %s", tt.args.db)) diff --git a/plugin/influxdb/plugin_test.go b/plugin/influxdb/plugin_test.go index 6b0ce01..2c19cdd 100644 --- a/plugin/influxdb/plugin_test.go +++ b/plugin/influxdb/plugin_test.go @@ -18,6 +18,7 @@ import ( "github.com/taosdata/driver-go/v3/wrapper" "github.com/taosdata/taosadapter/v3/config" "github.com/taosdata/taosadapter/v3/db" + "github.com/taosdata/taosadapter/v3/log" ) // @author: xftan @@ -28,8 +29,9 @@ func TestInfluxdb(t *testing.T) { rand.Seed(time.Now().UnixNano()) viper.Set("smlAutoCreateDB", true) defer viper.Set("smlAutoCreateDB", false) - config.Init() viper.Set("influxdb.enable", true) + config.Init() + log.ConfigLog() db.PrepareConnection() p := Influxdb{} router := gin.Default() diff --git a/plugin/nodeexporter/plugin_test.go b/plugin/nodeexporter/plugin_test.go index 71bb0a6..c6340d8 100644 --- a/plugin/nodeexporter/plugin_test.go +++ b/plugin/nodeexporter/plugin_test.go @@ -12,6 +12,7 @@ import ( "github.com/taosdata/driver-go/v3/af" "github.com/taosdata/taosadapter/v3/config" "github.com/taosdata/taosadapter/v3/db" + "github.com/taosdata/taosadapter/v3/log" ) var s = ` @@ -37,6 +38,7 @@ test_metric{label="value"} 1.0 1490802350000 // @description: test node-exporter plugin func TestNodeExporter_Gather(t *testing.T) { config.Init() + log.ConfigLog() db.PrepareConnection() ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { _, err := w.Write([]byte(s)) diff --git a/plugin/opentsdb/plugin_test.go b/plugin/opentsdb/plugin_test.go index 10a4d4c..2245190 100644 --- a/plugin/opentsdb/plugin_test.go +++ b/plugin/opentsdb/plugin_test.go @@ -18,6 +18,7 @@ import ( "github.com/taosdata/driver-go/v3/wrapper" "github.com/taosdata/taosadapter/v3/config" "github.com/taosdata/taosadapter/v3/db" + "github.com/taosdata/taosadapter/v3/log" ) // @author: xftan @@ -30,6 +31,7 @@ func TestOpentsdb(t *testing.T) { defer viper.Set("smlAutoCreateDB", false) config.Init() viper.Set("opentsdb.enable", true) + log.ConfigLog() db.PrepareConnection() p := Plugin{} diff --git a/plugin/opentsdbtelnet/plugin_test.go b/plugin/opentsdbtelnet/plugin_test.go index 6a8ffd1..d2c8b3f 100644 --- a/plugin/opentsdbtelnet/plugin_test.go +++ b/plugin/opentsdbtelnet/plugin_test.go @@ -15,6 +15,7 @@ import ( "github.com/taosdata/driver-go/v3/wrapper" "github.com/taosdata/taosadapter/v3/config" "github.com/taosdata/taosadapter/v3/db" + "github.com/taosdata/taosadapter/v3/log" "github.com/taosdata/taosadapter/v3/plugin/opentsdbtelnet" ) @@ -26,6 +27,7 @@ func TestPlugin(t *testing.T) { rand.Seed(time.Now().UnixNano()) p := &opentsdbtelnet.Plugin{} config.Init() + log.ConfigLog() db.PrepareConnection() viper.Set("opentsdb_telnet.enable", true) viper.Set("opentsdb_telnet.batchSize", 1) diff --git a/plugin/prometheus/plugin_test.go b/plugin/prometheus/plugin_test.go index 7b77e1f..76409f4 100644 --- a/plugin/prometheus/plugin_test.go +++ b/plugin/prometheus/plugin_test.go @@ -17,6 +17,7 @@ import ( "github.com/taosdata/driver-go/v3/wrapper" "github.com/taosdata/taosadapter/v3/config" "github.com/taosdata/taosadapter/v3/db" + "github.com/taosdata/taosadapter/v3/log" ) func TestMain(m *testing.M) { @@ -24,6 +25,7 @@ func TestMain(m *testing.M) { rand.Seed(time.Now().UnixNano()) config.Init() viper.Set("prometheus.enable", true) + log.ConfigLog() db.PrepareConnection() conn, err := wrapper.TaosConnect("", "root", "taosdata", "", 0) if err != nil { diff --git a/plugin/statsd/plugin_test.go b/plugin/statsd/plugin_test.go index 06a0380..14b3aee 100644 --- a/plugin/statsd/plugin_test.go +++ b/plugin/statsd/plugin_test.go @@ -15,6 +15,7 @@ import ( "github.com/taosdata/driver-go/v3/wrapper" "github.com/taosdata/taosadapter/v3/config" "github.com/taosdata/taosadapter/v3/db" + "github.com/taosdata/taosadapter/v3/log" ) // @author: xftan @@ -25,6 +26,7 @@ func TestStatsd(t *testing.T) { rand.Seed(time.Now().UnixNano()) p := &Plugin{} config.Init() + log.ConfigLog() db.PrepareConnection() viper.Set("statsd.gatherInterval", time.Millisecond) viper.Set("statsd.enable", true) diff --git a/schemaless/capi/influxdb_test.go b/schemaless/capi/influxdb_test.go index 85e2063..45527e6 100644 --- a/schemaless/capi/influxdb_test.go +++ b/schemaless/capi/influxdb_test.go @@ -4,9 +4,9 @@ import ( "testing" "unsafe" - "github.com/sirupsen/logrus" "github.com/taosdata/driver-go/v3/errors" "github.com/taosdata/driver-go/v3/wrapper" + "github.com/taosdata/taosadapter/v3/log" "github.com/taosdata/taosadapter/v3/schemaless/capi" ) @@ -79,7 +79,7 @@ func TestInsertInfluxdb(t *testing.T) { } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { - logger := logrus.New().WithField("test", "TestInsertInfluxdb") + logger := log.GetLogger("test").WithField("test", "TestInsertInfluxdb").WithField("name", tt.name) err := capi.InsertInfluxdb(tt.args.taosConnect, tt.args.data, tt.args.db, tt.args.precision, tt.args.ttl, 0, "", logger) if (err != nil) != tt.wantErr { t.Errorf("InsertInfluxdb() error = %v, wantErr %v", err, tt.wantErr) diff --git a/schemaless/capi/opentsdb_test.go b/schemaless/capi/opentsdb_test.go index 18a8332..c6e52e9 100644 --- a/schemaless/capi/opentsdb_test.go +++ b/schemaless/capi/opentsdb_test.go @@ -6,16 +6,20 @@ import ( "time" "unsafe" - "github.com/sirupsen/logrus" + "github.com/spf13/viper" "github.com/taosdata/driver-go/v3/errors" "github.com/taosdata/driver-go/v3/wrapper" "github.com/taosdata/taosadapter/v3/config" "github.com/taosdata/taosadapter/v3/db" + "github.com/taosdata/taosadapter/v3/log" "github.com/taosdata/taosadapter/v3/schemaless/capi" ) func TestMain(m *testing.M) { + viper.Set("logLevel", "trace") + viper.Set("uploadKeeper.enable", false) config.Init() + log.ConfigLog() db.PrepareConnection() m.Run() } @@ -94,10 +98,10 @@ func TestInsertOpentsdbTelnet(t *testing.T) { wantErr: false, }, } - logger := logrus.New().WithField("test", "TestInsertOpentsdbTelnet") + logger := log.GetLogger("test").WithField("test", "TestInsertOpentsdbTelnet") for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { - if err := capi.InsertOpentsdbTelnet(tt.args.taosConnect, []string{tt.args.data}, tt.args.db, tt.args.ttl, 0, "", logger); (err != nil) != tt.wantErr { + if err := capi.InsertOpentsdbTelnet(tt.args.taosConnect, []string{tt.args.data}, tt.args.db, tt.args.ttl, 0, "", logger.WithField("name", tt.name)); (err != nil) != tt.wantErr { t.Errorf("InsertOpentsdbTelnet() error = %v, wantErr %v", err, tt.wantErr) } }) @@ -120,7 +124,7 @@ func BenchmarkTelnet(b *testing.B) { } wrapper.TaosFreeResult(r) }() - logger := logrus.New().WithField("test", "BenchmarkTelnet") + logger := log.GetLogger("test").WithField("test", "BenchmarkTelnet") for i := 0; i < b.N; i++ { //`sys.if.bytes.out`,`host`=web01,`interface`=eth0 //t_98df8453856519710bfc2f1b5f8202cf @@ -234,7 +238,7 @@ func TestInsertOpentsdbJson(t *testing.T) { wantErr: false, }, } - logger := logrus.New().WithField("test", "TestInsertOpentsdbJson") + logger := log.GetLogger("test").WithField("test", "TestInsertOpentsdbJson") for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { if err := capi.InsertOpentsdbJson(tt.args.taosConnect, tt.args.data, tt.args.db, tt.args.ttl, 0, "", logger); (err != nil) != tt.wantErr { @@ -292,7 +296,7 @@ func TestInsertOpentsdbTelnetBatch(t *testing.T) { wantErr: false, }, } - logger := logrus.New().WithField("test", "TestInsertOpentsdbTelnetBatch") + logger := log.GetLogger("test").WithField("test", "TestInsertOpentsdbTelnetBatch") for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { if err := capi.InsertOpentsdbTelnet(tt.args.taosConnect, tt.args.data, tt.args.db, tt.args.ttl, 0, "", logger); (err != nil) != tt.wantErr {