KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > info > magnolia > logging > Log4jConfigurer


1 package info.magnolia.logging;
2
3 import info.magnolia.cms.core.SystemProperty;
4 import info.magnolia.cms.util.ConfigUtil;
5
6 import java.io.IOException JavaDoc;
7 import java.io.InputStream JavaDoc;
8 import java.util.HashMap JavaDoc;
9 import java.util.Map JavaDoc;
10 import java.util.Properties JavaDoc;
11
12 import javax.servlet.ServletContext JavaDoc;
13
14 import org.apache.commons.io.IOUtils;
15 import org.apache.commons.lang.StringUtils;
16 import org.apache.log4j.LogManager;
17 import org.apache.log4j.PropertyConfigurator;
18 import org.apache.log4j.xml.DOMConfigurator;
19 import org.w3c.dom.Document JavaDoc;
20
21
22 /**
23  * <p>
24  * Log4j initializer. Loads the file specified using the <code>log4j.config</code> init parameter and optionally set a
25  * system property containing the magnolia web application root directory with the name specified by the
26  * <code>magnolia.root.sysproperty</code> init parameter.
27  * </p>
28  * <p>
29  * If <code>magnolia.root.sysproperty</code> is empty no system variable will be set; if <code>log4j.config</code>
30  * is empty no log4j initialization will be performed.
31  * </p>
32  * <p>
33  * You can easily specify relative paths for log4j configuration files using the magnolia root system property, for
34  * example using <code>${magnolia.root}logs/magnolia-debug.log</code>
35  * </p>
36  * <p>
37  * Note: if you drop multiple magnolia wars in a container which doesn't isolate system properties (e.g. tomcat) you
38  * could need to change the name of the <code>magnolia.root.sysproperty</code> variable in web.xml and in log4j
39  * configuration files.
40  * </p>
41  * <p>
42  * <em>Some ideas and snippets borrowed from the more complex Spring implementation http://www.springframework.org</em>
43  * </p>
44  * @author Fabrizio Giustina
45  * @version $Id: Log4jConfigurer.java 6341 2006-09-12 09:18:27Z philipp $
46  */

47 public abstract class Log4jConfigurer {
48
49     /**
50      * Init parameter specifying the location of the Log4J config file
51      */

52     public static final String JavaDoc LOG4J_CONFIG = "log4j.config"; //$NON-NLS-1$
53

54     /**
55      * Utility class, don't instantiate.
56      */

57     private Log4jConfigurer() {
58         // unused
59
}
60
61     /**
62      * Initialize Log4J, including setting the web app root system property.
63      * @param servletContext ServletContext
64      * @param parameters parameter map, containing the <code>MAGNOLIA_ROOT_SYSPROPERTY</code> and
65      * <code>LOG4J_CONFIG</code> properties
66      */

67     public static void initLogging(ServletContext JavaDoc servletContext) {
68
69         // can't use log4j yet
70
log("Initializing Log4J"); //$NON-NLS-1$
71

72         String JavaDoc log4jFileName = (String JavaDoc) SystemProperty.getProperty(LOG4J_CONFIG);
73         if (StringUtils.isNotEmpty(log4jFileName)) {
74             boolean isXml = log4jFileName.toLowerCase().endsWith(".xml"); //$NON-NLS-1$
75

76             log("Initializing Log4J from [" + log4jFileName + "]"); //$NON-NLS-1$ //$NON-NLS-2$
77

78             InputStream JavaDoc stream = ConfigUtil.getConfigFile(log4jFileName);
79             
80             String JavaDoc config;
81             try {
82                 config = ConfigUtil.replaceTokens(stream);
83             }
84             catch (IOException JavaDoc e) {
85                 log("Unable to initialize Log4J from [" //$NON-NLS-1$
86
+ log4jFileName
87                     + "], got a IOException " //$NON-NLS-1$
88
+ e.getMessage());
89                 return;
90             }
91
92             // classpath?
93
if (isXml) {
94                 Document JavaDoc document;
95                 try {
96                     Map JavaDoc dtds = new HashMap JavaDoc();
97                     dtds.put("log4j.dtd", "/org/apache/log4j/xml/log4j.dtd");
98                     document = ConfigUtil.string2DOM(config, dtds);
99                 }
100                 catch (Exception JavaDoc e) {
101                     log("Unable to initialize Log4J from [" //$NON-NLS-1$
102
+ log4jFileName
103                         + "], got an Exception during reading the xml file " //$NON-NLS-1$
104
+ e.getMessage());
105                     return;
106                 }
107                 DOMConfigurator.configure(document.getDocumentElement());
108             }
109             else {
110                 Properties JavaDoc properties = new Properties JavaDoc();
111                 try {
112                     properties.load(IOUtils.toInputStream(config));
113                 }
114                 catch (IOException JavaDoc e) {
115                     log("Unable to initialize Log4J from [" //$NON-NLS-1$
116
+ log4jFileName
117                         + "], got an Exception during reading the properties file " //$NON-NLS-1$
118
+ e.getMessage());
119                     return;
120                 }
121                 PropertyConfigurator.configure(log4jFileName);
122             }
123
124         }
125     }
126
127     /**
128      * Shut down Log4J, properly releasing all file locks and resetting the web app root system property.
129      * @param parameters parameter map, containing the <code>LOG4J_CONFIG</code> property
130      */

131     public static void shutdownLogging(Map JavaDoc parameters) {
132         log("Shutting down Log4J"); //$NON-NLS-1$
133
try {
134             LogManager.shutdown();
135         }
136         finally {
137             // Remove the web app root system property.
138
String JavaDoc param = (String JavaDoc) parameters.get(SystemProperty.MAGNOLIA_ROOT_SYSPROPERTY);
139             if (StringUtils.isNotEmpty(param)) {
140                 System.getProperties().remove(param);
141             }
142         }
143     }
144
145     /**
146      * Handy System.out method to use when logging isn't configured yet.
147      * @param message log message
148      */

149     public static void log(String JavaDoc message) {
150         System.out.println(message);
151     }
152
153 }
154
Popular Tags