KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > springframework > util > Log4jConfigurer


1 /*
2  * Copyright 2002-2005 the original author or authors.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * 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.apache.org/licenses/LICENSE-2.0
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,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16
17 package org.springframework.util;
18
19 import java.io.File JavaDoc;
20 import java.io.FileNotFoundException JavaDoc;
21 import java.net.URL JavaDoc;
22
23 import org.apache.log4j.LogManager;
24 import org.apache.log4j.PropertyConfigurator;
25 import org.apache.log4j.xml.DOMConfigurator;
26
27 /**
28  * Convenience class that features simple methods for custom Log4J configuration.
29  *
30  * <p>Only needed for non-default Log4J initialization, for example with a custom
31  * config location or a refresh interval. By default, Log4J will simply read its
32  * configuration from a "log4j.properties" file in the root of the class path.
33  *
34  * <p>For web environments, the analogous Log4jWebConfigurer class can be found
35  * in the web package, reading in its configuration from context-params in web.xml.
36  * In a J2EE web application, Log4J is usually set up via Log4jConfigListener or
37  * Log4jConfigServlet, delegating to Log4jWebConfigurer underneath.
38  *
39  * @author Juergen Hoeller
40  * @since 13.03.2003
41  * @see org.springframework.web.util.Log4jWebConfigurer
42  * @see org.springframework.web.util.Log4jConfigListener
43  * @see org.springframework.web.util.Log4jConfigServlet
44  */

45 public abstract class Log4jConfigurer {
46
47     /** Pseudo URL prefix for loading from the class path: "classpath:" */
48     public static final String JavaDoc CLASSPATH_URL_PREFIX = "classpath:";
49
50     /** Extension that indicates a Log4J XML config file: ".xml" */
51     public static final String JavaDoc XML_FILE_EXTENSION = ".xml";
52
53
54     /**
55      * Initialize Log4J from the given file location, with no config file refreshing.
56      * Assumes an XML file in case of a ".xml" file extension, and a properties file else.
57      * @param location the location of the config file: either a "classpath:" location
58      * (e.g. "classpath:myLog4j.properties"), an absolute file URL
59      * (e.g. "file:C:/log4j.properties), or a plain absolute path in the file system
60      * (e.g. "C:/log4j.properties")
61      * @throws FileNotFoundException if the location specifies an invalid file path
62      */

63     public static void initLogging(String JavaDoc location) throws FileNotFoundException JavaDoc {
64         String JavaDoc resolvedLocation = SystemPropertyUtils.resolvePlaceholders(location);
65         URL JavaDoc url = ResourceUtils.getURL(resolvedLocation);
66         if (resolvedLocation.toLowerCase().endsWith(XML_FILE_EXTENSION)) {
67             DOMConfigurator.configure(url);
68         }
69         else {
70             PropertyConfigurator.configure(url);
71         }
72     }
73
74     /**
75      * Initialize Log4J from the given location, with the given refresh interval
76      * for the config file. Assumes an XML file in case of a ".xml" file extension,
77      * and a properties file else.
78      * <p>Log4J's watchdog thread will asynchronously check whether the timestamp
79      * of the config file has changed, using the given interval between checks.
80      * A refresh interval of 1000 milliseconds (one second), which allows to
81      * do on-demand log level changes with immediate effect, is not unfeasible.
82      * <p><b>WARNING:</b> Log4J's watchdog thread does not terminate until VM shutdown;
83      * in particular, it does not terminate on LogManager shutdown. Therefore, it is
84      * recommended to <i>not</i> use config file refreshing in a production J2EE
85      * environment; the watchdog thread would not stop on application shutdown there.
86      * @param location the location of the config file: either a "classpath:" location
87      * (e.g. "classpath:myLog4j.properties"), an absolute file URL
88      * (e.g. "file:C:/log4j.properties), or a plain absolute path in the file system
89      * (e.g. "C:/log4j.properties")
90      * @param refreshInterval interval between config file refresh checks, in milliseconds
91      * @throws FileNotFoundException if the location specifies an invalid file path
92      */

93     public static void initLogging(String JavaDoc location, long refreshInterval) throws FileNotFoundException JavaDoc {
94         String JavaDoc resolvedLocation = SystemPropertyUtils.resolvePlaceholders(location);
95         File JavaDoc file = ResourceUtils.getFile(resolvedLocation);
96         if (!file.exists()) {
97             throw new FileNotFoundException JavaDoc("Log4J config file [" + resolvedLocation + "] not found");
98         }
99         if (resolvedLocation.toLowerCase().endsWith(XML_FILE_EXTENSION)) {
100             DOMConfigurator.configureAndWatch(file.getAbsolutePath(), refreshInterval);
101         }
102         else {
103             PropertyConfigurator.configureAndWatch(file.getAbsolutePath(), refreshInterval);
104         }
105     }
106
107     /**
108      * Shut down Log4J, properly releasing all file locks.
109      * <p>This isn't strictly necessary, but recommended for shutting down
110      * Log4J in a scenario where the host VM stays alive (for example, when
111      * shutting down an application in a J2EE environment).
112      */

113     public static void shutdownLogging() {
114         LogManager.shutdown();
115     }
116
117     /**
118      * Set the specified system property to the current working directory.
119      * <p>This can be used e.g. for test environments, for applications that leverage
120      * Log4jWebConfigurer's "webAppRootKey" support in a web environment.
121      * @param key system property key to use, as expected in Log4j configuration
122      * (for example: "demo.root", used as "${demo.root}/WEB-INF/demo.log")
123      * @see org.springframework.web.util.Log4jWebConfigurer
124      */

125     public static void setWorkingDirSystemProperty(String JavaDoc key) {
126         System.setProperty(key, new File JavaDoc("").getAbsolutePath());
127     }
128
129 }
130
Popular Tags