Details
-
Improvement
-
Resolution: Fixed
-
Major
-
1.2.3
-
None
Description
If logback.xml contains a FILE appenders, but the parent directories cannot be created then we get a runtime exception. Example:
18:49:04,076 |-ERROR in ch.qos.logback.core.rolling.RollingFileAppender[FILE] - openFile(null,true) call failed. java.io.FileNotFoundException: log/auction-house-cluster.2019-02-06.log (No such file or directory) at java.io.FileNotFoundException: log/auction-house-cluster.2019-02-06.log (No such file or directory) at java.base/java.io.FileOutputStream.open0(Native Method) at java.base/java.io.FileOutputStream.open(FileOutputStream.java:298) at java.base/java.io.FileOutputStream.<init>(FileOutputStream.java:237) at ch.qos.logback.core.recovery.ResilientFileOutputStream.<init>(ResilientFileOutputStream.java:26) at ch.qos.logback.core.FileAppender.openFile(FileAppender.java:204) at ch.qos.logback.core.FileAppender.start(FileAppender.java:127) at ch.qos.logback.core.rolling.RollingFileAppender.start(RollingFileAppender.java:100) at ch.qos.logback.core.joran.action.AppenderAction.end(AppenderAction.java:90) at ch.qos.logback.core.joran.spi.Interpreter.callEndAction(Interpreter.java:309) at ch.qos.logback.core.joran.spi.Interpreter.endElement(Interpreter.java:193) at ch.qos.logback.core.joran.spi.Interpreter.endElement(Interpreter.java:179) at ch.qos.logback.core.joran.spi.EventPlayer.play(EventPlayer.java:62) at ch.qos.logback.core.joran.GenericConfigurator.doConfigure(GenericConfigurator.java:165) at ch.qos.logback.core.joran.GenericConfigurator.doConfigure(GenericConfigurator.java:152) at ch.qos.logback.core.joran.GenericConfigurator.doConfigure(GenericConfigurator.java:110) at ch.qos.logback.core.joran.GenericConfigurator.doConfigure(GenericConfigurator.java:53) at ch.qos.logback.classic.util.ContextInitializer.configureByResource(ContextInitializer.java:75) at ch.qos.logback.classic.util.ContextInitializer.autoConfig(ContextInitializer.java:150) at org.slf4j.impl.StaticLoggerBinder.init(StaticLoggerBinder.java:84) at org.slf4j.impl.StaticLoggerBinder.<clinit>(StaticLoggerBinder.java:55) at org.slf4j.LoggerFactory.bind(LoggerFactory.java:150) at org.slf4j.LoggerFactory.performInitialization(LoggerFactory.java:124) at org.slf4j.LoggerFactory.getILoggerFactory(LoggerFactory.java:412) at org.slf4j.LoggerFactory.getLogger(LoggerFactory.java:357) at org.slf4j.LoggerFactory.getLogger(LoggerFactory.java:383) at com.company.Testcase.<clinit>(Testcase.java:16)
However, when we run under Docker we want to keep the appender definition in place but direct output to STDOUT instead. The problem is that Logback initializes the appender even when it isn't in use, consequently we get an error even when nothing is wrong. There are workarounds, such as conditional blocks, but reduce the readability of the configuration file.
Please avoid initializing unused appenders.