Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support specifying a custom onPanic logger hook for tests #1415

Closed
MKrupauskas opened this issue Feb 9, 2024 · 0 comments · Fixed by #1416
Closed

Support specifying a custom onPanic logger hook for tests #1415

MKrupauskas opened this issue Feb 9, 2024 · 0 comments · Fixed by #1416

Comments

@MKrupauskas
Copy link
Contributor

There can be complications when testing Panic/DPanic logs similarly to Fatal logs as described in #846.

For example, it's impossible to recover from a logger.Panic in a tests if that log is emitted in another go routine. This is exactly the case with our periodic runner library.

The best approach we came up with was extending zap to support passing a custom onPanic hook like we have for onFatal.

sywhang pushed a commit that referenced this issue Feb 20, 2024
Add a `WithPanicHook` logger option that allows callers to specify
custom behavior besides panicking on Panic/DPanic logs. This is similar
to what we already have with the WithFatal hook implemented in
#861.

This will make it possible to unit test Panic log cases like the one we
had with our periodic runner which was impossible because of
unrecoverable panics in another go routine.

Added unit tests and they pass.

```
$ make test
?       go.uber.org/zap/internal        [no test files]
?       go.uber.org/zap/internal/bufferpool     [no test files]
ok      go.uber.org/zap (cached)
?       go.uber.org/zap/internal/readme [no test files]
ok      go.uber.org/zap/buffer  (cached)
ok      go.uber.org/zap/internal/color  (cached)
ok      go.uber.org/zap/internal/exit   (cached)
ok      go.uber.org/zap/internal/pool   (cached)
ok      go.uber.org/zap/internal/stacktrace     (cached)
ok      go.uber.org/zap/internal/ztest  (cached)
ok      go.uber.org/zap/zapcore (cached)
ok      go.uber.org/zap/zapgrpc (cached)
ok      go.uber.org/zap/zapio   (cached)
ok      go.uber.org/zap/zaptest (cached)
ok      go.uber.org/zap/zaptest/observer        (cached)
ok      go.uber.org/zap/exp/zapfield    (cached)
ok      go.uber.org/zap/exp/zapslog     (cached)
ok      go.uber.org/zap/benchmarks      (cached) [no tests to run]
ok      go.uber.org/zap/zapgrpc/internal/test   (cached)
```

Closes #1415
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging a pull request may close this issue.

1 participant