Skip to content

Commit cd356cb

Browse files
committed
Don't panic on nil values
1 parent 68327e2 commit cd356cb

File tree

3 files changed

+13
-0
lines changed

3 files changed

+13
-0
lines changed

internal/entryhuman/entry.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,9 @@ func reset(w io.Writer, termW io.Writer) {
5757
}
5858

5959
func formatValue(v interface{}) string {
60+
if v == nil {
61+
return "<nil>"
62+
}
6063
typ := reflect.TypeOf(v)
6164
switch typ.Kind() {
6265
case reflect.Struct, reflect.Map:

internal/entryhuman/entry_test.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,15 @@ func TestEntry(t *testing.T) {
8686
),
8787
},
8888
},
89+
{
90+
"nil",
91+
slog.SinkEntry{
92+
Level: slog.LevelWarn,
93+
Fields: slog.M(
94+
slog.F("nan", nil),
95+
),
96+
},
97+
},
8998
{
9099
"bytes",
91100
slog.SinkEntry{
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
0001-01-01 00:00:00.000 [warn] nan=<nil>

0 commit comments

Comments
 (0)