Skip to content

Commit

Permalink
Fix attribute indenting.
Browse files Browse the repository at this point in the history
  • Loading branch information
jmalloc committed Jul 19, 2024
1 parent 22a421a commit 38cd0e0
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 8 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,11 @@ The format is based on [Keep a Changelog], and this project adheres to

- Added `WithAbsoluteTimestamps()` and `WithRelativeTimestamps()` options.

### Fixed

- Fix loss of attribute indenting when using a grouped logger.
- Fix reversion to relative timestamps when logging to an `io.Writer`.

### Changed

- Indent the attributes when logging to an `io.Writer`.
Expand Down
18 changes: 11 additions & 7 deletions spruce.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,12 @@ import (
)

type handler struct {
log func(string) error
attrs []slog.Attr
groups []string
initialDepth int
threshold slog.Level
writeTime func(*strings.Builder, slog.Record)
log func(string) error
attrs []slog.Attr
groups []string
depth int
threshold slog.Level
writeTime func(*strings.Builder, slog.Record)
}

func (h *handler) Enabled(context.Context, slog.Level) bool {
Expand Down Expand Up @@ -76,7 +76,7 @@ func (h *handler) Handle(_ context.Context, rec slog.Record) error {

buf.WriteString(rec.Message)

writeAttrs(buf, h.initialDepth, attrs, true)
writeAttrs(buf, h.depth, attrs, true)

return h.log(buf.String())
}
Expand Down Expand Up @@ -145,6 +145,8 @@ func (h *handler) WithAttrs(attrs []slog.Attr) slog.Handler {
log: h.log,
attrs: append(slices.Clone(h.attrs), attrs...),
groups: h.groups,
depth: h.depth,
threshold: h.threshold,
writeTime: h.writeTime,
}
}
Expand All @@ -154,6 +156,8 @@ func (h *handler) WithGroup(name string) slog.Handler {
log: h.log,
attrs: h.attrs,
groups: append(slices.Clone(h.groups), name),
depth: h.depth,
threshold: h.threshold,
writeTime: h.writeTime,
}
}
2 changes: 1 addition & 1 deletion stream.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ func NewStreamHandler(w io.Writer, options ...Option) slog.Handler {
}
return nil
},
initialDepth: 1,
depth: 1,
}

WithAbsoluteTimestamps()(h)
Expand Down

0 comments on commit 38cd0e0

Please sign in to comment.