Install this theme

Posts tagged: logback

Configuring Logback programatically on web application startup

Logback is a successor of Log4J and a wonderful logging library to use in Java Applications.
Read about the reasons to prefer Logback over Log4j on this page.
This post describes just a simple usecase configuring Logback programmatically on Web Application startup.

Why should I want programmatic Logback configuration? Default Logback initialization (see Logback Manual) steps are:

  • Logback tries to find a file called logback.groovy in the classpath.
  • If no such file is found, logback tries to find a file called logback-test.xml in the classpath.
  • If no such file is found, it checks for the file logback.xml in the classpath..
  • If neither file is found, logback configures itself automatically using the BasicConfigurator which will cause logging output to be directed to the console.


Sometimes however you may want to read the configuration located outside your web application (e.g. somewhere in the filesystem, JNDI, database,…). This can be done by implementing a custom ServletContextListener, that does the job.

Here some sample code:

public class LogbackInitializer implements ServletContextListener {

	private static final Logger logger = LoggerFactory
			.getLogger(LogbackInitializer.class);

	private static final String logbackConfigSysProp = "app.logback.config.location";

	@Override
	public void contextInitialized(ServletContextEvent sce) {

		// init "Java Util Logging" bridge to forward JUL messages to SLF4J/Logback
		SLF4JBridgeHandler.install();
		
		// here we reading system property specifing the location of the 
// logback application's configuration file String logbackConfigLocation = System.getProperty(logbackConfigSysProp); if (logbackConfigLocation == null || logbackConfigLocation.isEmpty()) { sce.getServletContext().log( "WARNING! No logback configuration found! Please configure '" + logbackConfigSysProp + "' system property!"); } else { LoggerContext context = (LoggerContext) LoggerFactory.getILoggerFactory(); try { JoranConfigurator configurator = new JoranConfigurator(); configurator.setContext(context); // Call context.reset() to clear any previous configuration, // e.g. default configuration. For multi-step configuration, // omit calling context.reset(). context.reset(); configurator.doConfigure(logbackConfigLocation); StatusPrinter.printInCaseOfErrorsOrWarnings(context); logger.info("Logback logger configured successfully..."); } catch (JoranException e) { sce.getServletContext().log("Error configuring logback!", e); } } } @Override public void contextDestroyed(ServletContextEvent sce) { logger.info("Destroying logback logger context..."); LoggerContext lc = (LoggerContext) LoggerFactory.getILoggerFactory(); lc.stop(); } }

Note! Logback already provides a pre-defined system property logback.configurationFile to specify the external location of the configuration file. However if you want to use application specific property name (in case of multiple web apps running in the same server instance) or provide a not file-based configuration (e.g. external URL, database…) this should be the best way to configure logback.