[go: up one dir, main page]

Skip to content

Commit

Permalink
append record level attributes (#31)
Browse files Browse the repository at this point in the history
  • Loading branch information
Shubhaankar-Sharma authored Oct 23, 2023
1 parent a145684 commit 7af1557
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 0 deletions.
4 changes: 4 additions & 0 deletions _example/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,10 @@ func main() {
r.Get("/err", func(w http.ResponseWriter, r *http.Request) {
oplog := httplog.LogEntry(r.Context())
oplog.Error("msg here", "err", errors.New("err here"))

// logging with the global logger also works
slog.Default().With(slog.Group("ImpGroup", slog.String("account", "id"))).Error("doesn't exist")
slog.Default().Error("oops, err occured")
w.WriteHeader(500)
w.Write([]byte("oops, err"))
})
Expand Down
9 changes: 9 additions & 0 deletions text_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,15 @@ func (h *PrettyHandler) Handle(ctx context.Context, r slog.Record) error {
if h.groupOpen {
cW(h.preformattedAttrs, true, nWhite, "%s", "}")
}

// write record level attrs to buf
attrs := []slog.Attr{}
r.Attrs(func(attr slog.Attr) bool {
attrs = append(attrs, attr)
return true
})
writeAttrs(buf, attrs, false)

buf.WriteString("\n")
h.mu.Lock()
defer h.mu.Unlock()
Expand Down

0 comments on commit 7af1557

Please sign in to comment.