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 org.apache.commons.logging.Log;
20 import org.apache.commons.logging.LogConfigurationException;
21 import org.apache.commons.logging.LogFactory;
22 import org.slf4j.Logger;
23 import org.slf4j.LoggerFactory;
24 import org.slf4j.spi.LocationAwareLogger;
25
26 import java.util.ArrayList;
27 import java.util.Enumeration;
28 import java.util.Hashtable;
29 import java.util.List;
30 import java.util.concurrent.ConcurrentHashMap;
31 import java.util.concurrent.ConcurrentMap;
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ülcü
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 ConcurrentMap<String, Log> loggerMap;
62
63 /**
64 * Public no-arguments constructor required by the lookup mechanism.
65 */
66 public SLF4JLogFactory() {
67 loggerMap = new ConcurrentHashMap<String, Log>();
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 = loggerMap.get(name);
152 if (instance != null) {
153 return instance;
154 } else {
155 Log newInstance;
156 Logger slf4jLogger = LoggerFactory.getLogger(name);
157 if (slf4jLogger instanceof LocationAwareLogger) {
158 newInstance = new SLF4JLocationAwareLog((LocationAwareLogger) slf4jLogger);
159 } else {
160 newInstance = new SLF4JLog(slf4jLogger);
161 }
162 Log oldInstance = loggerMap.putIfAbsent(name, newInstance);
163 return oldInstance == null ? newInstance : oldInstance;
164 }
165 }
166
167 /**
168 * Release any internal references to previously created
169 * {@link org.apache.commons.logging.Log}instances returned by this factory.
170 * This is useful in environments like servlet containers, which implement
171 * application reloading by throwing away a ClassLoader. Dangling references
172 * to objects in that class loader would prevent garbage collection.
173 */
174 public void release() {
175 // This method is never called by jcl-over-slf4j classes. However,
176 // in certain deployment scenarios, in particular if jcl-over-slf4j.jar
177 // is
178 // in the the web-app class loader and the official commons-logging.jar is
179 // deployed in some parent class loader (e.g. commons/lib), then it is
180 // possible
181 // for the parent class loader to mask the classes shipping in
182 // jcl-over-slf4j.jar.
183 System.out.println("WARN: The method " + SLF4JLogFactory.class
184 + "#release() was invoked.");
185 System.out
186 .println("WARN: Please see http://www.slf4j.org/codes.html#release for an explanation.");
187 System.out.flush();
188 }
189
190 /**
191 * Remove any configuration attribute associated with the specified name. If
192 * there is no such attribute, no action is taken.
193 *
194 * @param name
195 * Name of the attribute to remove
196 */
197 public void removeAttribute(String name) {
198 attributes.remove(name);
199 }
200
201 /**
202 * Set the configuration attribute with the specified name. Calling this with
203 * a <code>null</code> value is equivalent to calling
204 * <code>removeAttribute(name)</code>.
205 *
206 * @param name
207 * Name of the attribute to set
208 * @param value
209 * Value of the attribute to set, or <code>null</code> to remove
210 * any setting for this attribute
211 */
212 public void setAttribute(String name, Object value) {
213
214 if (value == null) {
215 attributes.remove(name);
216 } else {
217 attributes.put(name, value);
218 }
219
220 }
221 }