Update logback configuratioin without redeploy

error1009 picture error1009 · Oct 31, 2012 · Viewed 7.7k times · Source

Idea is to make an ability to change logback configuration without redeploy. Slf4j and logback are used in project. logback.xml file is in ear, but it reads some properties from property file, which is placed out of ear. Something like that:

<configuration scan="true" scanPeriod="5 seconds">
<property file="${logconfig}"/>

<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender"> 
    <encoder>
        <pattern>${logback.consolePattern}</pattern>
    </encoder>
</appender>
<root level="DEBUG">
    <appender-ref ref="STDOUT" />
</root>

</configuration>

Problem is that scan checks if logback.xml was changed (and file always the same). That's why changing values in property file doesn't changes configuration of logback. Changes are applied only after redeploy.

So what is the best way to have an ability to modify logback configuration without redeploy? Is there some mechanism that allow to realize it?

upd: changes would be made very rarely. but they should be applied as soon as possible. performance is also important.

Answer

Pierre-Henri picture Pierre-Henri · Oct 31, 2012

I manage to reload it by doing this :

LoggerContext loggerContext = (LoggerContext) LoggerFactory.getILoggerFactory();
loggerContext.reset();
ContextInitializer ci = new ContextInitializer(loggerContext);
ci.autoConfig();

In my use case, I do that to add some properties to the context by doing :

loggerContext.putProperty("logDirectory", getLogDirectory().getAbsolutePath());

before the autoConfig.

Reading properties from the properties file should work too.