Is the following logback setup safe and good practice.
I have Multiple WARs (deployed WebSphere 8.5.5) and want them to share a single logback.xml
-Dlogback.configurationFile=/opt/logback.xml -Dlogback.ContextSelector=JNDI
The logback.xml
uses a SiftingAppender
with JNDIBasedContextDiscriminator
so each WAR gets its own log file.
<appender name="SIFT" class="ch.qos.logback.classic.sift.SiftingAppender">
<discriminator class="ch.qos.logback.classic.sift.JNDIBasedContextDiscriminator">
<defaultValue>unknown</defaultValue>
</discriminator>
<sift>
<appender name="FILE-${contextName}" class="ch.qos.logback.core.FileAppender">
<file>/var/log/${contextName}.log</file>
<encoder>
<pattern>%-50(%level %logger{35}) cn=%contextName - %msg%n</pattern>
</encoder>
</appender>
</sift>
</appender>
Each WAR web.xml
will have contextName
:
<env-entry>
<description>JNDI logging context for this app</description>
<env-entry-name>logback/context-name</env-entry-name>
<env-entry-type>java.lang.String</env-entry-type>
<env-entry-value>ContextNameWebAppA</env-entry-value>
</env-entry>
I don't know if using the Jndi discriminator is safe or a good practice, but it seems to be the way Logback solves this issue : http://logback.qos.ch/manual/loggingSeparation.html
They indicate that the performance can be better in adding this to your configuration :
On an other hand, I can share what I'm trying to do to avoid setting the system properties
logback.ContextSelector=JNDI
.I use instead the
MDCBasedDiscriminator
which will get the discriminating value defined withMDC.put(key,value)
.The MDC map is available as a thread local variable, so it must be set for every thread initiated by the web server.
For this initialisation I used a
javax.servlet.Filter
placed before other filters, this filter will put the correct value in MDC.I don't think this is better than what you did, but it's an alternative to the JNDI property, the problem is that the shutting down log are in the
unknown.log
.Here is some code :
And the logback file :
And the registration can be for example in a spring security initializer :