Skip to content

Commit

Permalink
feat: mount source using slog.Group (#5)
Browse files Browse the repository at this point in the history
  • Loading branch information
telmoandrade authored Apr 15, 2024
1 parent c4a5257 commit 907c6fd
Showing 1 changed file with 12 additions and 8 deletions.
20 changes: 12 additions & 8 deletions attributes.go
Original file line number Diff line number Diff line change
Expand Up @@ -254,14 +254,18 @@ func FormatRequest(req *http.Request, ignoreHeaders bool) map[string]any {
func Source(sourceKey string, r *slog.Record) slog.Attr {
fs := runtime.CallersFrames([]uintptr{r.PC})
f, _ := fs.Next()
return slog.Any(
sourceKey,
&slog.Source{
Function: f.Function,
File: f.File,
Line: f.Line,
},
)
var args []any
if f.Function != "" {
args = append(args, slog.String("function", f.Function))
}
if f.File != "" {
args = append(args, slog.String("file", f.File))
}
if f.Line != 0 {
args = append(args, slog.Int("line", f.Line))
}

return slog.Group(sourceKey, args...)
}

func StringSource(sourceKey string, r *slog.Record) slog.Attr {
Expand Down

0 comments on commit 907c6fd

Please sign in to comment.