rs / zerolog

Zero Allocation JSON Logger
MIT License
10.53k stars 571 forks source link

Formatted stack trace on panic #285

Open abagshaw opened 3 years ago

abagshaw commented 3 years ago

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?

trevoro commented 3 years ago

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.