logback initialization "à la" Spring

Hi, I'm thinking about moving to logback after a life happily spent using log4j: nevertheless I'm having problems understanding if there's a way to initialize it in a similar way as we did with log4j. What I do not want to do is put a configuration file under the WEB-INF/classes dir in my webapp: all of our configuration files are stored under WEB-INF/cfg, and we want to continue this way. I see that logback has a way to define contexts through jndi, but we do not want to ask our system administrators to change the config of our weblogic clusters just because we think about introducing a new library: is there another way? What I'd like to obtain is something like we did with Spring's Log4jConfigListener: simply define into the web.xml a listener which configures the logging subsystem at startup time using the xml file indicated by a servlet context parameter. Is there something like that? I was thinking about writing a class by myself, but I see no way to do it without triggering the BasicConfigurator part in the static initialize method of StaticLoggerBinder. Thank you, Davide Baroncelli P.s.: let me express my disagreement for logback and sl4j not supporting neither the FATAL logging level nor automatic reloading of config files. -- View this message in context: http://www.nabble.com/logback-initialization-%22%C3%A0-la%22-Spring-tf452797... Sent from the Logback User mailing list archive at Nabble.com.

Hi Davide. It's possible to change the logging configuration at runtime with code like this: ILoggerFactory loggerFactory = LoggerFactory.getILoggerFactory(); if (loggerFactory instanceof LoggerContext) { LoggerContext loggerContext = (LoggerContext) LoggerFactory.getILoggerFactory(); if (verbose) { // reset previous configuration initially loaded from logback.xml loggerContext.shutdownAndReset(); JoranConfigurator configurator = new JoranConfigurator(); configurator.setContext(loggerContext); URL configUrl; configUrl = getClass().getResource("/logbackVerbose.xml"); try { configurator.doConfigure(configUrl); if (logger.isDebugEnabled()) logger.debug("Configured logging with {}.", configUrl); StatusPrinter.print(loggerContext); } catch (JoranException ex) { if (logger.isErrorEnabled()) logger.error("Error configuring logging framework!", ex); StatusPrinter.print(loggerContext); } } } The instanceof and cast are only needed because we are using logback via slf4j. I don't think this is exactly what you are looking for but it should help you writing your own class, at least. I've never used Log4jConfigListener myself so I don't know how it works but doesn't the configuration of the logging environment during normal spring-initializations mean that at least some spring-log-events are swallowed? If this is not the case then everything should be fine. I hope this helps. Joern. Davide Baroncelli wrote:
Hi, I'm thinking about moving to logback after a life happily spent using log4j: nevertheless I'm having problems understanding if there's a way to initialize it in a similar way as we did with log4j.
What I do not want to do is put a configuration file under the WEB-INF/classes dir in my webapp: all of our configuration files are stored under WEB-INF/cfg, and we want to continue this way.
I see that logback has a way to define contexts through jndi, but we do not want to ask our system administrators to change the config of our weblogic clusters just because we think about introducing a new library: is there another way?
What I'd like to obtain is something like we did with Spring's Log4jConfigListener: simply define into the web.xml a listener which configures the logging subsystem at startup time using the xml file indicated by a servlet context parameter. Is there something like that? I was thinking about writing a class by myself, but I see no way to do it without triggering the BasicConfigurator part in the static initialize method of StaticLoggerBinder.
Thank you, Davide Baroncelli
P.s.: let me express my disagreement for logback and sl4j not supporting neither the FATAL logging level nor automatic reloading of config files.

Joern Huxhorn wrote:
Hi Davide.
It's possible to change the logging configuration at runtime with code like this: ILoggerFactory loggerFactory = LoggerFactory.getILoggerFactory();
Hi, thank you very much for your help: I finally managed to write something very similar to what's in Spring. You can find it here ( http://www.nabble.com/file/p12937453/logback-web-config.jar here ) in case someone should be interested. I finally resorted to using ContextInitializer.configureByResource( loggerContext, url ); which spares some lines of code. Joern Huxhorn wrote:
I've never used Log4jConfigListener myself so I don't know how it works but doesn't the configuration of the logging environment during normal spring-initializations mean that at least some spring-log-events are swallowed? If this is not the case then everything should be fine.
No, it does not because this is "in Spring" but only from a library perspective (i.e. you don't need a running application context in order to use it): it gets configured as a listener or a servlet with init order = 0, so it is initialized before anything else. The log4jconfiglistener and servlet in Spring allow configuring also the auto-reload (turn it on-off and reloading time): since there is no autoreload in logback I stripped this out of my listener. -- View this message in context: http://www.nabble.com/logback-initialization-%22%C3%A0-la%22-Spring-tf452797... Sent from the Logback User mailing list archive at Nabble.com.

Davide Baroncelli wrote:
P.s.: let me express my disagreement for logback and sl4j not supporting neither the FATAL logging level nor automatic reloading of config files.
Ok, I developed a small Spring runnable component useful for auto-reloading of logback configs. It may be configured in an application context like this: <bean id="logbackReconfigurer" class="org.springframework.scheduling.concurrent.ScheduledExecutorFactoryBean"> <property name="scheduledExecutorTasks"> <list> <bean class="org.springframework.scheduling.concurrent.ScheduledExecutorTask"> <property name="period" value="5000"/> <property name="runnable"> <bean class="it.stratosfera.commons.logback.LogbackConfigurationReloaderRunnable"> <property name="configResource" value="/WEB-INF/carcon-logback.xml"/> </bean> </property> </bean> </list> </property> </bean> should anyone be interested, I re-attach a package with the updated classes, http://www.nabble.com/file/p12942829/logback-auto-reloading.jar here . -- View this message in context: http://www.nabble.com/logback-initialization-%22%C3%A0-la%22-Spring-tf452797... Sent from the Logback User mailing list archive at Nabble.com.
participants (3)
-
Davide Baroncelli
-
Joern Huxhorn
-
Jorg Heymans