Re: [logback-user] logback initialization "à la" Spring

Joern Huxhorn jhuxhorn at bpinteractive.com
Thu Sep 27 17:13:56 CEST 2007


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.
>   





More information about the Logback-user mailing list