Skip to content

Commit

Permalink
Improve coordinator logging correctness (#3279)
Browse files Browse the repository at this point in the history
  • Loading branch information
aleksmaus authored Sep 8, 2023
1 parent 815558f commit 7675697
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 0 deletions.
3 changes: 3 additions & 0 deletions internal/pkg/agent/application/coordinator/coordinator.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
7 changes: 7 additions & 0 deletions pkg/core/logger/logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit 7675697

Please sign in to comment.