zerolog
zerolog copied to clipboard
Formatted stack trace on panic
Any way to get a nicely formatted stack trace like we do with log.Err(err).Stack().Msg("uh oh")
from a panic?
After recovering from a panic I'm wanting to log the stack trace and the only way I can do that right now is with debug.Stack()
which gives the stack trace as one big block of text. Is there any way to get the stack trace from a panic in a format that zerolog can recognize?
There are two things I do normally.
- I set the
zerolog.ErrorStackMarshaler = pkgerrors.MarshalStack
- In my recovery code do whatever clean up you want and then do something like this:
cause := errors.WithStack(v)
log.Error().Stack().Err(cause).Msg("uhoh")
Usually gets me what I want. The stack traces this way aren't as "robust" as you'd normally get but they're great for logging.