Skip to content

Commit 6494dc0

Browse files
jaimecaseroJaimeCasero
andauthored
feat: zap logger caller skip configurable (#512)
* feat: zap logger caller skip configurable * feat: yaml number parsed as float by cue config * fix: no default value in cue config causes bootstrap issue * fix: default value should be 2 * fix: def value should be 2 --------- Co-authored-by: JaimeCasero <[email protected]>
1 parent 8e42afe commit 6494dc0

File tree

1 file changed

+11
-1
lines changed

1 file changed

+11
-1
lines changed

core/zap/module.go

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ type (
2525
fieldMap map[string]string
2626
logSession bool
2727
callerEncoder zapcore.CallerEncoder
28+
callerskip float64
2829
}
2930

3031
shutdownEventSubscriber struct {
@@ -68,6 +69,7 @@ func (m *Module) Inject(config *struct {
6869
FieldMap config.Map `inject:"config:core.zap.fieldmap,optional"`
6970
LogSession bool `inject:"config:core.zap.logsession,optional"`
7071
CallerEncoder string `inject:"config:core.zap.encoding.caller,optional"`
72+
CallerSkip float64 `inject:"config:core.zap.callerskip,optional"`
7173
}) {
7274
m.area = config.Area
7375
m.json = config.JSON
@@ -80,6 +82,13 @@ func (m *Module) Inject(config *struct {
8082
m.logSession = config.LogSession
8183
m.callerEncoder = callerEncoders[ZapCallerEncoderShort]
8284

85+
// if not provided set 2 as default
86+
m.callerskip = 2
87+
if config.CallerSkip > 1 {
88+
// otherwise let user config override
89+
m.callerskip = config.CallerSkip
90+
}
91+
8392
if encoder, ok := callerEncoders[config.CallerEncoder]; ok {
8493
m.callerEncoder = encoder
8594
}
@@ -155,7 +164,7 @@ func (m *Module) createLoggerInstance() *Logger {
155164
InitialFields: nil,
156165
}
157166

158-
logger, err := cfg.Build(zap.AddCallerSkip(1))
167+
logger, err := cfg.Build(zap.AddCallerSkip(int(m.callerskip)))
159168
if err != nil {
160169
panic(err)
161170
}
@@ -208,6 +217,7 @@ core: zap: {
208217
encoding: {
209218
caller: *"%s" | "%s" | "%s"
210219
}
220+
callerskip: float64 | *2
211221
}
212222
`, allowedLevels, ZapCallerEncoderShort, ZapCallerEncoderSmart, ZapCallerEncoderFull)
213223
}

0 commit comments

Comments
 (0)