I have two contexts running in the same JVM and Tomcat instance, and I’d like to write to the same logging file using the logback RollingFileAppender. Is the logback RollingFileAppender synchronized to prevent any issues if I use the same file? I am concerned the rolling over of the files would not work correctly, as well as some logs getting overridden.
The logback docs mention a prudent mode, but this indicates a performance hit and only refers to logging to the same file from different JVMs. I will be running in the same JVM.
Here is an example of my logback.xml. I would like to have this in two separate WAR files for each context:
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<appender name="FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>${catalina.base}/logs/portal.log</file>
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<fileNamePattern>portal.%d.log</fileNamePattern>
<maxHistory>30</maxHistory>
</rollingPolicy>
<triggeringPolicy class="ch.qos.logback.core.rolling.SizeBasedTriggeringPolicy">
<maxFileSize>100MB</maxFileSize>
</triggeringPolicy>
<encoder>
<pattern>%d [%t] %-5p %c - %m%n</pattern>
</encoder>
</appender>
<root level="INFO">
<appender-ref ref="FILE" />
</root>
</configuration>
Looking at the source shows that the doAppend() method is synchronized. A comment in the rollover() method says it does not need to by synchronized since doAppend() is.
http://grepcode.com/file/repo1.maven.org/maven2/ch.qos.logback/logback-core/0.9.3/ch/qos/logback/core/AppenderBase.java#AppenderBase.doAppend%28java.lang.Object%29
http://grepcode.com/file/repo1.maven.org/maven2/ch.qos.logback/logback-core/0.9.3/ch/qos/logback/core/AppenderBase.java#AppenderBase.doAppend%28java.lang.Object%29
You could still run into issues, though.