View Javadoc

1   package org.ocltf.logging;
2   
3   import java.net.URL;
4   
5   import org.apache.log4j.BasicConfigurator;
6   import org.apache.log4j.xml.DOMConfigurator;
7   import org.ocltf.utils.ClassUtils;
8   import org.ocltf.utils.XmlResourceUtils;
9   
10  
11  /***
12   * Configures the Logger used with the MDA
13   * @author Chad Brandon 
14   */
15  public class Logger {
16  
17  	/***
18  	 * Configures logging for the MDA application
19  	 * from the the xml resource "log4j.xml" found within
20  	 * the same package as this class.     
21  	 */
22  	public static void configure() {
23          String methodName = "configure";
24  		String loggingConfiguration = 
25              ClassUtils.getPackageName(Logger.class) + ".log4j";
26  		URL url = XmlResourceUtils.getResource(loggingConfiguration);
27          if (url == null) {
28          	throw new RuntimeException(methodName
29          		+ " - could not find Logger configuration file '" 
30                  + loggingConfiguration + "'");
31          }
32  		configure(url);
33  	}
34  
35  	/***   
36  	 * Configures the Logger from the passed in logConfigurationXml 
37  	 * 
38  	 * @param logConfigurationXml
39  	 */
40  	protected static void configure(URL logConfigurationXml) {
41  		try {
42  			DOMConfigurator.configure(logConfigurationXml);
43  		} catch (Exception ex) {
44  			System.err.println(
45  				"Unable to initialize logging system with configuration file ("
46  					+ logConfigurationXml
47  					+ ") --> using basic configuration.");
48  			ex.printStackTrace();
49  			BasicConfigurator.configure();
50  		}
51  	}
52  
53  	/***
54  	 * Configures the Logger from the passed in logConfigurationXml
55  	 * 
56  	 * @param logConfigurationXml
57  	 */
58  	protected static void configure(String logConfigurationXml) {
59  
60  		try {
61  			URL url = new java.net.URL(logConfigurationXml);
62  			url.openStream().close(); // make sure URL is valid.
63  			configure(url);
64  		} catch (Exception ex) {
65  			System.err.println(
66  				"Unable to initialize logging system with config file ("
67  					+ logConfigurationXml
68  					+ ") --> using basic configuration.");
69  			ex.printStackTrace();
70  			BasicConfigurator.configure();
71  		}
72  	}
73  
74  }