KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > log4j > LogManager


1 /*
2  * Copyright 1999-2005 The Apache Software Foundation.
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.apache.log4j;
18
19 import org.apache.log4j.spi.LoggerRepository;
20 import org.apache.log4j.spi.LoggerFactory;
21 import org.apache.log4j.spi.RepositorySelector;
22 import org.apache.log4j.spi.DefaultRepositorySelector;
23 import org.apache.log4j.spi.RootLogger;
24 import org.apache.log4j.helpers.Loader;
25 import org.apache.log4j.helpers.OptionConverter;
26 import org.apache.log4j.helpers.LogLog;
27
28 import java.net.URL JavaDoc;
29 import java.net.MalformedURLException JavaDoc;
30
31
32 import java.util.Enumeration JavaDoc;
33
34 /**
35  * Use the <code>LogManager</code> class to retreive {@link Logger}
36  * instances or to operate on the current {@link
37  * LoggerRepository}. When the <code>LogManager</code> class is loaded
38  * into memory the default initalzation procedure is inititated. The
39  * default intialization procedure</a> is described in the <a
40  * HREF="../../../../manual.html#defaultInit">short log4j manual</a>.
41  *
42  * @author Ceki G&uuml;lc&uuml; */

43 public class LogManager {
44
45   /**
46    * @deprecated This variable is for internal use only. It will
47    * become package protected in future versions.
48    * */

49   static public final String JavaDoc DEFAULT_CONFIGURATION_FILE = "log4j.properties";
50   
51   static final String JavaDoc DEFAULT_XML_CONFIGURATION_FILE = "log4j.xml";
52    
53   /**
54    * @deprecated This variable is for internal use only. It will
55    * become private in future versions.
56    * */

57   static final public String JavaDoc DEFAULT_CONFIGURATION_KEY="log4j.configuration";
58
59   /**
60    * @deprecated This variable is for internal use only. It will
61    * become private in future versions.
62    * */

63   static final public String JavaDoc CONFIGURATOR_CLASS_KEY="log4j.configuratorClass";
64
65   /**
66   * @deprecated This variable is for internal use only. It will
67   * become private in future versions.
68   */

69   public static final String JavaDoc DEFAULT_INIT_OVERRIDE_KEY =
70                                                  "log4j.defaultInitOverride";
71
72
73   static private Object JavaDoc guard = null;
74   static private RepositorySelector repositorySelector;
75
76   static {
77     // By default we use a DefaultRepositorySelector which always returns 'h'.
78
Hierarchy h = new Hierarchy(new RootLogger((Level) Level.DEBUG));
79     repositorySelector = new DefaultRepositorySelector(h);
80
81     /** Search for the properties file log4j.properties in the CLASSPATH. */
82     String JavaDoc override =OptionConverter.getSystemProperty(DEFAULT_INIT_OVERRIDE_KEY,
83                                null);
84
85     // if there is no default init override, then get the resource
86
// specified by the user or the default config file.
87
if(override == null || "false".equalsIgnoreCase(override)) {
88
89       String JavaDoc configurationOptionStr = OptionConverter.getSystemProperty(
90                               DEFAULT_CONFIGURATION_KEY,
91                               null);
92
93       String JavaDoc configuratorClassName = OptionConverter.getSystemProperty(
94                                                    CONFIGURATOR_CLASS_KEY,
95                            null);
96
97       URL JavaDoc url = null;
98
99       // if the user has not specified the log4j.configuration
100
// property, we search first for the file "log4j.xml" and then
101
// "log4j.properties"
102
if(configurationOptionStr == null) {
103     url = Loader.getResource(DEFAULT_XML_CONFIGURATION_FILE);
104     if(url == null) {
105       url = Loader.getResource(DEFAULT_CONFIGURATION_FILE);
106     }
107       } else {
108     try {
109       url = new URL JavaDoc(configurationOptionStr);
110     } catch (MalformedURLException JavaDoc ex) {
111       // so, resource is not a URL:
112
// attempt to get the resource from the class path
113
url = Loader.getResource(configurationOptionStr);
114     }
115       }
116       
117       // If we have a non-null url, then delegate the rest of the
118
// configuration to the OptionConverter.selectAndConfigure
119
// method.
120
if(url != null) {
121     LogLog.debug("Using URL ["+url+"] for automatic log4j configuration.");
122     OptionConverter.selectAndConfigure(url, configuratorClassName,
123                        LogManager.getLoggerRepository());
124       } else {
125     LogLog.debug("Could not find resource: ["+configurationOptionStr+"].");
126       }
127     }
128   }
129
130   /**
131      Sets <code>LoggerFactory</code> but only if the correct
132      <em>guard</em> is passed as parameter.
133      
134      <p>Initally the guard is null. If the guard is
135      <code>null</code>, then invoking this method sets the logger
136      factory and the guard. Following invocations will throw a {@link
137      IllegalArgumentException}, unless the previously set
138      <code>guard</code> is passed as the second parameter.
139
140      <p>This allows a high-level component to set the {@link
141      RepositorySelector} used by the <code>LogManager</code>.
142      
143      <p>For example, when tomcat starts it will be able to install its
144      own repository selector. However, if and when Tomcat is embedded
145      within JBoss, then JBoss will install its own repository selector
146      and Tomcat will use the repository selector set by its container,
147      JBoss. */

148   static
149   public
150   void setRepositorySelector(RepositorySelector selector, Object JavaDoc guard)
151                                                  throws IllegalArgumentException JavaDoc {
152     if((LogManager.guard != null) && (LogManager.guard != guard)) {
153       throw new IllegalArgumentException JavaDoc(
154            "Attempted to reset the LoggerFactory without possessing the guard.");
155     }
156
157     if(selector == null) {
158       throw new IllegalArgumentException JavaDoc("RepositorySelector must be non-null.");
159     }
160
161     LogManager.guard = guard;
162     LogManager.repositorySelector = selector;
163   }
164
165   static
166   public
167   LoggerRepository getLoggerRepository() {
168     return repositorySelector.getLoggerRepository();
169   }
170
171   /**
172      Retrieve the appropriate root logger.
173    */

174   public
175   static
176   Logger getRootLogger() {
177      // Delegate the actual manufacturing of the logger to the logger repository.
178
return repositorySelector.getLoggerRepository().getRootLogger();
179   }
180
181   /**
182      Retrieve the appropriate {@link Logger} instance.
183   */

184   public
185   static
186   Logger getLogger(String JavaDoc name) {
187      // Delegate the actual manufacturing of the logger to the logger repository.
188
return repositorySelector.getLoggerRepository().getLogger(name);
189   }
190
191  /**
192      Retrieve the appropriate {@link Logger} instance.
193   */

194   public
195   static
196   Logger getLogger(Class JavaDoc clazz) {
197      // Delegate the actual manufacturing of the logger to the logger repository.
198
return repositorySelector.getLoggerRepository().getLogger(clazz.getName());
199   }
200
201
202   /**
203      Retrieve the appropriate {@link Logger} instance.
204   */

205   public
206   static
207   Logger getLogger(String JavaDoc name, LoggerFactory factory) {
208      // Delegate the actual manufacturing of the logger to the logger repository.
209
return repositorySelector.getLoggerRepository().getLogger(name, factory);
210   }
211
212   public
213   static
214   Logger exists(String JavaDoc name) {
215     return repositorySelector.getLoggerRepository().exists(name);
216   }
217
218   public
219   static
220   Enumeration JavaDoc getCurrentLoggers() {
221     return repositorySelector.getLoggerRepository().getCurrentLoggers();
222   }
223
224   public
225   static
226   void shutdown() {
227     repositorySelector.getLoggerRepository().shutdown();
228   }
229
230   public
231   static
232   void resetConfiguration() {
233     repositorySelector.getLoggerRepository().resetConfiguration();
234   }
235 }
236
237
Popular Tags