]> Cypherpunks repositories - gostls13.git/commit
log/slog: catch panics in LogValue
authorJonathan Amsterdam <jba@google.com>
Wed, 12 Apr 2023 14:07:35 +0000 (10:07 -0400)
committerJonathan Amsterdam <jba@google.com>
Wed, 12 Apr 2023 20:35:18 +0000 (20:35 +0000)
commit38531c6290bfb321d74fa4e48a6e889e29b60dab
treed06fc536175c92d9a3ffb77df8dda4501410bf98
parentf3e6f0f296c5d0a317527ebe6471eedabc408fae
log/slog: catch panics in LogValue

If a LogValue call panics, recover and return an error instead.

The error contains some stack information to make it easier to
find the problem. A number of people complained that panics
in fmt.Formatter.Format functions are hard to debug because
there is no context.

This is an example of the error text:

    LogValue panicked
    called from log/slog.panickingLogValue.LogValue (/usr/local/google/home/jba/repos/go/src/log/slog/value_test.go:221)
    called from log/slog.Value.resolve (/usr/local/google/home/jba/repos/go/src/log/slog/value.go:465)
    called from log/slog.Value.Resolve (/usr/local/google/home/jba/repos/go/src/log/slog/value.go:446)
    called from log/slog.TestLogValue (/usr/local/google/home/jba/repos/go/src/log/slog/value_test.go:192)
    called from testing.tRunner (/usr/local/google/home/jba/repos/go/src/testing/testing.go:1595)
    (rest of stack elided)

Fixes #59141.

Change-Id: I62e6ff6968d1aa34873e955c2d606d25418a673b
Reviewed-on: https://go-review.googlesource.com/c/go/+/484097
TryBot-Result: Gopher Robot <gobot@golang.org>
Reviewed-by: Alan Donovan <adonovan@google.com>
Run-TryBot: Jonathan Amsterdam <jba@google.com>
src/log/slog/value.go
src/log/slog/value_test.go