From 56c0341ef247298542e3eaa66967c197bbf9e8e7 Mon Sep 17 00:00:00 2001 From: Carsten Dietrich Date: Thu, 17 Nov 2022 16:59:10 +0100 Subject: [PATCH 1/6] feat(core/zap): Make caller encoder configurable, add additional one --- core/zap/caller_encoder.go | 31 +++++++++++++++++++++++ core/zap/caller_encoder_test.go | 12 +++++++++ core/zap/module.go | 44 +++++++++++++++++++++++++-------- 3 files changed, 77 insertions(+), 10 deletions(-) create mode 100644 core/zap/caller_encoder.go create mode 100644 core/zap/caller_encoder_test.go diff --git a/core/zap/caller_encoder.go b/core/zap/caller_encoder.go new file mode 100644 index 000000000..1cc02b954 --- /dev/null +++ b/core/zap/caller_encoder.go @@ -0,0 +1,31 @@ +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 { + if i == len(parts)-1 || len(part) == 0 { + file += part + } else if i == len(parts)-2 { + file += part + "/" + } else { + 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 000000000..8be230e55 --- /dev/null +++ b/core/zap/caller_encoder_test.go @@ -0,0 +1,12 @@ +package zap + +import ( + "testing" + + "github.com/stretchr/testify/assert" +) + +func TestShort(t *testing.T) { + 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 1b5cea64b..268c3818a 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"}, @@ -173,12 +192,6 @@ func (m *Module) CueConfig() string { // language=cue return fmt.Sprintf(` core: zap: { - loglevel: %s - sampling: { - enabled: bool | *true - initial: int | *100 - thereafter: int | *100 - } json: bool | *false colored: bool | *false devmode: bool | *false @@ -186,8 +199,19 @@ core: zap: { fieldmap: { [string]: string } + + loglevel: %s + sampling: { + enabled: bool | *true + initial: int | *100 + thereafter: int | *100 + } + + encoding: { + caller: *"%s" | "%s" | "%s" + } } -`, allowedLevels) +`, allowedLevels, ZapCallerEncoderShort, ZapCallerEncoderSmart, ZapCallerEncoderFull) } // FlamingoLegacyConfigAlias mapping From 18185ef737b8af0b205550d09f9b69c056826a17 Mon Sep 17 00:00:00 2001 From: Carsten Dietrich Date: Wed, 12 Feb 2025 11:09:15 +0100 Subject: [PATCH 2/6] chore: move config around for smaller diff --- core/zap/module.go | 14 ++++++-------- 1 file changed, 6 insertions(+), 8 deletions(-) diff --git a/core/zap/module.go b/core/zap/module.go index 268c3818a..c74f9a6fc 100644 --- a/core/zap/module.go +++ b/core/zap/module.go @@ -192,6 +192,12 @@ func (m *Module) CueConfig() string { // language=cue return fmt.Sprintf(` core: zap: { + loglevel: %s + sampling: { + enabled: bool | *true + initial: int | *100 + thereafter: int | *100 + } json: bool | *false colored: bool | *false devmode: bool | *false @@ -199,14 +205,6 @@ core: zap: { fieldmap: { [string]: string } - - loglevel: %s - sampling: { - enabled: bool | *true - initial: int | *100 - thereafter: int | *100 - } - encoding: { caller: *"%s" | "%s" | "%s" } From 9146abcc96676887c4fbe1dd2cf57a9738398095 Mon Sep 17 00:00:00 2001 From: Carsten Dietrich Date: Wed, 12 Feb 2025 11:26:47 +0100 Subject: [PATCH 3/6] chore: cleanup golangci lint --- .golangci.yml | 13 ++----------- 1 file changed, 2 insertions(+), 11 deletions(-) diff --git a/.golangci.yml b/.golangci.yml index 21eb294b5..c2b4a6cad 100644 --- a/.golangci.yml +++ b/.golangci.yml @@ -7,19 +7,10 @@ run: skip-dirs-use-default: true modules-download-mode: readonly allow-parallel-runners: false - go: '1.22' # output configuration options output: - # Format: colored-line-number|line-number|json|tab|checkstyle|code-climate|junit-xml|github-actions - # - # Multiple can be specified by separating them by comma, output can be provided - # for each of them by separating format name and path by colon symbol. - # Output path can be either `stdout`, `stderr` or path to the file to write to. - # Example: "checkstyle:report.json,colored-line-number" - # - # Default: colored-line-number - format: colored-line-number + formats: colored-line-number print-issued-lines: true print-linter-name: true uniq-by-line: true @@ -32,6 +23,7 @@ linters: - bodyclose - containedctx - contextcheck + - copyloopvar - cyclop - durationcheck - err113 @@ -39,7 +31,6 @@ linters: - errname - errorlint - exhaustive - - exportloopref - forbidigo - forcetypeassert - gocognit From 92c5ba9d5c57428434e0989a5911f17a40e0a8d8 Mon Sep 17 00:00:00 2001 From: Carsten Dietrich Date: Wed, 12 Feb 2025 11:28:57 +0100 Subject: [PATCH 4/6] chore: fix linter --- core/zap/caller_encoder.go | 7 ++++--- core/zap/caller_encoder_test.go | 3 ++- 2 files changed, 6 insertions(+), 4 deletions(-) diff --git a/core/zap/caller_encoder.go b/core/zap/caller_encoder.go index 1cc02b954..35300b105 100644 --- a/core/zap/caller_encoder.go +++ b/core/zap/caller_encoder.go @@ -14,11 +14,12 @@ func short(file string) string { file = "" for i, part := range parts { - if i == len(parts)-1 || len(part) == 0 { + switch { + case i == len(parts)-1 || len(part) == 0: file += part - } else if i == len(parts)-2 { + case i == len(parts)-2: file += part + "/" - } else { + default: file += string(part[0]) + "/" } } diff --git a/core/zap/caller_encoder_test.go b/core/zap/caller_encoder_test.go index 8be230e55..b33d312e3 100644 --- a/core/zap/caller_encoder_test.go +++ b/core/zap/caller_encoder_test.go @@ -1,4 +1,4 @@ -package zap +package zap //nolint:testpackage whitebox test import ( "testing" @@ -7,6 +7,7 @@ import ( ) 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")) } From b0c7474e74a79d910a7131410d4fcf6fe23c59b1 Mon Sep 17 00:00:00 2001 From: Carsten Dietrich Date: Wed, 12 Feb 2025 11:32:30 +0100 Subject: [PATCH 5/6] chore: fix linter --- core/zap/caller_encoder_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/core/zap/caller_encoder_test.go b/core/zap/caller_encoder_test.go index b33d312e3..da1b3129d 100644 --- a/core/zap/caller_encoder_test.go +++ b/core/zap/caller_encoder_test.go @@ -1,4 +1,4 @@ -package zap //nolint:testpackage whitebox test +package zap //nolint:testpackage // explicit whitebox test import ( "testing" From 435b5f5ea44c4f60ced07c9f030b2400fca1fd78 Mon Sep 17 00:00:00 2001 From: Carsten Dietrich Date: Wed, 12 Feb 2025 11:34:49 +0100 Subject: [PATCH 6/6] chore: add example to readme --- core/zap/Readme.md | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/core/zap/Readme.md b/core/zap/Readme.md index 2573218ec..38d39e564 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