From 4cf5484b56ccde0e9d251ae7790164ee260bc9ad Mon Sep 17 00:00:00 2001 From: Julien Date: Tue, 8 Oct 2024 10:09:02 +0200 Subject: [PATCH] promslog: Only log basename, not full path Signed-off-by: Julien --- promslog/slog.go | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/promslog/slog.go b/promslog/slog.go index f77ce91e..1677605a 100644 --- a/promslog/slog.go +++ b/promslog/slog.go @@ -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 } ) @@ -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 {