zap: Expose the ability to disable errorVerbose via logger config
I use Zap for basically every service I write, it’s great! There’s one issue I have though. Since I use pkg/errors, each error satisfies the fmt.Formatter interface and thus, every error log comes with a stack trace. This is great in production when that information is vital, but during development, (i.e. with NewDevelopmentConfig) they still show up; which, given the less vital scenario, is a bit annoying.
I notice there’s DisableStacktrace which removes the stack trace that originates from the log call itself but no way to disable errorVerbose.
In development, stack traces clutter the logs and makes scanning the terminal pretty difficult. I usually don’t need to know the full stack trace, only the top-most element (or, the top-most errors.New call). And if I do, in a perfect world, I could flip a env var to enable it!
About this issue
- Original URL
- State: open
- Created 8 years ago
- Reactions: 53
- Comments: 27 (2 by maintainers)
Really, what I would like is for the cause trace provided by
pkg/errorsto be pretty-printed so that I can read it. Right now, the formatting is that it provides several lines, separated by\nwhich get shoved into theerrorVerbosefield. Imagine if the stacktrace provided by Zap were to be shoved into a field in development mode. It would be very hard to read.Another workaround sample based on using
AddStacktraceis to have a wrapper function and use that for logging.This. 10000% This.
Do you have any update about it ?
I’m sorry, then i’m out of ideas!
edit: I just had the idea that one can offload that task to the encoder maybe… something like this could work
basically you look for errors in fields that fulfill the
stackTracerinterface ( pkg/errors does) and in that case you add a plain error and override the stack of the entry with the stack from the error. I tried it and it works; but im not sure if that is the best ideaedit2: there are some bugs in that code; one should take more care in plugging the stack traces together, ( if there are more than one error fields i.e.) but the idea should basically work
I eventually stopped using error libraries that include stack traces by default, as I found no real use-case for them and they cluttered logs up and increased log storage costs by a factor of ~x3. Now we use Fault, which only includes stack frames from your own code and not stdlib/external libraries which is exactly what we wanted. May not suit everyone’s needs, but Fault is an error wrapping framework so it’s versatile enough to build your own error management strategy.
It also plays well with Zap via the ErrorFormatter/errorVerbose things mentioned above.
It still would be nice to be able to disable errorVerbose but the discussion above outlined why the original problem was not solvable via Zap internally and it’s just an attribute of the chosen error library. Given I feel that the original problem was addressed/explained well, I feel this issue could be closed. But as mentioned, disabling errorVerbose may still have values so I’ll defer the decision of closing the issue to Zap maintainers.
A possible way to address this issue is by introducing an
ErrorEnoderinterface. Something like,If the provided
ObjectEncoderimplements this interface,Field.AddToshould use it to serialize theerrorinstead ofencodeError. We’d have to make sure that the solution also works for thezap.Errorsfield.Additionally, if we want
zap.Errorsto support serializing a list of errors into a list of strings (as opposed to the list of objects they get serialized to today, i.e.,"errors": [{"error": "problem"}, ...]),ErrorEncodermay need to support anAddErrors(string, []error) errormethod as well.