zerolog icon indicating copy to clipboard operation
zerolog copied to clipboard

Formatted stack trace on panic

Open abagshaw opened this issue 4 years ago • 1 comments

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?

abagshaw avatar Jan 29 '21 05:01 abagshaw

There are two things I do normally.

  1. I set the zerolog.ErrorStackMarshaler = pkgerrors.MarshalStack
  2. 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.

trevoro avatar Feb 19 '21 23:02 trevoro