Reputation: 3657
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
}
}
}
}
}
Upvotes: 2
Views: 1646
Reputation: 27435
Status messages can be generated by any logback 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.
Upvotes: 2