View Javadoc

1   package org.lsst.ccs.utilities.logging;
2   
3   import java.util.Iterator;
4   import java.util.Properties;
5   import org.apache.log4j.PropertyConfigurator;
6   import org.lsst.ccs.bootstrap.resources.BootstrapResourceUtils;
7   
8   /**
9    * Utility class to provide Log4j configuration.
10   * All log messages coming from underlying libraries will be redirected
11   * to the org.lsst.ccs.utilities.logging.JULAppender
12   * so that they are logged in CCS framework.
13   *
14   * @author turri
15   */
16  public abstract class Log4JConfiguration {
17  
18      private static boolean isInitialized = false;
19  
20      public static void initialize() {
21          if (!isInitialized) {
22              try {
23                  Properties defaultLog4JProperties = new Properties();
24                  defaultLog4JProperties.setProperty("log4j.rootLogger", "info, stdout");
25                  defaultLog4JProperties.setProperty("log4j.appender.julAppender", "org.lsst.ccs.utilities.logging.JULAppender");
26                  defaultLog4JProperties.setProperty("log4j.appender.stdout", "org.lsst.ccs.utilities.logging.JULAppender");
27                  defaultLog4JProperties.setProperty("log4j.appender.stdout.layout", "org.apache.log4j.PatternLayout");
28                  defaultLog4JProperties.setProperty("log4j.appender.julAppender.layout", "org.apache.log4j.PatternLayout");
29                  defaultLog4JProperties.setProperty("log4j.appender.stdout.layout.ConversionPattern", "%d{ISO8601} - %m%n");
30                  defaultLog4JProperties.setProperty("log4j.logger.org.springframework.aop", "error");
31                  defaultLog4JProperties.setProperty("log4j.logger.org.springframework", "error");
32  
33                  Properties externalProps = BootstrapResourceUtils.getBootstrapProperties("log4j");
34                  Iterator iter = BootstrapResourceUtils.getAllKeysInProperties(externalProps).iterator();
35                  while (iter.hasNext()) {
36                      String key = (String) iter.next();
37                      defaultLog4JProperties.put(key, externalProps.getProperty(key));
38                  }
39  
40                  PropertyConfigurator.configure(defaultLog4JProperties);
41                  isInitialized = true;
42              } catch (Exception e) {
43                  System.out.println("#### Cannot load Log4J properties file");
44              }
45          }
46      }
47  
48  }