@@ -147,6 +147,8 @@ func TestDebug(t *testing.T) {
147147}
148148
149149func 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
180225func TestRace (t * testing.T ) {
0 commit comments