View Javadoc

1   /*
2    * Copyright 2013 The Kuali Foundation
3    * 
4    * Licensed under the Educational Community License, Version 1.0 (the
5    * "License"); you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    * 
8    * http://www.opensource.org/licenses/ecl1.php
9    * 
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12   * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13   * License for the specific language governing permissions and limitations under
14   * the License.
15   */
16  package org.kuali.student.common.test.spring.log4j;
17  
18  import org.mortbay.log.Log;
19  import org.slf4j.Logger;
20  import org.slf4j.LoggerFactory;
21  import org.springframework.util.Log4jConfigurer;
22  
23  import java.io.FileNotFoundException;
24  
25  /**
26   * 
27   * When used within a <b>static</b> code block in a unit test it allows the
28   * developer to override the log4j.properties used when executing the test.
29   * 
30   * This is useful because for performance reasons running tests in jenkins use
31   * the <b>WARN</b> log level. But when developing a test seeing <b>DEBUG</b>
32   * level events is very useful.
33   * 
34   * @author Kuali Student Team
35   * 
36   */
37  public final class KSLog4JConfigurer {
38  
39      /**
40       * Default name for the refresh value property key
41       */
42      public static final String LOG4J_PROPERTIES_REFRESH = "log4j.properties.refresh";
43      
44      /**
45       * Default name for the log4j.properties property key
46       */
47      public static final String LOG4J_PROPERTIES = "log4j.properties";
48  
49      private KSLog4JConfigurer() {
50          // intentionally private
51      }
52  
53      /**
54       * Initialize the org.slf4j.Logger for the given class.
55       * 
56       * If a System Property of <em>log4j.properties</em> exists that file will be used to configure <b>log4j</b>
57       * 
58       * @param clazz the class to configure the logger for.
59       * 
60       * @return the initialized org.slf4j.Logger for the class specified.
61       */
62      public static final Logger getLogger(Class<?> clazz) {
63          
64          return getLogger(clazz, LOG4J_PROPERTIES, LOG4J_PROPERTIES_REFRESH);
65      }
66  
67      /**
68       * Initialize the org.slf4j.Logger for the given class.
69       * 
70       * If a System Property of the name given by the systemPropertyName parameter exists that file will be used to configure <b>log4j</b>
71       * 
72       * @param clazz the class to configure the logger for.
73       * @param systemPropertyName The name of the system property to look for to contain the absolute path to the alternate log4j.properties file.
74       * @return the initialized org.slf4j.Logger for the class specified.
75       */
76      public static final Logger getLogger(Class<?> clazz,
77              String systemPropertyName, String refreshPropertyName) {
78  
79          String log4jconfig = System.getProperty(systemPropertyName);
80          
81          Long refreshInterval;
82          
83          try {
84              refreshInterval = Long.valueOf(System.getProperty(refreshPropertyName));
85          } catch (NumberFormatException e1) {
86              refreshInterval = null;
87          }
88  
89          boolean exception = false;
90  
91          if (log4jconfig != null) {
92              try {
93                  
94                  if (refreshInterval != null) {
95                      Log4jConfigurer.initLogging(log4jconfig, refreshInterval);
96                  }
97                  else {
98                      Log4jConfigurer.initLogging(log4jconfig);
99                  }
100                 
101             } catch (FileNotFoundException e) {
102 
103                 exception = true;
104             }
105         }
106 
107         Logger log = LoggerFactory.getLogger(clazz);
108 
109         if (log.isDebugEnabled()) {
110             
111             if (refreshInterval != null) 
112                 log.debug(String.format("Log4jConfigurer.initLogging(log4jconfig=%s, refreshInterval=%s)", log4jconfig, refreshInterval));
113             else
114                 log.debug(String.format("Log4jConfigurer.initLogging(log4jconfig=%s)", log4jconfig));
115                 
116         }
117         
118         if (exception)
119             log.error("Missing Configuration File: " + log4jconfig
120                     + ", For system property: " + systemPropertyName);
121         
122         return log;
123 
124     }
125 
126 }