Skip to content

Commit

Permalink
optimize: add log init (apache#408)
Browse files Browse the repository at this point in the history
  • Loading branch information
Code-Fight authored and georgehao committed Feb 3, 2023
1 parent 78c4551 commit 116950d
Show file tree
Hide file tree
Showing 10 changed files with 65 additions and 7 deletions.
10 changes: 10 additions & 0 deletions pkg/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -38,13 +39,15 @@ func Init() {
func InitPath(configFilePath string) {
cfg := LoadPath(configFilePath)

initLog(cfg)
initRmClient(cfg)
initTmClient(cfg)
}

var (
onceInitTmClient sync.Once
onceInitRmClient sync.Once
onceInitLog sync.Once
)

// InitTmClient init client tm client
Expand All @@ -69,3 +72,10 @@ func initRmClient(cfg *Config) {
at.InitAT()
})
}

// initLog init log
func initLog(cfg *Config) {
onceInitLog.Do(func() {
log.Init()
})
}
Original file line number Diff line number Diff line change
Expand Up @@ -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{}
)
Expand Down
2 changes: 2 additions & 0 deletions pkg/remoting/getty/getty_client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
7 changes: 4 additions & 3 deletions pkg/rm/tcc/fence/fence_api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions pkg/rm/tcc/tcc_service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ var (
)

func InitMock() {
log.Init()
var (
registerResource = func(_ *TCCServiceProxy) error {
return nil
Expand Down
3 changes: 3 additions & 0 deletions pkg/tm/global_transaction_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -32,6 +34,7 @@ import (
)

func TestBegin(t *testing.T) {
log.Init()
InitTm(TmConfig{
CommitRetryCount: 5,
RollbackRetryCount: 5,
Expand Down
40 changes: 38 additions & 2 deletions pkg/util/log/logging.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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...)
}
2 changes: 1 addition & 1 deletion sample/at/non_transaction/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ var (
)

func main() {
client.Init()
client.InitPath("./testdata/conf/seatago.yml")
initService()

insertId := insertData()
Expand Down
2 changes: 1 addition & 1 deletion sample/at/non_transaction/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}
Expand Down

0 comments on commit 116950d

Please sign in to comment.