[logback-user] Disable scanning of logback.xml during initialization

Simon Wong simonwg at gmail.com
Thu Apr 17 04:44:39 CEST 2014


Hi,

  I am using logback with Spring logback extension. The exact log location
is determined at runtime through the "webAppRootKey" context param, that
mentioned in WebLogbackConfigurer

  In web.xml,

    <context-param>
        <param-name>webAppRootKey</param-name>
        <param-value>myApp.root</param-value>
    </context-param>


  In logback.xml,
    <appender name="FILE"
class="ch.qos.logback.core.rolling.RollingFileAppender">
        <rollingPolicy
            class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">

<FileNamePattern>${myApp.root}/../../logs/${FILE_NAME_PREFIX}.%d.log.zip</FileNamePattern>
        </rollingPolicy>
        <encoder>
            <pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{32} -
%msg%n</pattern>
        </encoder>
    </appender>


  The issue is during logback initialization, it will scan the logback.xml
automatically but the ${myApp.root} is not yet resolved, due to Spring Web
Application Context is initialized lazily. The log entry could not write to
log file successfully.

  The workaround solution is the program "touch" the logback.xml again
during @PostConstruct phase after Spring Web Application Context is
constructed. Logback will scan the change of logback.xml some time later
and at that time the ${myApp.root} is correctly resolved.

e.g.

    @PostConstruct
    void init() {
        try {
            ClassPathResource logbackResource = new
ClassPathResource(logback.xml);
            File logbackConfigFile = logbackResource.getFile();
            FileUtils.touch(logbackConfigFile);
        } catch (IOException ioe) {
            throw new RuntimeException(ioe);
        }
    }

   Is there a way that I could disable the auto scanning of logback.xml
during initialization? Or defer
until ch.qos.logback.ext.spring.web.LogbackConfigListener is well
configured?
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mailman.qos.ch/pipermail/logback-user/attachments/20140417/05085f52/attachment.html>


More information about the Logback-user mailing list