2

I'm migrating a legacy app to use logback, in the process I'm trying to keep all of the old functionality working in the same way. One thing the legacy app did was log to the console if the log file could not be written (due to lack of space, bad permissions, etc)

With logback it seems a StatusListener should handle this, I can use getOrigin to get the sifted appender, but I can't figure out how to get the logger associated with the origin appender. Is it possible?

logback.xml:

<statusListener class="com.example.LogStatusListener"/>

<appender name="CONSOLE" class="ch.qos.logback.core.ConsoleAppender">
  <encoder>
    <pattern>WARNING: %logger is only being logged to CONSOLE!%n%msg%n</pattern>
  </encoder>
</appender>

<appender name="FILE-SIMPLE" class="ch.qos.logback.classic.sift.SiftingAppender">
  <discriminator class="com.example.CustomDiscriminator"/>
  <sift>
    <appender name="FILE-${logfile}" class="ch.qos.logback.core.rolling.FileAppender">
      <file>${logfile}.log</file>
      <encoder>
        <pattern>%msg%n</pattern>
      </encoder>
    </appender>
  </sift>
</appender>

<logger name="debug">
  <appender-ref ref="FILE-SIMPLE" />
</logger>

Listener:

public class LogStatusListener implements StatusListener {
    @Override
    public void addStatusEvent(Status status) {
        if (status instanceof ErrorStatus) {
            System.err.println(status);
            if (status.getOrigin() instanceof Appender) {
                Appender origin = (Appender) status.getOrigin();
                if (!origin.isStarted()) {
                    // find the logger associated with origin, and add ConsoleAppender
                }
            }
        }
    }
}
case nelson
  • 3,537
  • 3
  • 30
  • 37

1 Answers1

2

Status messages can be generated by any components, not just appenders. A status message doesn't have a logger associated with it, so you can't get the logger for a status message. By the way, logback implements graceful recovery from I/O errors. For example, if the disk becomes full, logback will stop logging and will automatically start logging again when space becomes available on the disk. Note that the logging events occurring in the interim are lost.

Ceki
  • 26,753
  • 7
  • 62
  • 71
  • 1
    That's good to know, but what happens to the logs in the interim? Are they buffered until the it starts again? Because I'm using log-sift this appender error occurs when I try to send a log, how do I make sure that log goes somewhere and isn't lost? – case nelson May 29 '12 at 22:00
  • logging events occurring in the interim are lost. – Ceki May 29 '12 at 22:28