storm_buster
storm_buster

Reputation: 7568

How to identify or define a unique logging id per request?

I am adding logs to my rest application in which there are several layers (controller, service, dao ...).

How to identify or define a unique logging id per request so that i could use that same log Id in different layer for the one request? (without creating it in the controller and passing it to each layer)

FYI: I am using log4j

thanks!

Upvotes: 1

Views: 774

Answers (1)

Dave Newton
Dave Newton

Reputation: 160301

Without knowing what logging framework you're using it's difficult to provide specific advice.

I've usually used something like Log4J's NDC or MDC for this: the request comes in, gets tagged with relevant information in the diagnostic context, and a log formatter spits out that diagnostic context in a way meaningful to your application.

I've also generally used a custom message formatter in order to make sure that the DC gets logged consistently, for later use by log file consumers. If it's going to a DB/message queue/etc. it might not matter as much because the context can often be dumped as-is and processed accordingly.

Upvotes: 1

Related Questions