diff --git a/pkg/client/client.go b/pkg/client/client.go index be1be2725..a16f70144 100644 --- a/pkg/client/client.go +++ b/pkg/client/client.go @@ -27,6 +27,7 @@ import ( "github.com/seata/seata-go/pkg/remoting/getty" "github.com/seata/seata-go/pkg/remoting/processor/client" "github.com/seata/seata-go/pkg/rm/tcc" + "github.com/seata/seata-go/pkg/util/log" ) // Init seata client client @@ -38,6 +39,7 @@ func Init() { func InitPath(configFilePath string) { cfg := LoadPath(configFilePath) + initLog(cfg) initRmClient(cfg) initTmClient(cfg) } @@ -45,6 +47,7 @@ func InitPath(configFilePath string) { var ( onceInitTmClient sync.Once onceInitRmClient sync.Once + onceInitLog sync.Once ) // InitTmClient init client tm client @@ -69,3 +72,10 @@ func initRmClient(cfg *Config) { at.InitAT() }) } + +// initLog init log +func initLog(cfg *Config) { + onceInitLog.Do(func() { + log.Init() + }) +} diff --git a/pkg/datasource/sql/undo/builder/mysql_delete_undo_log_builder_test.go b/pkg/datasource/sql/undo/builder/mysql_delete_undo_log_builder_test.go index 5f4098402..c05ab1275 100644 --- a/pkg/datasource/sql/undo/builder/mysql_delete_undo_log_builder_test.go +++ b/pkg/datasource/sql/undo/builder/mysql_delete_undo_log_builder_test.go @@ -21,10 +21,13 @@ import ( "database/sql/driver" "testing" + "github.com/seata/seata-go/pkg/util/log" + "github.com/stretchr/testify/assert" ) func TestBuildDeleteBeforeImageSQL(t *testing.T) { + log.Init() var ( builder = MySQLDeleteUndoLogBuilder{} ) diff --git a/pkg/remoting/getty/getty_client_test.go b/pkg/remoting/getty/getty_client_test.go index 89ae2dd1b..5e6ff7e90 100644 --- a/pkg/remoting/getty/getty_client_test.go +++ b/pkg/remoting/getty/getty_client_test.go @@ -24,6 +24,7 @@ import ( "github.com/seata/seata-go/pkg/protocol/codec" "github.com/seata/seata-go/pkg/protocol/message" + "github.com/seata/seata-go/pkg/util/log" "github.com/agiledragon/gomonkey" getty "github.com/apache/dubbo-getty" @@ -89,6 +90,7 @@ func TestGettyRemotingClient_SendAsyncRequest(t *testing.T) { // Test_syncCallback unit test for syncCallback function func Test_syncCallback(t *testing.T) { codec.Init() + log.Init() tests := []struct { name string respMsg *message.MessageFuture diff --git a/pkg/remoting/processor/client/client_heart_beat_processor_test.go b/pkg/remoting/processor/client/client_heart_beat_processor_test.go index 26f103bb8..38c67498f 100644 --- a/pkg/remoting/processor/client/client_heart_beat_processor_test.go +++ b/pkg/remoting/processor/client/client_heart_beat_processor_test.go @@ -23,9 +23,11 @@ import ( "github.com/seata/seata-go/pkg/protocol/codec" "github.com/seata/seata-go/pkg/protocol/message" + "github.com/seata/seata-go/pkg/util/log" ) func TestClientHeartBeatProcessor(t *testing.T) { + log.Init() // testcases tests := []struct { name string // testcase name diff --git a/pkg/rm/tcc/fence/fence_api_test.go b/pkg/rm/tcc/fence/fence_api_test.go index ad75e62ce..7e8e0494c 100644 --- a/pkg/rm/tcc/fence/fence_api_test.go +++ b/pkg/rm/tcc/fence/fence_api_test.go @@ -23,16 +23,17 @@ import ( "fmt" "testing" + "github.com/seata/seata-go/pkg/rm/tcc/fence/enum" + "github.com/seata/seata-go/pkg/tm" "github.com/seata/seata-go/pkg/util/errors" + "github.com/seata/seata-go/pkg/util/log" "github.com/DATA-DOG/go-sqlmock" "github.com/stretchr/testify/assert" - - "github.com/seata/seata-go/pkg/rm/tcc/fence/enum" - "github.com/seata/seata-go/pkg/tm" ) func TestWithFence(t *testing.T) { + log.Init() tests := []struct { xid string txName string diff --git a/pkg/rm/tcc/tcc_service_test.go b/pkg/rm/tcc/tcc_service_test.go index 1872591b3..3ccb93694 100644 --- a/pkg/rm/tcc/tcc_service_test.go +++ b/pkg/rm/tcc/tcc_service_test.go @@ -46,6 +46,7 @@ var ( ) func InitMock() { + log.Init() var ( registerResource = func(_ *TCCServiceProxy) error { return nil diff --git a/pkg/tm/global_transaction_test.go b/pkg/tm/global_transaction_test.go index 01cdc68e3..dba76d0cc 100644 --- a/pkg/tm/global_transaction_test.go +++ b/pkg/tm/global_transaction_test.go @@ -23,6 +23,8 @@ import ( "testing" "time" + "github.com/seata/seata-go/pkg/util/log" + "github.com/agiledragon/gomonkey" "github.com/pkg/errors" "github.com/stretchr/testify/assert" @@ -32,6 +34,7 @@ import ( ) func TestBegin(t *testing.T) { + log.Init() InitTm(TmConfig{ CommitRetryCount: 5, RollbackRetryCount: 5, diff --git a/pkg/util/log/logging.go b/pkg/util/log/logging.go index 9c0661bea..29e44eda3 100644 --- a/pkg/util/log/logging.go +++ b/pkg/util/log/logging.go @@ -140,14 +140,14 @@ func encodeCaller(caller zapcore.EntryCaller, enc zapcore.PrimitiveArrayEncoder) enc.AppendString(fmt.Sprintf("%-45s", caller.TrimmedPath())) } -func init() { +func Init() { zapLoggerConfig.EncoderConfig = zapLoggerEncoderConfig zapLogger, _ = zapLoggerConfig.Build(zap.AddCallerSkip(1)) log = zapLogger.Sugar() getty.SetLogger(log) } -func Init(logPath string, level LogLevel) { +func InitWithOption(logPath string, level LogLevel) { lumberJackLogger := &lumberjack.Logger{ Filename: logPath, MaxSize: 10, @@ -182,60 +182,96 @@ func GetLogger() Logger { // Debug ... func Debug(v ...interface{}) { + if log == nil { + return + } log.Debug(v...) } // Debugf ... func Debugf(format string, v ...interface{}) { + if log == nil { + return + } log.Debugf(format, v...) } // Info ... func Info(v ...interface{}) { + if log == nil { + return + } log.Info(v...) } // Infof ... func Infof(format string, v ...interface{}) { + if log == nil { + return + } log.Infof(format, v...) } // Warn ... func Warn(v ...interface{}) { + if log == nil { + return + } log.Warn(v...) } // Warnf ... func Warnf(format string, v ...interface{}) { + if log == nil { + return + } log.Warnf(format, v...) } // Error ... func Error(v ...interface{}) { + if log == nil { + return + } log.Error(v...) } // Errorf ... func Errorf(format string, v ...interface{}) { + if log == nil { + return + } log.Errorf(format, v...) } // Panic ... func Panic(v ...interface{}) { + if log == nil { + return + } log.Panic(v...) } // Panicf ... func Panicf(format string, v ...interface{}) { + if log == nil { + return + } log.Panicf(format, v...) } // Fatal ... func Fatal(v ...interface{}) { + if log == nil { + return + } log.Fatal(v...) } // Fatalf ... func Fatalf(format string, v ...interface{}) { + if log == nil { + return + } log.Fatalf(format, v...) } diff --git a/sample/at/non_transaction/main.go b/sample/at/non_transaction/main.go index 92628fdbf..510221534 100644 --- a/sample/at/non_transaction/main.go +++ b/sample/at/non_transaction/main.go @@ -43,7 +43,7 @@ var ( ) func main() { - client.Init() + client.InitPath("./testdata/conf/seatago.yml") initService() insertId := insertData() diff --git a/sample/at/non_transaction/service.go b/sample/at/non_transaction/service.go index 9ea0da774..6e5ca6542 100644 --- a/sample/at/non_transaction/service.go +++ b/sample/at/non_transaction/service.go @@ -29,7 +29,7 @@ var ( func initService() { var err error - db, err = sql.Open(sql2.SeataATMySQLDriver, "root:123456@tcp(127.0.0.1:3306)/seata_client?multiStatements=true&interpolateParams=true") + db, err = sql.Open(sql2.SeataATMySQLDriver, "root:12345678@tcp(127.0.0.1:3306)/seata_client?multiStatements=true&interpolateParams=true") if err != nil { panic("init service error") }