Structured / Combined Log with Log4J

I hope you help me.

I have a web service that should log transactions. Since there are many hits, the log file contains log statements that do not intersect / fragment.

I considered passing a StringBuilder instance through layers and adding statements to that instance, after which I return its contents once at the very end (the finally clause in the main controller) before returning the response to the client. This seems unnecessary, and I'm sure there is a cleaner way to do this with Log4J.

Can anyone shed some light on the problem?

thanks

+3
source share
5 answers

, Log4j. (org.apache.log4j.NDC) (org.apache.log4j.MDC). , , - , .

+8

, : .

, , , . () , . , ; , . , , , , , .

, , Log4J.

+1

, , . , , .

, , , , , - grep.

grep userId *.log

.

, . .

+1

- . , (, ) appender. appender instanceof , toString ( ).

SLF4J Marker . . . Javadoc ( , String msg) .

0

Another idea, instead of passing an instance of StringBuilder through layers, which can lead to a rather inconvenient API (additional method parameters), you could consider using the java.lang.ThreadLocalcurrent context for storage, which could be reset to log at the very end.

0
source

Source: https://habr.com/ru/post/1708636/


All Articles