문제

Is there a way to pass further information which just applies to the current log event with slf4j? Request based information like user, ip address or application name can be stored and accessed via the MDC. Later I can access that information in a layout or a converter and dont have to parse the log message. If I use a JSON based format I have another field with "appName":myApp and if I log with log4j in plaint text I can access it via the %{appName} notation. Is there a way to achieve this with information which just applies to one log event? For example I want to pass an exception id(even for exceptions I do not own). The best but still ugly solution is to pass the id to the MDC, log the exception and delete it afterwards. Does anyone know a better solution?

도움이 되었습니까?

해결책

If you don't 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

If you want slf4j - you could implement your own Logger adapter which would treat any extra parameters appropriately (just like http://www.slf4j.org/xref/org/slf4j/impl/JDK14LoggerAdapter.html)

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top