watermill
watermill copied to clipboard
Log with fields on error in router
Hey. I have noticed that router does not pass fields of the message here:
if err != nil {
h.logger.Error("Handler returned error", err, nil)
msg.Nack()
return
}
What do you think about adding msgFields
to the Error
function there?
Also, I think it would be nice to add correlation ID to the logs by default. I know it's something that middleware can handle, but having it in logs by default would be really nice. Or redo logger interface, so it has access to the message metadata
What about exposing message.Context() to the log method? Would it be possible? This way we could inject the correlation ID (or anything else to the message's context) in the middleware and the logger implementation would use it to customise the logger fields.
Hey, I just found this thread evaluating possible ways handling an error on the router level. I do wonder if it make sense to have a "router-global error handler" (for the lack of better terms) which could fall back to the existing default behaviour. This way, you can be totally flexible how you want to handle the error in general; you could just send it to an error tracker instead of plain logging. Using a middleware for this feels odd as it needs to swallow the handler-error at the end to keep the semantics. Would you be open to have a PR for that @roblaszczak @m110 ? (sorry to hijack this thread @ivaaaan 😅 )