Skip to content

Commit

Permalink
✨ Add SetFieldKey
Browse files Browse the repository at this point in the history
  • Loading branch information
nkmr-jp committed Oct 27, 2023
1 parent e8ea1e1 commit 7fb5dba
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 9 deletions.
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
5 changes: 5 additions & 0 deletions options.go
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,11 @@ func SetOmitKeys(key ...Key) {
omitKeys = key
}

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

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

func TestSetSeparator(t *testing.T) {
func TestSetFieldKey(t *testing.T) {
SetFieldKey(FunctionKey, "func")
SetFieldKey(StacktraceKey, "stack_trace")
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

0 comments on commit 7fb5dba

Please sign in to comment.