1 /*
2 * Licensed to the Apache Software Foundation (ASF) under one or more
3 * contributor license agreements. See the NOTICE file distributed with
4 * this work for additional information regarding copyright ownership.
5 * The ASF licenses this file to You under the Apache License, Version 2.0
6 * (the "License"); you may not use this file except in compliance with
7 * the License. You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17 package org.apache.juli.logging;
18
19 import java.lang.reflect.Constructor;
20 import java.nio.file.FileSystems;
21 import java.util.ServiceLoader;
22 import java.util.logging.LogManager;
23
24 /**
25 * This is a modified LogFactory that uses a simple {@link ServiceLoader} based
26 * discovery mechanism with a default of using JDK based logging. An
27 * implementation that uses the full Commons Logging discovery mechanism is
28 * available as part of the Tomcat extras download.
29 *
30 * Why? It is an attempt to strike a balance between simpler code (no discovery)
31 * and providing flexibility - particularly for those projects that embed Tomcat
32 * or some of Tomcat's components - is an alternative logging
33 * implementation is desired.
34 *
35 * Note that this implementation is not just a wrapper around JDK logging (like
36 * the original commons-logging impl). It adds 2 features - a simpler
37 * configuration (which is in fact a subset of log4j.properties) and a
38 * formatter that is less ugly.
39 *
40 * The removal of 'abstract' preserves binary backward compatibility. It is
41 * possible to preserve the abstract - and introduce another (hardcoded) factory
42 * - but I see no benefit.
43 *
44 * Since this class is not intended to be extended - all protected methods are
45 * removed. This can be changed - but again, there is little value in keeping
46 * dead code. Just take a quick look at the removed code ( and it's complexity).
47 *
48 * --------------
49 *
50 * Original comment:
51 * <p>Factory for creating {@link Log} instances, with discovery and
52 * configuration features similar to that employed by standard Java APIs
53 * such as JAXP.</p>
54 *
55 * <p><strong>IMPLEMENTATION NOTE</strong> - This implementation is heavily
56 * based on the SAXParserFactory and DocumentBuilderFactory implementations
57 * (corresponding to the JAXP pluggability APIs) found in Apache Xerces.</p>
58 *
59 *
60 * @author Craig R. McClanahan
61 * @author Costin Manolache
62 * @author Richard A. Sitze
63 */
64 public class LogFactory {
65
66 private static final LogFactory singleton = new LogFactory();
67
68 private final Constructor<? extends Log> discoveredLogConstructor;
69
70 /**
71 * Private constructor that is not available for public use.
72 */
73 private LogFactory() {
74 /*
75 * Work-around known a JRE bug.
76 * https://bugs.openjdk.java.net/browse/JDK-8194653
77 *
78 * Pre-load the default file system. No performance impact as we need to
79 * load the default file system anyway. Just do it earlier to avoid the
80 * potential deadlock.
81 *
82 * This can be removed once the oldest JRE supported by Tomcat includes
83 * a fix.
84 */
85 FileSystems.getDefault();
86
87 // Look via a ServiceLoader for a Log implementation that has a
88 // constructor taking the String name.
89 ServiceLoader<Log> logLoader = ServiceLoader.load(Log.class);
90 Constructor<? extends Log> m=null;
91 for (Log log: logLoader) {
92 Class<? extends Log> c=log.getClass();
93 try {
94 m=c.getConstructor(String.class);
95 break;
96 }
97 catch (NoSuchMethodException | SecurityException e) {
98 throw new Error(e);
99 }
100 }
101 discoveredLogConstructor=m;
102 }
103
104
105 // --------------------------------------------------------- Public Methods
106
107 // only those 2 methods need to change to use a different direct logger.
108
109 /**
110 * <p>Construct (if necessary) and return a <code>Log</code> instance,
111 * using the factory's current set of configuration attributes.</p>
112 *
113 * <p><strong>NOTE</strong> - Depending upon the implementation of
114 * the <code>LogFactory</code> you are using, the <code>Log</code>
115 * instance you are returned may or may not be local to the current
116 * application, and may or may not be returned again on a subsequent
117 * call with the same name argument.</p>
118 *
119 * @param name Logical name of the <code>Log</code> instance to be
120 * returned (the meaning of this name is only known to the underlying
121 * logging implementation that is being wrapped)
122 *
123 * @return A log instance with the requested name
124 *
125 * @exception LogConfigurationException if a suitable <code>Log</code>
126 * instance cannot be returned
127 */
128 public Log getInstance(String name) throws LogConfigurationException {
129 if (discoveredLogConstructor == null) {
130 return DirectJDKLog.getInstance(name);
131 }
132
133 try {
134 return discoveredLogConstructor.newInstance(name);
135 } catch (ReflectiveOperationException | IllegalArgumentException e) {
136 throw new LogConfigurationException(e);
137 }
138 }
139
140
141 /**
142 * Convenience method to derive a name from the specified class and
143 * call <code>getInstance(String)</code> with it.
144 *
145 * @param clazz Class for which a suitable Log name will be derived
146 *
147 * @return A log instance with a name of clazz.getName()
148 *
149 * @exception LogConfigurationException if a suitable <code>Log</code>
150 * instance cannot be returned
151 */
152 public Log getInstance(Class<?> clazz) throws LogConfigurationException {
153 return getInstance( clazz.getName());
154 }
155
156
157 // ------------------------------------------------------- Static Variables
158
159
160 // --------------------------------------------------------- Static Methods
161
162
163 /**
164 * <p>Construct (if necessary) and return a <code>LogFactory</code>
165 * instance, using the following ordered lookup procedure to determine
166 * the name of the implementation class to be loaded.</p>
167 * <ul>
168 * <li>The <code>org.apache.commons.logging.LogFactory</code> system
169 * property.</li>
170 * <li>The JDK 1.3 Service Discovery mechanism</li>
171 * <li>Use the properties file <code>commons-logging.properties</code>
172 * file, if found in the class path of this class. The configuration
173 * file is in standard <code>java.util.Properties</code> format and
174 * contains the fully qualified name of the implementation class
175 * with the key being the system property defined above.</li>
176 * <li>Fall back to a default implementation class
177 * (<code>org.apache.commons.logging.impl.LogFactoryImpl</code>).</li>
178 * </ul>
179 *
180 * <p><em>NOTE</em> - If the properties file method of identifying the
181 * <code>LogFactory</code> implementation class is utilized, all of the
182 * properties defined in this file will be set as configuration attributes
183 * on the corresponding <code>LogFactory</code> instance.</p>
184 *
185 * @return The singleton LogFactory instance
186 *
187 * @exception LogConfigurationException if the implementation class is not
188 * available or cannot be instantiated.
189 */
190 public static LogFactory getFactory() throws LogConfigurationException {
191 return singleton;
192 }
193
194
195 /**
196 * Convenience method to return a named logger, without the application
197 * having to care about factories.
198 *
199 * @param clazz Class from which a log name will be derived
200 *
201 * @return A log instance with a name of clazz.getName()
202 *
203 * @exception LogConfigurationException if a suitable <code>Log</code>
204 * instance cannot be returned
205 */
206 public static Log getLog(Class<?> clazz)
207 throws LogConfigurationException {
208 return getFactory().getInstance(clazz);
209 }
210
211
212 /**
213 * Convenience method to return a named logger, without the application
214 * having to care about factories.
215 *
216 * @param name Logical name of the <code>Log</code> instance to be
217 * returned (the meaning of this name is only known to the underlying
218 * logging implementation that is being wrapped)
219 *
220 * @return A log instance with the requested name
221 *
222 * @exception LogConfigurationException if a suitable <code>Log</code>
223 * instance cannot be returned
224 */
225 public static Log getLog(String name)
226 throws LogConfigurationException {
227 return getFactory().getInstance(name);
228 }
229
230
231 /**
232 * Release any internal references to previously created {@link LogFactory}
233 * instances that have been associated with the specified class loader
234 * (if any), after calling the instance method <code>release()</code> on
235 * each of them.
236 *
237 * @param classLoader ClassLoader for which to release the LogFactory
238 */
239 public static void release(ClassLoader classLoader) {
240 // JULI's log manager looks at the current classLoader so there is no
241 // need to use the passed in classLoader, the default implementation
242 // does not so calling reset in that case will break things
243 if (!LogManager.getLogManager().getClass().getName().equals(
244 "java.util.logging.LogManager")) {
245 LogManager.getLogManager().reset();
246 }
247 }
248 }
249