diff --git a/CHANGELOG.md b/CHANGELOG.md index cb4e09e..c5a257f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -10,6 +10,12 @@ The format is based on [Keep a Changelog], and this project adheres to [Keep a Changelog]: https://keepachangelog.com/en/1.0.0/ [Semantic Versioning]: https://semver.org/spec/v2.0.0.html +## [Unreleased] + +### Fixed + +- `With()` now places attributes in the current group instead of at the root. + ## [0.2.0] - 2024-07-19 ### Added diff --git a/spruce.go b/spruce.go index bc2a8dc..f30d1f3 100644 --- a/spruce.go +++ b/spruce.go @@ -22,15 +22,21 @@ func (h *handler) Enabled(context.Context, slog.Level) bool { func (h *handler) Handle(_ context.Context, rec slog.Record) error { buf := &strings.Builder{} - attrs := slices.Clone(h.attrs) + var attrs []slog.Attr if len(h.groups) == 0 { + attrs = slices.Clone(h.attrs) rec.Attrs(func(attr slog.Attr) bool { attrs = append(attrs, attr) return true }) } else { var grouped []any + + for _, attr := range h.attrs { + grouped = append(grouped, attr) + } + rec.Attrs(func(attr slog.Attr) bool { grouped = append(grouped, attr) return true diff --git a/spruce_test.go b/spruce_test.go index 9ea5b80..6f1716f 100644 --- a/spruce_test.go +++ b/spruce_test.go @@ -132,6 +132,21 @@ func TestHandler_WithAttrs_sameKey(t *testing.T) { ) } +func TestHandler_WithAttrs_inGroupKey(t *testing.T) { + s := &testingTStub{T: t} + l := spruce. + NewTestLogger(s). + WithGroup(""). + With("", "") + + l.Info("") + s.Expect( + `[INFO ] `, + `╰─┬ `, + ` ╰── `, + ) +} + func TestHandler_WithGroup(t *testing.T) { s := &testingTStub{T: t} l := spruce.