From 76756971025cd373e711c65d25a597ea3a4d0cc0 Mon Sep 17 00:00:00 2001 From: Aleksandr Maus Date: Fri, 8 Sep 2023 14:30:49 -0400 Subject: [PATCH] Improve coordinator logging correctness (#3279) --- internal/pkg/agent/application/coordinator/coordinator.go | 3 +++ pkg/core/logger/logger.go | 7 +++++++ 2 files changed, 10 insertions(+) diff --git a/internal/pkg/agent/application/coordinator/coordinator.go b/internal/pkg/agent/application/coordinator/coordinator.go index 111aacd8bdb..f45ba99ba86 100644 --- a/internal/pkg/agent/application/coordinator/coordinator.go +++ b/internal/pkg/agent/application/coordinator/coordinator.go @@ -1238,6 +1238,9 @@ type coordinatorUnitLog struct { } func logBasedOnState(l *logger.Logger, state client.UnitState, msg string, args ...interface{}) { + // Skipping one more stack frame in order to have correct file line set in the logger output while using this wrapper function + l = logger.AddCallerSkip(l, 1) + switch state { case client.UnitStateStarting: l.With(args...).Info(msg) diff --git a/pkg/core/logger/logger.go b/pkg/core/logger/logger.go index 28bac63c7cc..e82bfd323b0 100644 --- a/pkg/core/logger/logger.go +++ b/pkg/core/logger/logger.go @@ -71,6 +71,13 @@ func NewWithoutConfig(name string) *Logger { return logp.NewLogger(name) } +// AddCallerSkip returns new logger with incremented stack frames to skip. +// This is needed in order to correctly report the log file lines when the logging statement +// is wrapped in some convenience wrapping function for example. +func AddCallerSkip(l *Logger, skip int) *Logger { + return l.WithOptions(zap.AddCallerSkip(skip)) +} + func new(name string, cfg *Config, logInternal bool) (*Logger, error) { commonCfg, err := ToCommonConfig(cfg) if err != nil {