[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 -->
          <!-- or whenever the file size reaches a size -->

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]">
  <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
  <log xmlns="[snip]">

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> {
    public void openFile(String fileName) throws IOException {
        File file = new File(fileName);
        if (file.exists()) {
            try {
            } catch (RolloverFailure rolloverFailure) {
                // ...

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.


More information about the Logback-user mailing list