Skip to content

Instantly share code, notes, and snippets.

@danieldbower
Created March 15, 2012 15:53
Show Gist options
  • Save danieldbower/2044916 to your computer and use it in GitHub Desktop.
Save danieldbower/2044916 to your computer and use it in GitHub Desktop.
ExternalConfigLoader
package com.example.app.config.logging;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* Simple utility listener to load certain properties before Spring Starts up.
*
* Add this entry to your web.xml:
* <pre><listener>
<listener-class>com.example.app.config.logging.ExternalConfigLoaderContextListener</listener-class>
</listener></pre>
*
* @author daniel
*
*/
public class ExternalConfigLoaderContextListener implements ServletContextListener {
private static final Logger logger = LoggerFactory.getLogger(ExternalConfigLoaderContextListener.class);
@Override
public void contextInitialized(ServletContextEvent sce) {
String configLocation = sce.getServletContext().getInitParameter("CONFIGDIR");
if(configLocation == null){
configLocation = System.getenv("CONFIGDIR");
}
try{
new LogBackConfigLoader(configLocation + "logback.xml");
}catch(Exception e){
logger.error("Unable to read config file", e);
}
}
@Override
public void contextDestroyed(ServletContextEvent sce) {
}
}
package com.example.app.config.logging;
import java.io.File;
import java.io.IOException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import ch.qos.logback.classic.LoggerContext;
import ch.qos.logback.classic.joran.JoranConfigurator;
import ch.qos.logback.core.joran.spi.JoranException;
/**
* Simple Utility class for loading an external config file for logback
* @author daniel
*/
public class LogBackConfigLoader {
private Logger logger = LoggerFactory.getLogger(LogBackConfigLoader.class);
public LogBackConfigLoader(String externalConfigFileLocation) throws IOException, JoranException{
LoggerContext lc = (LoggerContext) LoggerFactory.getILoggerFactory();
File externalConfigFile = new File(externalConfigFileLocation);
if(!externalConfigFile.exists()){
throw new IOException("Logback External Config File Parameter does not reference a file that exists");
}else{
if(!externalConfigFile.isFile()){
throw new IOException("Logback External Config File Parameter exists, but does not reference a file");
}else{
if(!externalConfigFile.canRead()){
throw new IOException("Logback External Config File exists and is a file, but cannot be read.");
}else{
JoranConfigurator configurator = new JoranConfigurator();
configurator.setContext(lc);
lc.reset();
configurator.doConfigure(externalConfigFileLocation);
logger.info("Configured Logback with config file from: " + externalConfigFileLocation);
}
}
}
}
}
//Assumes a context.xml with the following contents:
/*<?xml version="1.0" encoding="UTF-8"?>
<Context>
<Parameter name="CONFIGDIR" value="/usr/local/etc/exampleApp/"
override="false" />
</Context>*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment