001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017:
018: package org.apache.juli.logging;
019:
020: import java.util.Properties;
021:
022: /**
023: * Modified LogFactory: removed all discovery, hardcode a specific implementation
024: * If you like a different logging implementation - use either the discovery-based
025: * commons-logging, or better - another implementation hardcoded to your favourite
026: * logging impl.
027: *
028: * Why ? Each application and deployment can choose a logging implementation -
029: * that involves configuration, installing the logger jar and optional plugins, etc.
030: * As part of this process - they can as well install the commons-logging implementation
031: * that corresponds to their logger of choice. This completely avoids any discovery
032: * problem, while still allowing the user to switch.
033: *
034: * Note that this implementation is not just a wrapper arround JDK logging ( like
035: * the original commons-logging impl ). It adds 2 features - a simpler configuration
036: * ( which is in fact a subset of log4j.properties ) and a formatter that is
037: * less ugly.
038: *
039: * The removal of 'abstract' preserves binary backward compatibility. It is possible
040: * to preserve the abstract - and introduce another ( hardcoded ) factory - but I
041: * see no benefit.
042: *
043: * Since this class is not intended to be extended - and provides
044: * no plugin for other LogFactory implementation - all protected methods are removed.
045: * This can be changed - but again, there is little value in keeping dead code.
046: * Just take a quick look at the removed code ( and it's complexity)
047: *
048: * --------------
049: *
050: * Original comment:
051: * <p>Factory for creating {@link Log} instances, with discovery and
052: * configuration features similar to that employed by standard Java APIs
053: * such as JAXP.</p>
054: *
055: * <p><strong>IMPLEMENTATION NOTE</strong> - This implementation is heavily
056: * based on the SAXParserFactory and DocumentBuilderFactory implementations
057: * (corresponding to the JAXP pluggability APIs) found in Apache Xerces.</p>
058: *
059: *
060: * @author Craig R. McClanahan
061: * @author Costin Manolache
062: * @author Richard A. Sitze
063: * @version $Revision: 467222 $ $Date: 2006-10-24 05:17:11 +0200 (mar., 24 oct. 2006) $
064: */
065: public/* abstract */class LogFactory {
066:
067: // ----------------------------------------------------- Manifest Constants
068:
069: /**
070: * The name of the property used to identify the LogFactory implementation
071: * class name.
072: */
073: public static final String FACTORY_PROPERTY = "org.apache.commons.logging.LogFactory";
074:
075: /**
076: * The fully qualified class name of the fallback <code>LogFactory</code>
077: * implementation class to use, if no other can be found.
078: */
079: public static final String FACTORY_DEFAULT = "org.apache.commons.logging.impl.LogFactoryImpl";
080:
081: /**
082: * The name of the properties file to search for.
083: */
084: public static final String FACTORY_PROPERTIES = "commons-logging.properties";
085:
086: /**
087: * <p>Setting this system property value allows the <code>Hashtable</code> used to store
088: * classloaders to be substituted by an alternative implementation.
089: * </p>
090: * <p>
091: * <strong>Note:</strong> <code>LogFactory</code> will print:
092: * <code><pre>
093: * [ERROR] LogFactory: Load of custom hashtable failed</em>
094: * </code></pre>
095: * to system error and then continue using a standard Hashtable.
096: * </p>
097: * <p>
098: * <strong>Usage:</strong> Set this property when Java is invoked
099: * and <code>LogFactory</code> will attempt to load a new instance
100: * of the given implementation class.
101: * For example, running the following ant scriplet:
102: * <code><pre>
103: * <java classname="${test.runner}" fork="yes" failonerror="${test.failonerror}">
104: * ...
105: * <sysproperty
106: * key="org.apache.commons.logging.LogFactory.HashtableImpl"
107: * value="org.apache.commons.logging.AltHashtable"/>
108: * </java>
109: * </pre></code>
110: * will mean that <code>LogFactory</code> will load an instance of
111: * <code>org.apache.commons.logging.AltHashtable</code>.
112: * </p>
113: * <p>
114: * A typical use case is to allow a custom
115: * Hashtable implementation using weak references to be substituted.
116: * This will allow classloaders to be garbage collected without
117: * the need to release them (on 1.3+ JVMs only, of course ;)
118: * </p>
119: */
120: public static final String HASHTABLE_IMPLEMENTATION_PROPERTY = "org.apache.commons.logging.LogFactory.HashtableImpl";
121:
122: private static LogFactory singleton = new LogFactory();
123:
124: Properties logConfig;
125:
126: // ----------------------------------------------------------- Constructors
127:
128: /**
129: * Protected constructor that is not available for public use.
130: */
131: private LogFactory() {
132: logConfig = new Properties();
133: }
134:
135: // hook for syserr logger - class level
136: void setLogConfig(Properties p) {
137: this .logConfig = p;
138: }
139:
140: // --------------------------------------------------------- Public Methods
141:
142: // only those 2 methods need to change to use a different direct logger.
143:
144: /**
145: * <p>Construct (if necessary) and return a <code>Log</code> instance,
146: * using the factory's current set of configuration attributes.</p>
147: *
148: * <p><strong>NOTE</strong> - Depending upon the implementation of
149: * the <code>LogFactory</code> you are using, the <code>Log</code>
150: * instance you are returned may or may not be local to the current
151: * application, and may or may not be returned again on a subsequent
152: * call with the same name argument.</p>
153: *
154: * @param name Logical name of the <code>Log</code> instance to be
155: * returned (the meaning of this name is only known to the underlying
156: * logging implementation that is being wrapped)
157: *
158: * @exception LogConfigurationException if a suitable <code>Log</code>
159: * instance cannot be returned
160: */
161: public Log getInstance(String name)
162: throws LogConfigurationException {
163: return DirectJDKLog.getInstance(name);
164: }
165:
166: /**
167: * Release any internal references to previously created {@link Log}
168: * instances returned by this factory. This is useful in environments
169: * like servlet containers, which implement application reloading by
170: * throwing away a ClassLoader. Dangling references to objects in that
171: * class loader would prevent garbage collection.
172: */
173: public void release() {
174: DirectJDKLog.release();
175: }
176:
177: /**
178: * Return the configuration attribute with the specified name (if any),
179: * or <code>null</code> if there is no such attribute.
180: *
181: * @param name Name of the attribute to return
182: */
183: public Object getAttribute(String name) {
184: return logConfig.get(name);
185: }
186:
187: /**
188: * Return an array containing the names of all currently defined
189: * configuration attributes. If there are no such attributes, a zero
190: * length array is returned.
191: */
192: public String[] getAttributeNames() {
193: return (String[]) logConfig.keySet().toArray();
194: }
195:
196: /**
197: * Remove any configuration attribute associated with the specified name.
198: * If there is no such attribute, no action is taken.
199: *
200: * @param name Name of the attribute to remove
201: */
202: public void removeAttribute(String name) {
203: logConfig.remove(name);
204: }
205:
206: /**
207: * Set the configuration attribute with the specified name. Calling
208: * this with a <code>null</code> value is equivalent to calling
209: * <code>removeAttribute(name)</code>.
210: *
211: * @param name Name of the attribute to set
212: * @param value Value of the attribute to set, or <code>null</code>
213: * to remove any setting for this attribute
214: */
215: public void setAttribute(String name, Object value) {
216: logConfig.put(name, value);
217: }
218:
219: /**
220: * Convenience method to derive a name from the specified class and
221: * call <code>getInstance(String)</code> with it.
222: *
223: * @param clazz Class for which a suitable Log name will be derived
224: *
225: * @exception LogConfigurationException if a suitable <code>Log</code>
226: * instance cannot be returned
227: */
228: public Log getInstance(Class clazz)
229: throws LogConfigurationException {
230: return getInstance(clazz.getName());
231: }
232:
233: // ------------------------------------------------------- Static Variables
234:
235: // --------------------------------------------------------- Static Methods
236:
237: /**
238: * <p>Construct (if necessary) and return a <code>LogFactory</code>
239: * instance, using the following ordered lookup procedure to determine
240: * the name of the implementation class to be loaded.</p>
241: * <ul>
242: * <li>The <code>org.apache.commons.logging.LogFactory</code> system
243: * property.</li>
244: * <li>The JDK 1.3 Service Discovery mechanism</li>
245: * <li>Use the properties file <code>commons-logging.properties</code>
246: * file, if found in the class path of this class. The configuration
247: * file is in standard <code>java.util.Properties</code> format and
248: * contains the fully qualified name of the implementation class
249: * with the key being the system property defined above.</li>
250: * <li>Fall back to a default implementation class
251: * (<code>org.apache.commons.logging.impl.LogFactoryImpl</code>).</li>
252: * </ul>
253: *
254: * <p><em>NOTE</em> - If the properties file method of identifying the
255: * <code>LogFactory</code> implementation class is utilized, all of the
256: * properties defined in this file will be set as configuration attributes
257: * on the corresponding <code>LogFactory</code> instance.</p>
258: *
259: * @exception LogConfigurationException if the implementation class is not
260: * available or cannot be instantiated.
261: */
262: public static LogFactory getFactory()
263: throws LogConfigurationException {
264: return singleton;
265: }
266:
267: /**
268: * Convenience method to return a named logger, without the application
269: * having to care about factories.
270: *
271: * @param clazz Class from which a log name will be derived
272: *
273: * @exception LogConfigurationException if a suitable <code>Log</code>
274: * instance cannot be returned
275: */
276: public static Log getLog(Class clazz)
277: throws LogConfigurationException {
278: return (getFactory().getInstance(clazz));
279:
280: }
281:
282: /**
283: * Convenience method to return a named logger, without the application
284: * having to care about factories.
285: *
286: * @param name Logical name of the <code>Log</code> instance to be
287: * returned (the meaning of this name is only known to the underlying
288: * logging implementation that is being wrapped)
289: *
290: * @exception LogConfigurationException if a suitable <code>Log</code>
291: * instance cannot be returned
292: */
293: public static Log getLog(String name)
294: throws LogConfigurationException {
295: return (getFactory().getInstance(name));
296:
297: }
298:
299: /**
300: * Release any internal references to previously created {@link LogFactory}
301: * instances that have been associated with the specified class loader
302: * (if any), after calling the instance method <code>release()</code> on
303: * each of them.
304: *
305: * @param classLoader ClassLoader for which to release the LogFactory
306: */
307: public static void release(ClassLoader classLoader) {
308: // nothing - we don't use any class loaders
309: }
310:
311: /**
312: * Release any internal references to previously created {@link LogFactory}
313: * instances, after calling the instance method <code>release()</code> on
314: * each of them. This is useful in environments like servlet containers,
315: * which implement application reloading by throwing away a ClassLoader.
316: * Dangling references to objects in that class loader would prevent
317: * garbage collection.
318: */
319: public static void releaseAll() {
320: singleton.release();
321: }
322:
323: /**
324: * Returns a string that uniquely identifies the specified object, including
325: * its class.
326: * <p>
327: * The returned string is of form "classname@hashcode", ie is the same as
328: * the return value of the Object.toString() method, but works even when
329: * the specified object's class has overidden the toString method.
330: *
331: * @param o may be null.
332: * @return a string of form classname@hashcode, or "null" if param o is null.
333: */
334: public static String objectId(Object o) {
335: if (o == null) {
336: return "null";
337: } else {
338: return o.getClass().getName() + "@"
339: + System.identityHashCode(o);
340: }
341: }
342: }
|