Skip to content

Commit

Permalink
fix: update new logger logic. (#126)
Browse files Browse the repository at this point in the history
  • Loading branch information
mo3et authored Aug 19, 2024
1 parent 2e43916 commit e65ff29
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 40 deletions.
43 changes: 16 additions & 27 deletions log/zap.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ const hoursPerDay = 24
// InitFromConfig initializes a Zap-based logger.
func InitLoggerFromConfig(
loggerPrefixName, moduleName string,
sdkType, platformName string,
logLevel int,
isStdout bool,
isJson bool,
Expand All @@ -61,11 +62,22 @@ func InitLoggerFromConfig(
moduleVersion string,
isSimplify bool,
) error {
l, err := NewZapLogger(loggerPrefixName, moduleName, logLevel, isStdout, isJson, logLocation,
rotateCount, rotationTime, moduleVersion, isSimplify)
if err != nil {
return err
var l Logger
var err error

if sdkType != "" && platformName != "" {
l, err = NewSDKZapLogger(loggerPrefixName, moduleName, sdkType, platformName, logLevel, isStdout, isJson, logLocation, rotateCount, rotationTime, moduleVersion, isSimplify)
if err != nil {
return err
}
} else {
l, err = NewZapLogger(loggerPrefixName, moduleName, logLevel, isStdout, isJson, logLocation,
rotateCount, rotationTime, moduleVersion, isSimplify)
if err != nil {
return err
}
}

pkgLogger = l.WithCallDepth(callDepth)
if isJson {
pkgLogger = pkgLogger.WithName(moduleName)
Expand All @@ -89,29 +101,6 @@ func InitConsoleLogger(moduleName string,

}

func InitSDKLogger(
loggerPrefixName, moduleName string,
sdkType, platformName string,
logLevel int,
isStdout bool,
isJson bool,
logLocation string,
rotateCount uint,
rotationTime uint,
moduleVersion string,
isSimplify bool,
) error {
l, err := NewSDKZapLogger(loggerPrefixName, moduleName, sdkType, platformName, logLevel, isStdout, isJson, logLocation, rotateCount, rotationTime, moduleVersion, isSimplify)
if err != nil {
return err
}
pkgLogger = l.WithCallDepth(callDepth)
if isJson {
pkgLogger = pkgLogger.WithName(moduleName)
}
return nil
}

func ZDebug(ctx context.Context, msg string, keysAndValues ...any) {
if pkgLogger == nil {
return
Expand Down
28 changes: 15 additions & 13 deletions log/zap_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,19 +11,21 @@ import (

// TestSDKLog tests the SDKLog function for proper log output including custom [file:line] information
func TestSDKLog(t *testing.T) {
err := InitSDKLogger(
"testLogger", // loggerPrefixName
"testModule", // moduleName
"TestSDK", // sdkType
"TestPlatform", // platformName
5, // logLevel (INFO)
true, // isStdout
true, // isJson
"./logs", // logLocation
5, // rotateCount
24, // rotationTime
"1.0.0", // moduleVersion
false, // isSimplify
sdkType := "TestSDK"
platformName := "testPlatform"
err := InitLoggerFromConfig(
"testLogger", // loggerPrefixName
"testModule", // moduleName
sdkType, // sdkType
platformName, // platformName
5, // logLevel (INFO)
true, // isStdout
false, // isJson
"./logs", // logLocation
5, // rotateCount
24, // rotationTime
"1.0.0", // moduleVersion
false, // isSimplify
)
assert.NoError(t, err)

Expand Down

0 comments on commit e65ff29

Please sign in to comment.