Printing context root in loggers Log4j
I want to print loggers along with context root.
For example my loggers printing in console as below.
INFO [MandatoryAdapter] Ends - validateData
开发者_运维百科
Now I want as below.
INFO [APPLICATION_NAME] [MandatoryAdapter] Ends - validateData
Can any one help me on this regard.
Thanks,
Narendra
Assuming you have multiple apps, and each app has it's own log4j.properties file
we do something like this i.e. hardcode the web app name in the ConversionPattern
log4j.appender.LOGFILE.layout=org.apache.log4j.PatternLayout
log4j.appender.LOGFILE.layout.ConversionPattern=%d{yyyy-MM-dd HH\:mm\:ss.SSS} <business> [-] %-5p %m \: %c-(%L) %n
Here <business>
is the app name
Further Reading
log4j properties for multiple webapps
I suggest using Log4j's MDC (Mapped Diagnostic Context, see docs) for this.
Essentially, your application sets the MDC thread-local variable to your application name, and the log4j logger then prints that out with each message.
If this is for a webapp, then you can set the MDC at the start of the request, and unset it when it finishes.
精彩评论