Tuesday, 14 June 2011

Load a properties files and use one of its properties to initialize log4j DOMConfigurator

In sample code below, we assume the main application configuration (properties) file is /opt/system.properties and it contains property LOG4J_XML_PATH which holds the full path to log4j xml configuration file. Of course, the configuration file can all other parameters required by the application.

public class ConfigFileReader{ 

    private static Properties systemProperties; 
    static {
        try {
            systemProperties = new Properties();
            systemProperties.load(new FileInputStream("/opt/system.properties"));
            String log4jpath=systemProperties.getProperty("LOG4J_XML_PATH");
            DOMConfigurator.configure(log4jpath);
       } catch (Exception e) {
              e.printStackTrace();
              System.exit(1);
       }
    }
}

No comments:

Post a Comment