Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Update logback configuratioin without redeploy

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.

like image 319
error1009 Avatar asked Oct 31 '12 14:10

error1009


1 Answers

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.

like image 167
Pierre-Henri Avatar answered Oct 13 '22 14:10

Pierre-Henri