Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

structured/combined logging with Log4J

I hope you can help me.

I have a web service that needs to log transactions. Since there are many hits, the log statements appears disjoint/fragmented in the log file.

I've considered passing a StringBuilder instance through-out the layers and appending the statements to this instance, then I log its contents once at the very end (finally clause in the main controller) before returning a response to the client. This seems unnecessary and I'm sure there's a cleaner way to do it with Log4J.

Can anyone shed light on the issue?

Thanks

like image 750
opyate Avatar asked Dec 22 '22 11:12

opyate


1 Answers

the good things is that you are using Log4j. The library itself implements the concept of nested diagnostic context (org.apache.log4j.NDC) and mapped diagnostic context (org.apache.log4j.MDC). I reckon you should definitely have a look at both, because those allows you to build some sort of the context that you can use in your log output layout.

like image 200
Tomasz Błachowicz Avatar answered Dec 28 '22 08:12

Tomasz Błachowicz