Skip to content

Commit 20e99d1

Browse files
Fix attrs being silently dropped on child loggers (#33)
* Fix attrs being silently dropped on child loggers * Clean up un-needed nil
1 parent e7dda5d commit 20e99d1

3 files changed

Lines changed: 63 additions & 18 deletions

File tree

Taskfile.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ tasks:
6969
- .golangci.yml
7070
preconditions:
7171
- sh: command -v golangci-lint
72-
msg: staticcheck not installed, run `brew install golangci-lint`
72+
msg: golangci-lint not installed, run `brew install golangci-lint`
7373

7474
- sh: command -v typos
7575
msg: requires typos-cli, run `brew install typos-cli`

log.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,7 @@ func (l *Logger) clone() *Logger {
178178
timeFunc: l.timeFunc,
179179
timeFormat: l.timeFormat,
180180
prefix: l.prefix,
181+
attrs: l.attrs,
181182
level: l.level,
182183
mu: l.mu,
183184
}
@@ -216,8 +217,7 @@ func putBuffer(buf *bytes.Buffer) {
216217
// Approx 65kb
217218
const maxSize = 64 << 10
218219
if buf.Cap() > maxSize {
219-
// Make the buffer nil so GC cleans it up
220-
buf = nil
220+
return
221221
}
222222

223223
bufPool.Put(buf)

log_test.go

Lines changed: 60 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,8 @@ func TestDebug(t *testing.T) {
147147
}
148148

149149
func TestWith(t *testing.T) {
150+
hue.Enabled(false) // Force no color
151+
150152
// Constantly return the same time
151153
fixedTime := func() time.Time {
152154
fixed, err := time.Parse(time.RFC3339, "2025-04-01T13:34:03Z")
@@ -157,24 +159,67 @@ func TestWith(t *testing.T) {
157159

158160
fixedTimeString := fixedTime().Format(time.RFC3339)
159161

160-
buf := &bytes.Buffer{}
161-
162-
logger := log.New(buf, log.TimeFunc(fixedTime))
163-
164-
logger.Info("I'm an info message")
165-
166-
sub := logger.With(
167-
slog.Bool("sub", true),
168-
slog.String("hello", "world"),
169-
)
162+
tests := []struct {
163+
name string
164+
fn func() string // Exercise the logger, return output
165+
want string
166+
}{
167+
{
168+
name: "attrs appear on sub logger",
169+
fn: func() string {
170+
buf := &bytes.Buffer{}
171+
l := log.New(buf, log.TimeFunc(fixedTime))
172+
l.Info("I'm an info message")
173+
sub := l.With(slog.Bool("sub", true), slog.String("hello", "world"))
174+
sub.Info("I'm also an info message")
175+
176+
return buf.String()
177+
},
178+
want: "[TIME] INFO: I'm an info message\n[TIME] INFO: I'm also an info message sub=true hello=world\n",
179+
},
180+
{
181+
name: "chained With preserves earlier attrs",
182+
fn: func() string {
183+
buf := &bytes.Buffer{}
184+
l := log.New(buf, log.TimeFunc(fixedTime))
185+
l.With(slog.String("a", "1")).With(slog.String("b", "2")).Info("chained")
170186

171-
sub.Info("I'm also an info message")
187+
return buf.String()
188+
},
189+
want: "[TIME] INFO: chained a=1 b=2\n",
190+
},
191+
{
192+
name: "Prefixed preserves attrs from With",
193+
fn: func() string {
194+
buf := &bytes.Buffer{}
195+
l := log.New(buf, log.TimeFunc(fixedTime))
196+
l.With(slog.String("a", "1")).Prefixed("svc").Info("prefixed")
172197

173-
got := buf.String()
174-
got = strings.TrimSpace(strings.ReplaceAll(got, fixedTimeString, "[TIME]")) + "\n"
198+
return buf.String()
199+
},
200+
want: "[TIME] INFO svc: prefixed a=1\n",
201+
},
202+
{
203+
name: "parent logger not affected by child With",
204+
fn: func() string {
205+
buf := &bytes.Buffer{}
206+
l := log.New(buf, log.TimeFunc(fixedTime))
207+
_ = l.With(slog.String("child", "attr"))
208+
l.Info("parent should have no attrs")
209+
210+
return buf.String()
211+
},
212+
want: "[TIME] INFO: parent should have no attrs\n",
213+
},
214+
}
175215

176-
want := "[TIME] INFO: I'm an info message\n[TIME] INFO: I'm also an info message sub=true hello=world\n"
177-
test.Diff(t, got, want)
216+
for _, tt := range tests {
217+
t.Run(tt.name, func(t *testing.T) {
218+
got := tt.fn()
219+
got = strings.ReplaceAll(got, fixedTimeString, "[TIME]")
220+
test.Diff(t, got, tt.want)
221+
})
222+
}
178223
}
179224

180225
func TestRace(t *testing.T) {

0 commit comments

Comments
 (0)