[logback-user] RollingFileAppender for logs with headers
Chris Dolan
chris at chrisdolan.net
Wed Jun 15 19:00:54 CEST 2011
I use an XML-based Layout for one of my appenders. This Layout makes use
of the getFileHeader() to output "<?xml...?>" and
getPresentationHeader/Footer() methods to output
"<log><header>...</header>" and "</log>.
I want a daily rolling log that additionally rolls at a specific file
size. So:
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<!-- rollover daily, one year duration -->
<fileNamePattern>${log.out.dir}/Foo-${log.host}-%d{yyyy-MM-dd}.%i.xml.zip</fileNamePattern>
<maxHistory>365</maxHistory>
<timeBasedFileNamingAndTriggeringPolicy
class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
<!-- or whenever the file size reaches a size -->
<maxFileSize>5MB</maxFileSize>
</timeBasedFileNamingAndTriggeringPolicy>
</rollingPolicy>
The problem with this configuration is that on application reboot, I get
log files like this:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<log xmlns="[snip]">
<header>
<tz>America/Chicago</tz>
<tzoffset>-21600000</tzoffset>
</header>
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<log xmlns="[snip]">
<header>
<tz>America/Chicago</tz>
<tzoffset>-21600000</tzoffset>
</header>
That is, the new run emits another header appended into the existing log
file. I've tried to work around it by making a subclassed appender that
looks like the following, but it fails with a NullPointerException because
the rolling filename hasn't been set up yet...
public class RollingXmlFileAppender<E> extends RollingFileAppender<E> {
@Override
public void openFile(String fileName) throws IOException {
File file = new File(fileName);
if (file.exists()) {
try {
getRollingPolicy().rollover();
} catch (RolloverFailure rolloverFailure) {
// ...
}
}
super.openFile(fileName);
}
}
Surely other people use log files with headers? How do you roll those
files? It would seem that the trigger should always be checked on start().
If so, I could probably make a TriggeringPolicy that always returns true
on its first invocation.
Chris
More information about the Logback-user
mailing list