Skip to content

Commit 58353cb

Browse files
committed
log/slog: fix call depth when used with log.Fatal*
When slog.SetDefault has been changed and log.Fatal is used, the call depth was off by one. The handleWriter within slog expect the depth to be 4 levels deep. Fixes golang#62418
1 parent 1c92457 commit 58353cb

File tree

1 file changed

+40
-13
lines changed

1 file changed

+40
-13
lines changed

src/log/log.go

+40-13
Original file line numberDiff line numberDiff line change
@@ -280,40 +280,52 @@ func (l *Logger) Println(v ...any) {
280280

281281
// Fatal is equivalent to l.Print() followed by a call to [os.Exit](1).
282282
func (l *Logger) Fatal(v ...any) {
283-
l.Output(2, fmt.Sprint(v...))
283+
l.output(0, 2, func(b []byte) []byte {
284+
return fmt.Append(b, v...)
285+
})
284286
os.Exit(1)
285287
}
286288

287289
// Fatalf is equivalent to l.Printf() followed by a call to [os.Exit](1).
288290
func (l *Logger) Fatalf(format string, v ...any) {
289-
l.Output(2, fmt.Sprintf(format, v...))
291+
l.output(0, 2, func(b []byte) []byte {
292+
return fmt.Appendf(b, format, v...)
293+
})
290294
os.Exit(1)
291295
}
292296

293297
// Fatalln is equivalent to l.Println() followed by a call to [os.Exit](1).
294298
func (l *Logger) Fatalln(v ...any) {
295-
l.Output(2, fmt.Sprintln(v...))
299+
l.output(0, 2, func(b []byte) []byte {
300+
return fmt.Appendln(b, v...)
301+
})
296302
os.Exit(1)
297303
}
298304

299305
// Panic is equivalent to l.Print() followed by a call to panic().
300306
func (l *Logger) Panic(v ...any) {
301307
s := fmt.Sprint(v...)
302-
l.Output(2, s)
308+
l.output(0, 2, func(b []byte) []byte {
309+
return append(b, s...)
310+
})
303311
panic(s)
304312
}
305313

306314
// Panicf is equivalent to l.Printf() followed by a call to panic().
307315
func (l *Logger) Panicf(format string, v ...any) {
308316
s := fmt.Sprintf(format, v...)
309-
l.Output(2, s)
317+
l.output(0, 2, func(b []byte) []byte {
318+
return append(b, s...)
319+
})
310320
panic(s)
311321
}
312322

313323
// Panicln is equivalent to l.Println() followed by a call to panic().
314324
func (l *Logger) Panicln(v ...any) {
315325
s := fmt.Sprintln(v...)
316-
l.Output(2, s)
326+
l.output(0, 2, func(b []byte) []byte {
327+
return append(b, s...)
328+
})
317329
panic(s)
318330
}
319331

@@ -409,40 +421,52 @@ func Println(v ...any) {
409421

410422
// Fatal is equivalent to [Print] followed by a call to [os.Exit](1).
411423
func Fatal(v ...any) {
412-
std.Output(2, fmt.Sprint(v...))
424+
std.output(0, 2, func(b []byte) []byte {
425+
return fmt.Append(b, v...)
426+
})
413427
os.Exit(1)
414428
}
415429

416430
// Fatalf is equivalent to [Printf] followed by a call to [os.Exit](1).
417431
func Fatalf(format string, v ...any) {
418-
std.Output(2, fmt.Sprintf(format, v...))
432+
std.output(0, 2, func(b []byte) []byte {
433+
return fmt.Appendf(b, format, v...)
434+
})
419435
os.Exit(1)
420436
}
421437

422438
// Fatalln is equivalent to [Println] followed by a call to [os.Exit](1).
423439
func Fatalln(v ...any) {
424-
std.Output(2, fmt.Sprintln(v...))
440+
std.output(0, 2, func(b []byte) []byte {
441+
return fmt.Appendln(b, v...)
442+
})
425443
os.Exit(1)
426444
}
427445

428446
// Panic is equivalent to [Print] followed by a call to panic().
429447
func Panic(v ...any) {
430448
s := fmt.Sprint(v...)
431-
std.Output(2, s)
449+
std.output(0, 2, func(b []byte) []byte {
450+
return append(b, s...)
451+
})
432452
panic(s)
433453
}
434454

435455
// Panicf is equivalent to [Printf] followed by a call to panic().
436456
func Panicf(format string, v ...any) {
437457
s := fmt.Sprintf(format, v...)
438-
std.Output(2, s)
458+
std.output(0, 2, func(b []byte) []byte {
459+
return append(b, s...)
460+
})
439461
panic(s)
440462
}
441463

442464
// Panicln is equivalent to [Println] followed by a call to panic().
443465
func Panicln(v ...any) {
444466
s := fmt.Sprintln(v...)
445-
std.Output(2, s)
467+
std.output(0, 2, func(b []byte) []byte {
468+
return append(b, s...)
469+
})
446470
panic(s)
447471
}
448472

@@ -454,5 +478,8 @@ func Panicln(v ...any) {
454478
// if [Llongfile] or [Lshortfile] is set; a value of 1 will print the details
455479
// for the caller of Output.
456480
func Output(calldepth int, s string) error {
457-
return std.Output(calldepth+1, s) // +1 for this frame.
481+
calldepth++ // +1 for this frame
482+
return std.output(0, calldepth, func(b []byte) []byte {
483+
return append(b, s...)
484+
})
458485
}

0 commit comments

Comments
 (0)