Skip to content

Commit

Permalink
feat: implement default Logger init. (#144)
Browse files Browse the repository at this point in the history
* feat: implement default Logger init.

* improve init func.

* remove unused field.
  • Loading branch information
mo3et authored Oct 23, 2024
1 parent ef74300 commit 49edc89
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 15 deletions.
40 changes: 25 additions & 15 deletions log/zap.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,30 @@ var (
}
)

const callDepth = 2
const hoursPerDay = 24
const (
callDepth int = 2
rotateCount uint = 1
hoursPerDay uint = 24
logPath string = "./logs/"
version string = "undefined version"
isSimplify = false
)

func init() {
InitLoggerFromConfig(
"DefaultLogger",
"DefaultLoggerModule",
"", "",
LevelDebug,
true,
false,
logPath,
rotateCount,
hoursPerDay,
version,
isSimplify,
)
}

// InitFromConfig initializes a Zap-based logger.
func InitLoggerFromConfig(
Expand Down Expand Up @@ -87,35 +109,23 @@ func InitConsoleLogger(moduleName string,
if isJson {
osStdout = osStdout.WithName(moduleName)
}
return nil

return nil
}

func ZDebug(ctx context.Context, msg string, keysAndValues ...any) {
if pkgLogger == nil {
return
}
pkgLogger.Debug(ctx, msg, keysAndValues...)
}

func ZInfo(ctx context.Context, msg string, keysAndValues ...any) {
if pkgLogger == nil {
return
}
pkgLogger.Info(ctx, msg, keysAndValues...)
}

func ZWarn(ctx context.Context, msg string, err error, keysAndValues ...any) {
if pkgLogger == nil {
return
}
pkgLogger.Warn(ctx, msg, err, keysAndValues...)
}

func ZError(ctx context.Context, msg string, err error, keysAndValues ...any) {
if pkgLogger == nil {
return
}
pkgLogger.Error(ctx, msg, err, keysAndValues...)
}

Expand Down
56 changes: 56 additions & 0 deletions log/zap_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package log

import (
"context"
"errors"
"os"
"testing"

Expand Down Expand Up @@ -59,3 +60,58 @@ func TestSDKLog(t *testing.T) {
// assert.Contains(t, output, "key")
// assert.Contains(t, output, "value")
}

func TestDefaultLog(t *testing.T) {
er := errors.New("error")
ZInfo(context.Background(), "Are you OK?")
ZDebug(context.Background(), "Hello")
ZWarn(context.Background(), "3Q", er)
ZError(context.Background(), "3Q very much", er)


sdkType := "TestSDK"
platformName := "testPlatform"

err := InitLoggerFromConfig(
"testLogger", // loggerPrefixName
"testModule", // moduleName
sdkType, // sdkType
platformName, // platformName
int(5), // logLevel (Debug)
true, // isStdout
false, // isJson
"./logs", // logLocation
uint(5), // rotateCount
uint(24), // rotationTime
"1.0.0", // moduleVersion
false, // isSimplify
)
assert.NoError(t, err)

stdout := os.Stdout
r, w, _ := os.Pipe()
os.Stdout = w

logger := zap.NewExample()
defer logger.Sync()

ZDebug(context.Background(), "hello")
SDKLog(context.Background(), 5, "cmd/abc.go", 666, "This is a test message", nil, []any{"key", "value"})
SDKLog(context.Background(), 4, "cmd/abc.go", 666, "This is a test message", nil, []any{"key", "value", "key", "key", 1})
SDKLog(context.Background(), 3, "cmd/abc.go", 666, "This is a test message", nil, []any{"key", "value"})
SDKLog(context.Background(), 2, "cmd/abc.go", 666, "This is a test message", nil, []any{"key", "value"})
ZWarn(context.TODO(), "msg", nil)
ZInfo(context.TODO(), "msg", nil)
ZDebug(context.TODO(), "msg")

w.Close()
out, _ := os.ReadFile(r.Name())
os.Stdout = stdout

_ = string(out)
// assert.Contains(t, output, "This is a test message")
// assert.Contains(t, output, "[TestSDK/TestPlatform]")
// assert.Contains(t, output, "[test_file.go:123]")
// assert.Contains(t, output, "key")
// assert.Contains(t, output, "value")
}

0 comments on commit 49edc89

Please sign in to comment.