watermill icon indicating copy to clipboard operation
watermill copied to clipboard

Log with fields on error in router

Open ivaaaan opened this issue 1 year ago • 3 comments

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?

ivaaaan avatar Nov 08 '23 10:11 ivaaaan

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

ivaaaan avatar Nov 08 '23 11:11 ivaaaan

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.

arthurspa avatar Nov 20 '23 23:11 arthurspa

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 😅 )

bastiankoetsier avatar Mar 05 '24 07:03 bastiankoetsier