View Javadoc

1   /*
2    * Copyright 2001-2004 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.commons.logging.impl;
18  
19  import java.util.ArrayList;
20  import java.util.Enumeration;
21  import java.util.HashMap;
22  import java.util.Hashtable;
23  import java.util.List;
24  import java.util.Map;
25  
26  import org.apache.commons.logging.Log;
27  import org.apache.commons.logging.LogConfigurationException;
28  import org.apache.commons.logging.LogFactory;
29  import org.slf4j.Logger;
30  import org.slf4j.LoggerFactory;
31  import org.slf4j.spi.LocationAwareLogger;
32  
33  /**
34   * <p>
35   * Concrete subclass of {@link LogFactory} which always delegates to the
36   * {@link LoggerFactory org.slf4j.LoggerFactory} class.
37   * 
38   * <p>
39   * This factory generates instances of {@link SLF4JLog}. It will remember
40   * previously created instances for the same name, and will return them on
41   * repeated requests to the <code>getInstance()</code> method.
42   * 
43   * <p>
44   * This implementation ignores any configured attributes.
45   * </p>
46   * 
47   * @author Rod Waldhoff
48   * @author Craig R. McClanahan
49   * @author Richard A. Sitze
50   * @author Ceki G&uuml;lc&uuml;
51   */
52  
53  public class SLF4JLogFactory extends LogFactory {
54  
55    // ----------------------------------------------------------- Constructors
56  
57    /**
58     * The {@link org.apache.commons.logging.Log}instances that have already been
59     * created, keyed by logger name.
60     */
61    Map loggerMap;
62  
63    /**
64     * Public no-arguments constructor required by the lookup mechanism.
65     */
66    public SLF4JLogFactory() {
67      loggerMap = new HashMap();
68    }
69  
70    // ----------------------------------------------------- Manifest Constants
71  
72    /**
73     * The name of the system property identifying our {@link Log}implementation
74     * class.
75     */
76    public static final String LOG_PROPERTY = "org.apache.commons.logging.Log";
77  
78    // ----------------------------------------------------- Instance Variables
79  
80    /**
81     * Configuration attributes.
82     */
83    protected Hashtable attributes = new Hashtable();
84  
85    // --------------------------------------------------------- Public Methods
86  
87    /**
88     * Return the configuration attribute with the specified name (if any), or
89     * <code>null</code> if there is no such attribute.
90     * 
91     * @param name
92     *          Name of the attribute to return
93     */
94    public Object getAttribute(String name) {
95  
96      return (attributes.get(name));
97  
98    }
99  
100   /**
101    * Return an array containing the names of all currently defined configuration
102    * attributes. If there are no such attributes, a zero length array is
103    * returned.
104    */
105   public String[] getAttributeNames() {
106 
107     List names = new ArrayList();
108     Enumeration keys = attributes.keys();
109     while (keys.hasMoreElements()) {
110       names.add((String) keys.nextElement());
111     }
112     String results[] = new String[names.size()];
113     for (int i = 0; i < results.length; i++) {
114       results[i] = (String) names.get(i);
115     }
116     return (results);
117 
118   }
119 
120   /**
121    * Convenience method to derive a name from the specified class and call
122    * <code>getInstance(String)</code> with it.
123    * 
124    * @param clazz
125    *          Class for which a suitable Log name will be derived
126    * 
127    * @exception LogConfigurationException
128    *              if a suitable <code>Log</code> instance cannot be returned
129    */
130   public Log getInstance(Class clazz) throws LogConfigurationException {
131 
132     return (getInstance(clazz.getName()));
133 
134   }
135 
136   /**
137    * <p>
138    * Construct (if necessary) and return a <code>Log</code> instance, using
139    * the factory's current set of configuration attributes.
140    * </p>
141    * 
142    * @param name
143    *          Logical name of the <code>Log</code> instance to be returned
144    *          (the meaning of this name is only known to the underlying logging
145    *          implementation that is being wrapped)
146    * 
147    * @exception LogConfigurationException
148    *              if a suitable <code>Log</code> instance cannot be returned
149    */
150   public Log getInstance(String name) throws LogConfigurationException {
151     Log instance = null;
152     // protect against concurrent access of loggerMap
153     synchronized (loggerMap) {
154       instance = (Log) loggerMap.get(name);
155       if (instance == null) {
156         Logger logger = LoggerFactory.getLogger(name);
157         if(logger instanceof LocationAwareLogger) {
158           instance = new SLF4JLocationAwareLog((LocationAwareLogger) logger);
159         } else {
160           instance = new SLF4JLog(logger);
161         }
162         loggerMap.put(name, instance);
163       }
164     }
165     return (instance);
166 
167   }
168 
169   /**
170    * Release any internal references to previously created
171    * {@link org.apache.commons.logging.Log}instances returned by this factory.
172    * This is useful in environments like servlet containers, which implement
173    * application reloading by throwing away a ClassLoader. Dangling references
174    * to objects in that class loader would prevent garbage collection.
175    */
176   public void release() {
177     // This method is never called by jcl-over-slf4j classes. However,
178     // in certain deployment scenarios, in particular if jcl-over-slf4j.jar
179     // is
180     // in the the web-app class loader and the official commons-logging.jar is
181     // deployed in some parent class loader (e.g. commons/lib), then it is
182     // possible
183     // for the parent class loader to mask the classes shipping in
184     // jcl-over-slf4j.jar.
185     System.out.println("WARN: The method " + SLF4JLogFactory.class
186         + "#release() was invoked.");
187     System.out
188         .println("WARN: Please see http://www.slf4j.org/codes.html#release for an explanation.");
189     System.out.flush();
190   }
191 
192   /**
193    * Remove any configuration attribute associated with the specified name. If
194    * there is no such attribute, no action is taken.
195    * 
196    * @param name
197    *          Name of the attribute to remove
198    */
199   public void removeAttribute(String name) {
200     attributes.remove(name);
201   }
202 
203   /**
204    * Set the configuration attribute with the specified name. Calling this with
205    * a <code>null</code> value is equivalent to calling
206    * <code>removeAttribute(name)</code>.
207    * 
208    * @param name
209    *          Name of the attribute to set
210    * @param value
211    *          Value of the attribute to set, or <code>null</code> to remove
212    *          any setting for this attribute
213    */
214   public void setAttribute(String name, Object value) {
215 
216     if (value == null) {
217       attributes.remove(name);
218     } else {
219       attributes.put(name, value);
220     }
221 
222   }
223 }