diff --git a/core/zap/Readme.md b/core/zap/Readme.md index 2573218e..38d39e56 100644 --- a/core/zap/Readme.md +++ b/core/zap/Readme.md @@ -11,9 +11,9 @@ Use the method `.WithContext(ctx)` whenever you have a context, this will make s ## Configuration -``` +```yaml zap: - loglevel: Info + loglevel: Info # "Debug" | "Info" | "Warn" | "Error" | "DPanic" | "Panic" | "Fatal" json: true colored: false devmode: false @@ -24,7 +24,8 @@ zap: thereafter: 100 fieldmap: key: value - + encoding: + caller: "short" # short | smart | full ``` ## More about Zap diff --git a/core/zap/caller_encoder.go b/core/zap/caller_encoder.go new file mode 100644 index 00000000..35300b10 --- /dev/null +++ b/core/zap/caller_encoder.go @@ -0,0 +1,32 @@ +package zap + +import ( + "os" + "runtime" + "strings" + + "go.uber.org/zap/zapcore" +) + +func short(file string) string { + parts := strings.Split(file, string(os.PathSeparator)) + + file = "" + + for i, part := range parts { + switch { + case i == len(parts)-1 || len(part) == 0: + file += part + case i == len(parts)-2: + file += part + "/" + default: + file += string(part[0]) + "/" + } + } + + return file +} + +func smartCallerEncoder(caller zapcore.EntryCaller, enc zapcore.PrimitiveArrayEncoder) { + enc.AppendString(short(runtime.FuncForPC(caller.PC).Name())) +} diff --git a/core/zap/caller_encoder_test.go b/core/zap/caller_encoder_test.go new file mode 100644 index 00000000..da1b3129 --- /dev/null +++ b/core/zap/caller_encoder_test.go @@ -0,0 +1,13 @@ +package zap //nolint:testpackage // explicit whitebox test + +import ( + "testing" + + "github.com/stretchr/testify/assert" +) + +func TestShort(t *testing.T) { + t.Parallel() + assert.Equal(t, "", short("")) + assert.Equal(t, "a/bbb/ccc.ddd.eee", short("aaa/bbb/ccc.ddd.eee")) +} diff --git a/core/zap/module.go b/core/zap/module.go index 1b5cea64..c74f9a6f 100644 --- a/core/zap/module.go +++ b/core/zap/module.go @@ -12,7 +12,7 @@ import ( ) type ( - // Module for logrus logging + // Module for zap logging Module struct { area string json bool @@ -24,6 +24,7 @@ type ( samplingThereafter float64 fieldMap map[string]string logSession bool + callerEncoder zapcore.CallerEncoder } shutdownEventSubscriber struct { @@ -31,6 +32,12 @@ type ( } ) +const ( + ZapCallerEncoderShort = "short" + ZapCallerEncoderSmart = "smart" + ZapCallerEncoderFull = "full" +) + var logLevels = map[string]zapcore.Level{ "Trace": zap.DebugLevel - 1, // does not exist in zap by default "Debug": zap.DebugLevel, @@ -42,6 +49,12 @@ var logLevels = map[string]zapcore.Level{ "Fatal": zap.FatalLevel, } +var callerEncoders = map[string]zapcore.CallerEncoder{ + ZapCallerEncoderSmart: smartCallerEncoder, + ZapCallerEncoderFull: zapcore.FullCallerEncoder, + ZapCallerEncoderShort: zapcore.ShortCallerEncoder, +} + // Inject dependencies func (m *Module) Inject(config *struct { Area string `inject:"config:area"` @@ -54,6 +67,7 @@ func (m *Module) Inject(config *struct { SamplingThereafter float64 `inject:"config:core.zap.sampling.thereafter,optional"` FieldMap config.Map `inject:"config:core.zap.fieldmap,optional"` LogSession bool `inject:"config:core.zap.logsession,optional"` + CallerEncoder string `inject:"config:core.zap.encoding.caller,optional"` }) { m.area = config.Area m.json = config.JSON @@ -64,6 +78,11 @@ func (m *Module) Inject(config *struct { m.samplingInitial = config.SamplingInitial m.samplingThereafter = config.SamplingThereafter m.logSession = config.LogSession + m.callerEncoder = callerEncoders[ZapCallerEncoderShort] + + if encoder, ok := callerEncoders[config.CallerEncoder]; ok { + m.callerEncoder = encoder + } if config.FieldMap != nil { m.fieldMap = make(map[string]string, len(config.FieldMap)) @@ -76,7 +95,7 @@ func (m *Module) Inject(config *struct { } } -// Configure the zap logger as flamingo.Logger (in JSON mode kibana compatible) +// Configure the zap logger as flamingo.Logger func (m *Module) Configure(injector *dingo.Injector) { injector.Bind(new(flamingo.Logger)).ToInstance(m.createLoggerInstance()) flamingo.BindEventSubscriber(injector).To(shutdownEventSubscriber{}) @@ -128,7 +147,7 @@ func (m *Module) createLoggerInstance() *Logger { EncodeLevel: encoder, EncodeTime: zapcore.ISO8601TimeEncoder, EncodeDuration: zapcore.SecondsDurationEncoder, - EncodeCaller: zapcore.ShortCallerEncoder, + EncodeCaller: m.callerEncoder, EncodeName: zapcore.FullNameEncoder, }, OutputPaths: []string{"stderr"}, @@ -186,8 +205,11 @@ core: zap: { fieldmap: { [string]: string } + encoding: { + caller: *"%s" | "%s" | "%s" + } } -`, allowedLevels) +`, allowedLevels, ZapCallerEncoderShort, ZapCallerEncoderSmart, ZapCallerEncoderFull) } // FlamingoLegacyConfigAlias mapping