Skip to content

Commit

Permalink
promslog: Only log basename, not full path
Browse files Browse the repository at this point in the history
Signed-off-by: Julien <roidelapluie@o11y.eu>
  • Loading branch information
roidelapluie committed Oct 8, 2024
1 parent dae848d commit 4cf5484
Showing 1 changed file with 14 additions and 2 deletions.
16 changes: 14 additions & 2 deletions promslog/slog.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,17 @@ var (
default:
}

return a
}
truncateSourceAttrFunc = func(groups []string, a slog.Attr) slog.Attr {
if a.Key != slog.SourceKey {
return a
}

if src, ok := a.Value.Any().(*slog.Source); ok {
a.Value = slog.StringValue(filepath.Base(src.File) + ":" + strconv.Itoa(src.Line))
}

return a
}
)
Expand Down Expand Up @@ -165,8 +176,9 @@ func New(config *Config) *slog.Logger {
}

logHandlerOpts := &slog.HandlerOptions{
Level: config.Level.lvl,
AddSource: true,
Level: config.Level.lvl,
AddSource: true,
ReplaceAttr: truncateSourceAttrFunc,
}

if config.Style == GoKitStyle {
Expand Down

0 comments on commit 4cf5484

Please sign in to comment.