@@ -12,6 +12,7 @@ import (
12
12
"io"
13
13
"log"
14
14
loginternal "log/internal"
15
+ "os"
15
16
"path/filepath"
16
17
"regexp"
17
18
"runtime"
@@ -83,6 +84,13 @@ func TestConnections(t *testing.T) {
83
84
Info ("msg" , "a" , 1 )
84
85
checkLogOutput (t , logbuf .String (), `logger_test.go:\d+: INFO msg a=1` )
85
86
logbuf .Reset ()
87
+ Info ("msg" , "p" , nil )
88
+ checkLogOutput (t , logbuf .String (), `logger_test.go:\d+: INFO msg p=<nil>` )
89
+ logbuf .Reset ()
90
+ var r * regexp.Regexp
91
+ Info ("msg" , "r" , r )
92
+ checkLogOutput (t , logbuf .String (), `logger_test.go:\d+: INFO msg r=<nil>` )
93
+ logbuf .Reset ()
86
94
Warn ("msg" , "b" , 2 )
87
95
checkLogOutput (t , logbuf .String (), `logger_test.go:\d+: WARN msg b=2` )
88
96
logbuf .Reset ()
@@ -571,3 +579,62 @@ func wantAllocs(t *testing.T, want int, f func()) {
571
579
t .Errorf ("got %d allocs, want %d" , got , want )
572
580
}
573
581
}
582
+
583
+ // panicTextAndJsonMarshaler is a type that panics in MarshalText and MarshalJSON.
584
+ type panicTextAndJsonMarshaler struct {
585
+ msg any
586
+ }
587
+
588
+ func (p panicTextAndJsonMarshaler ) MarshalText () ([]byte , error ) {
589
+ panic (p .msg )
590
+ }
591
+
592
+ func (p panicTextAndJsonMarshaler ) MarshalJSON () ([]byte , error ) {
593
+ panic (p .msg )
594
+ }
595
+
596
+ func TestPanics (t * testing.T ) {
597
+ // Revert any changes to the default logger. This is important because other
598
+ // tests might change the default logger using SetDefault. Also ensure we
599
+ // restore the default logger at the end of the test.
600
+ currentLogger := Default ()
601
+ t .Cleanup (func () {
602
+ SetDefault (currentLogger )
603
+ log .SetOutput (os .Stderr )
604
+ log .SetFlags (log .LstdFlags )
605
+ })
606
+
607
+ var logBuf bytes.Buffer
608
+ log .SetOutput (& logBuf )
609
+ log .SetFlags (log .Lshortfile &^ log .LstdFlags )
610
+
611
+ SetDefault (New (newDefaultHandler (loginternal .DefaultOutput )))
612
+ for _ , pt := range []struct {
613
+ in any
614
+ out string
615
+ }{
616
+ {(* panicTextAndJsonMarshaler )(nil ), `logger_test.go:\d+: INFO msg p=<nil>` },
617
+ {panicTextAndJsonMarshaler {io .ErrUnexpectedEOF }, `logger_test.go:\d+: INFO msg p="!PANIC: unexpected EOF"` },
618
+ {panicTextAndJsonMarshaler {"panicking" }, `logger_test.go:\d+: INFO msg p="!PANIC: panicking"` },
619
+ {panicTextAndJsonMarshaler {42 }, `logger_test.go:\d+: INFO msg p="!PANIC: 42"` },
620
+ } {
621
+ Info ("msg" , "p" , pt .in )
622
+ checkLogOutput (t , logBuf .String (), pt .out )
623
+ logBuf .Reset ()
624
+ }
625
+
626
+ SetDefault (New (NewJSONHandler (& logBuf , nil )))
627
+ for _ , pt := range []struct {
628
+ in any
629
+ out string
630
+ }{
631
+ {(* panicTextAndJsonMarshaler )(nil ), `{"time":".*?","level":"INFO","msg":"msg","p":null}` },
632
+ {panicTextAndJsonMarshaler {io .ErrUnexpectedEOF }, `{"time":".*?","level":"INFO","msg":"msg","p":"!PANIC: unexpected EOF"}` },
633
+ {panicTextAndJsonMarshaler {"panicking" }, `{"time":".*?","level":"INFO","msg":"msg","p":"!PANIC: panicking"}` },
634
+ {panicTextAndJsonMarshaler {42 }, `{"time":".*?","level":"INFO","msg":"msg","p":"!PANIC: 42"}` },
635
+ } {
636
+ Info ("msg" , "p" , pt .in )
637
+ checkLogOutput (t , logBuf .String (), pt .out )
638
+ logBuf .Reset ()
639
+ }
640
+ }
0 commit comments