rs / zerolog

Zero Allocation JSON Logger
MIT License
10.61k stars 572 forks source link

Dumb Question on logging error #546

Closed BigBoulard closed 8 months ago

BigBoulard commented 1 year ago

Hi team,

Disclaimer: I never logged at production grade so my questions may be dumb.

I can see this kind of code in your docs:

log.Error().Err(err).Msg("")

log is the logger, Error() sets the error level, and Err() should contain the raw error from what I understand, but I don't get the point of Msg. What is expected to be passed toMsg()?

In my microservice app, only the gateway is logging, other services only return errors to each other up to the gateway. The errors are wrapped by each layer of each microservice up to the gateway which logs an error like this one:

2023-04-29T09:14:44+04:00 ERR | can't do what you want me to | error="gw.controllerX.MethodX: gw.httpclientY.MethodY: service1.controllerX.MethodX: service1.serviceY.MethodY: service2.controllerX.MethodX: service2.serviceY.MethodY: service2.repositoryY.MethodY: error 9009 in database" code=9009 correlationID=cgva1dg3lcjadv69n180 service=gw status=500

I really have a doubt cause I am pretty sure I've read that some apps are only logging from the gateway but that would make the correlationID that I see everywhere completely useless. On the other hands, I see that error in go should be wrapped for better tracking of the execution path so I'm a bit lost ... Any advice is gold to me.

Thank you so much.

v613 commented 10 months ago

Hi @BigBoulard Accourding to documentation you need to call .Msg("") or .Send() to dispose the *Event.

It should be mentioned that in this case (when the message is missing), the log will not contain the "message" field.

You can ommit the .Err() method and use only .Msg(err.Error())