Skip to content

Panic when trying to log a nil *time.Time #22

@JDiPierro

Description

@JDiPierro

I have an object with a *time.Time field that's sometimes filled in and sometimes not.

When trying to log it with a call like this:

	logger.Info("Object created",
		"expiration", object.Expiration,
	)

I received a panic because of the nil Expiration field:

panic: value method time.Time.String called using nil *Time pointer

goroutine 148 [running]:
time.(*Time).String(0x103630640?)
	<autogenerated>:1 +0x48
github.com/phsym/console-slog.encoder.writeValue({{0x0, {0x1026880c8, 0x1400048d280}, 0x0, {0x101b5fea1, 0x13}, {0x1026c3020, 0x140001fe600}}}, 0x14002923788, {{}, ...})
	/path/to/project/vendor/github.com/phsym/console-slog/encoding.go:150 +0x4a8
github.com/phsym/console-slog.encoder.writeAttr({{0x0, {0x1026880c8, 0x1400048d280}, 0x0, {0x101b5fea1, 0x13}, {0x1026c3020, 0x140001fe600}}}, 0x14002923788, {{0x101b5444b, ...}, ...}, ...)
	/path/to/project/vendor/github.com/phsym/console-slog/encoding.go:126 +0x90c
github.com/phsym/console-slog.(*Handler).Handle.func1(...)
	/path/to/project/vendor/github.com/phsym/console-slog/handler.go:95
log/slog.Record.Attrs(...)
	/Users/justin.dipierro/go/pkg/mod/golang.org/[email protected]/src/log/slog/record.go:84
github.com/phsym/console-slog.(*Handler).Handle(_, {_, _}, {{0xc1fa1a63d3bd11f0, 0x2846b2e9a90, 0x1036868a0}, {0x101bc6735, 0x37}, 0x0, 0x10180c755, ...})
	/path/to/project/vendor/github.com/phsym/console-slog/handler.go:94 +0x36c
github.com/go-slog/otelslog.Handler.Handle({{_, _}}, {_, _}, {{0xc1fa1a63d3bd11f0, 0x2846b2e9a90, 0x1036868a0}, {0x101bc6735, 0x37}, 0x0, ...})
	/path/to/project/vendor/github.com/go-slog/otelslog/handler.go:55 +0x1fc

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions