Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding SetFieldKey Function and Corresponding Tests #58

Merged
merged 2 commits into from
Oct 27, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 19 additions & 1 deletion example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ func ExampleSetVersion() {
fmt.Println(string(bytes))

// Output:
// {"severity":"DEBUG","caller":"zl/zl.go:85","message":"INIT_LOGGER","version":"v1.0.0","console":"Severity: DEBUG, Output: ConsoleAndFile, File: ./log/example-set-version_v1.0.0.jsonl"}
// {"severity":"DEBUG","caller":"zl/zl.go:86","message":"INIT_LOGGER","version":"v1.0.0","console":"Severity: DEBUG, Output: ConsoleAndFile, File: ./log/example-set-version_v1.0.0.jsonl"}
// {"severity":"INFO","caller":"https://github.com/nkmr-jp/zl/blob/v1.0.0/example_test.go#L135","message":"INFO_MESSAGE","version":"v1.0.0","detail":"detail info xxxxxxxxxxxxxxxxx"}
// {"severity":"WARN","caller":"https://github.com/nkmr-jp/zl/blob/v1.0.0/example_test.go#L136","message":"WARN_MESSAGE","version":"v1.0.0","detail":"detail info xxxxxxxxxxxxxxxxx"}

Expand Down Expand Up @@ -270,6 +270,24 @@ func ExampleSetOmitKeys() {
// {"function":"github.com/nkmr-jp/zl_test.ExampleSetOmitKeys"}
}

func ExampleSetFieldKey() {
setupForExampleTest()

zl.SetOutputByString("Console")
zl.SetStdout()
zl.SetOmitKeys(
zl.LevelKey, zl.LoggerKey, zl.TimeKey,
zl.CallerKey, zl.VersionKey, zl.HostnameKey, zl.StacktraceKey, zl.PIDKey,
)
zl.SetFieldKey(zl.MessageKey, "msg")
zl.SetFieldKey(zl.FunctionKey, "fn")
zl.Init()
zl.Info("INFO_MESSAGE")

// Output:
// {"fn":"github.com/nkmr-jp/zl_test.ExampleSetFieldKey","msg":"INFO_MESSAGE"}
}

func ExampleError() {
setupForExampleTest()

Expand Down
8 changes: 8 additions & 0 deletions options.go
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,14 @@ func SetOmitKeys(key ...Key) {
omitKeys = key
}

// SetFieldKey is changes the key of the default field.
func SetFieldKey(key Key, val string) {
if key == "" || val == "" {
return
}
fieldKeys[key] = val
}

// SetStdout is changes the console log output from stderr to stdout.
func SetStdout() {
isStdOut = true
Expand Down
17 changes: 16 additions & 1 deletion options_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,23 @@ func TestSetLevelByString(t *testing.T) {
}
}

func TestSetSeparator(t *testing.T) {
func TestSetFieldKey(t *testing.T) {
SetFieldKey(FunctionKey, "func")
SetFieldKey(StacktraceKey, "stack_trace")
SetFieldKey(CallerKey, "")
SetFieldKey("", "abc")
assert.Equal(t,
map[Key]string{
FunctionKey: "func",
StacktraceKey: "stack_trace",
},
fieldKeys,
)
ResetGlobalLoggerSettings()
}

func TestSetSeparator(t *testing.T) {
SetSeparator(":")
assert.Equal(t, ":", separator)
ResetGlobalLoggerSettings()
}
24 changes: 17 additions & 7 deletions zl.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ var (
callerEncoder zapcore.CallerEncoder
consoleFields = []string{consoleFieldDefault}
omitKeys []Key
fieldKeys = make(map[Key]string)
isStdOut bool
separator = " "
pid int
Expand Down Expand Up @@ -88,13 +89,13 @@ func Init() {

func newEncoderConfig() *zapcore.EncoderConfig {
enc := zapcore.EncoderConfig{
MessageKey: string(MessageKey),
LevelKey: string(LevelKey),
TimeKey: string(TimeKey),
NameKey: string(LoggerKey),
CallerKey: string(CallerKey),
FunctionKey: string(FunctionKey),
StacktraceKey: string(StacktraceKey),
MessageKey: fieldKey(MessageKey),
LevelKey: fieldKey(LevelKey),
TimeKey: fieldKey(TimeKey),
NameKey: fieldKey(LoggerKey),
CallerKey: fieldKey(CallerKey),
FunctionKey: fieldKey(FunctionKey),
StacktraceKey: fieldKey(StacktraceKey),
EncodeLevel: zapcore.CapitalLevelEncoder,
EncodeTime: zapcore.RFC3339NanoTimeEncoder,
EncodeDuration: zapcore.StringDurationEncoder,
Expand All @@ -104,6 +105,14 @@ func newEncoderConfig() *zapcore.EncoderConfig {
return &enc
}

func fieldKey(key Key) string {
value, ok := fieldKeys[key]
if !ok {
return string(key)
}
return value
}

// See https://pkg.go.dev/go.uber.org/zap
func newLogger(enc *zapcore.EncoderConfig) *zap.Logger {
core := zapcore.NewCore(
Expand Down Expand Up @@ -270,6 +279,7 @@ func ResetGlobalLoggerSettings() {
callerEncoder = nil
consoleFields = []string{consoleFieldDefault}
omitKeys = nil
fieldKeys = make(map[Key]string)
isStdOut = false
separator = " "
fileName = ""
Expand Down