Skip to content

Commit

Permalink
feat: automatically inject more CallerOffset in `pterm.NewSlogHandl…
Browse files Browse the repository at this point in the history
…er` (#609)
  • Loading branch information
MarvinJWendt committed Jan 3, 2024
1 parent 0c9989e commit 2894abb
Showing 1 changed file with 12 additions and 5 deletions.
17 changes: 12 additions & 5 deletions slog_handler.go
Expand Up @@ -48,17 +48,24 @@ func (s *SlogHandler) Handle(ctx context.Context, record slog.Record) error {
// Wrapping args inside another slice to match [][]LoggerArgument
argsWrapped := [][]LoggerArgument{args}

logger := s.logger

// Must be done here, see https://github.com/pterm/pterm/issues/608#issuecomment-1876001650
if logger.CallerOffset == 0 {
logger = logger.WithCallerOffset(3)
}

switch level {
case slog.LevelDebug:
s.logger.Debug(message, argsWrapped...)
logger.Debug(message, argsWrapped...)
case slog.LevelInfo:
s.logger.Info(message, argsWrapped...)
logger.Info(message, argsWrapped...)
case slog.LevelWarn:
s.logger.Warn(message, argsWrapped...)
logger.Warn(message, argsWrapped...)
case slog.LevelError:
s.logger.Error(message, argsWrapped...)
logger.Error(message, argsWrapped...)
default:
s.logger.Print(message, argsWrapped...)
logger.Print(message, argsWrapped...)
}

return nil
Expand Down

0 comments on commit 2894abb

Please sign in to comment.