Pass event data in the event log using slf4j (and log4j)

Is there a way to pass additional information that applies only to the current log event with slf4j? Requesting information, such as user, IP address or application name, can be saved and obtained through the MDC. Later I can access this information in a breadboard or converter and should not parse the log message. If I use a JSON based format, I have another field with "appName":myApp, and if I register log4j in the plaint text, I can access it through a note%{appName}. Is there a way to achieve this with information that applies to only one log event? For example, I want to pass an exception identifier (even for exceptions that I do not own). The best, but still ugly solution is to pass the identifier to the MDC, register an exception, and then delete it. Does anyone know a better solution?

+4
source share
1 answer

If you do not need to use slf4j, and log4j is good enough, you can use custom messages with log4j: https://logging.apache.org/log4j/2.x/manual/messages.html p>

slf4j - Logger, ( , http://www.slf4j.org/xref/org/slf4j/impl/JDK14LoggerAdapter.html)

+6

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


All Articles