diff --git a/example_test.go b/example_test.go index 12cf225..b5551d5 100644 --- a/example_test.go +++ b/example_test.go @@ -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"} @@ -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() diff --git a/options.go b/options.go index 7426466..18e7330 100644 --- a/options.go +++ b/options.go @@ -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 diff --git a/options_test.go b/options_test.go index a2018ec..733eb8c 100644 --- a/options_test.go +++ b/options_test.go @@ -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() } diff --git a/zl.go b/zl.go index d610c7a..c2a221c 100644 --- a/zl.go +++ b/zl.go @@ -36,6 +36,7 @@ var ( callerEncoder zapcore.CallerEncoder consoleFields = []string{consoleFieldDefault} omitKeys []Key + fieldKeys = make(map[Key]string) isStdOut bool separator = " " pid int @@ -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, @@ -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( @@ -270,6 +279,7 @@ func ResetGlobalLoggerSettings() { callerEncoder = nil consoleFields = []string{consoleFieldDefault} omitKeys = nil + fieldKeys = make(map[Key]string) isStdOut = false separator = " " fileName = ""