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 * https://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
18 package org.apache.commons.logging.impl;
19
20 import java.io.Serializable;
21
22 import org.apache.commons.logging.Log;
23 import org.apache.log4j.Level;
24 import org.apache.log4j.Logger;
25 import org.apache.log4j.Priority;
26
27 /**
28 * Implements {@link Log} to map directly to a
29 * <strong>Logger</strong> for Apache Log4J version 1.2.
30 * <p>
31 * Initial configuration of the corresponding Logger instances should be done
32 * in the usual manner, as outlined in the Log4J documentation.
33 * </p>
34 * <p>
35 * The reason this logger is distinct from the 1.3.0 logger is that in version 1.2
36 * of Log4J:
37 * </p>
38 * <ul>
39 * <li>class Logger takes Priority parameters not Level parameters.</li>
40 * <li>class Level extends Priority</li>
41 * </ul>
42 * <p>
43 * Log4j 1.3 is expected to change Level so it no longer extends Priority, which is
44 * a non-binary-compatible change. The class generated by compiling this code against
45 * Log4j 1.2 will therefore not run against Log4j 1.3.
46 * </p>
47 *
48 * @deprecated Scheduled for removal since version 1.x of Log4j has reached end-of-life.
49 */
50 @Deprecated
51 public class Log4JLogger implements Log, Serializable {
52
53 /** Serializable version identifier. */
54 private static final long serialVersionUID = 5160705895411730424L;
55
56 /** The fully qualified name of the Log4JLogger class. */
57 private static final String FQCN = Log4JLogger.class.getName();
58
59 private static final Priority TRACE_LEVEL;
60
61 //
62 // Note that this must come after the static variable declarations
63 // otherwise initializer expressions associated with those variables
64 // will override any settings done here.
65 //
66 // Verify that Log4j is available, and that it is version 1.2.
67 // If an ExceptionInInitializerError is generated, then LogFactoryImpl
68 // will treat that as meaning that the appropriate underlying logging
69 // library is just not present - if discovery is in progress then
70 // discovery will continue.
71 static {
72 if (!Priority.class.isAssignableFrom(Level.class)) {
73 // nope, this is Log4j 1.3, so force an ExceptionInInitializerError
74 throw new InstantiationError("Log4J 1.2 not available");
75 }
76 // Releases of Log4j 1.2 >= 1.2.12 have Priority.TRACE available, earlier
77 // versions do not. If TRACE is not available, then we have to map
78 // calls to Log.trace(...) onto the DEBUG level.
79 Priority traceLevel;
80 try {
81 traceLevel = (Priority) Level.class.getDeclaredField("TRACE").get(null);
82 } catch (final Exception ex) {
83 // ok, trace not available
84 traceLevel = Level.DEBUG;
85 }
86 TRACE_LEVEL = traceLevel;
87 }
88
89 /** Log to this logger */
90 private transient volatile Logger logger;
91
92 /** Logger name */
93 private final String name;
94
95 /**
96 * Constructs a new instance.
97 */
98 public Log4JLogger() {
99 name = null;
100 }
101
102 /**
103 * For use with a Log4j factory.
104 *
105 * @param logger Logger.
106 */
107 public Log4JLogger(final Logger logger) {
108 if (logger == null) {
109 throw new IllegalArgumentException("Warning - null logger in constructor; possible Log4j misconfiguration.");
110 }
111 this.name = logger.getName();
112 this.logger = logger;
113 }
114
115 /**
116 * Base constructor.
117 *
118 * @param name name.
119 */
120 public Log4JLogger(final String name) {
121 this.name = name;
122 this.logger = getLogger();
123 }
124
125 /**
126 * Logs a message with {@code org.apache.log4j.Priority.DEBUG}.
127 *
128 * @param message to log
129 * @see org.apache.commons.logging.Log#debug(Object)
130 */
131 @Override
132 public void debug(final Object message) {
133 getLogger().log(FQCN, Level.DEBUG, message, null);
134 }
135
136 /**
137 * Logs a message with {@code org.apache.log4j.Priority.DEBUG}.
138 *
139 * @param message to log
140 * @param t log this cause
141 * @see org.apache.commons.logging.Log#debug(Object, Throwable)
142 */
143 @Override
144 public void debug(final Object message, final Throwable t) {
145 getLogger().log(FQCN, Level.DEBUG, message, t);
146 }
147
148 /**
149 * Logs a message with {@code org.apache.log4j.Priority.ERROR}.
150 *
151 * @param message to log
152 * @see org.apache.commons.logging.Log#error(Object)
153 */
154 @Override
155 public void error(final Object message) {
156 getLogger().log(FQCN, Level.ERROR, message, null);
157 }
158
159 /**
160 * Logs a message with {@code org.apache.log4j.Priority.ERROR}.
161 *
162 * @param message to log
163 * @param t log this cause
164 * @see org.apache.commons.logging.Log#error(Object, Throwable)
165 */
166 @Override
167 public void error(final Object message, final Throwable t) {
168 getLogger().log(FQCN, Level.ERROR, message, t);
169 }
170
171 /**
172 * Logs a message with {@code org.apache.log4j.Priority.FATAL}.
173 *
174 * @param message to log
175 * @see org.apache.commons.logging.Log#fatal(Object)
176 */
177 @Override
178 public void fatal(final Object message) {
179 getLogger().log(FQCN, Level.FATAL, message, null);
180 }
181
182 /**
183 * Logs a message with {@code org.apache.log4j.Priority.FATAL}.
184 *
185 * @param message to log
186 * @param t log this cause
187 * @see org.apache.commons.logging.Log#fatal(Object, Throwable)
188 */
189 @Override
190 public void fatal(final Object message, final Throwable t) {
191 getLogger().log(FQCN, Level.FATAL, message, t);
192 }
193
194 /**
195 * Gets the native Logger instance we are using.
196 *
197 * @return the native Logger instance we are using.
198 */
199 public Logger getLogger() {
200 Logger result = logger;
201 if (result == null) {
202 synchronized(this) {
203 result = logger;
204 if (result == null) {
205 logger = result = Logger.getLogger(name);
206 }
207 }
208 }
209 return result;
210 }
211
212 /**
213 * Logs a message with {@code org.apache.log4j.Priority.INFO}.
214 *
215 * @param message to log
216 * @see org.apache.commons.logging.Log#info(Object)
217 */
218 @Override
219 public void info(final Object message) {
220 getLogger().log(FQCN, Level.INFO, message, null);
221 }
222
223 /**
224 * Logs a message with {@code org.apache.log4j.Priority.INFO}.
225 *
226 * @param message to log
227 * @param t log this cause
228 * @see org.apache.commons.logging.Log#info(Object, Throwable)
229 */
230 @Override
231 public void info(final Object message, final Throwable t) {
232 getLogger().log(FQCN, Level.INFO, message, t);
233 }
234
235 /**
236 * Tests whether the Log4j Logger used is enabled for {@code DEBUG} priority.
237 */
238 @Override
239 public boolean isDebugEnabled() {
240 return getLogger().isDebugEnabled();
241 }
242
243 /**
244 * Tests whether the Log4j Logger used is enabled for {@code ERROR} priority.
245 */
246 @Override
247 public boolean isErrorEnabled() {
248 return getLogger().isEnabledFor(Level.ERROR);
249 }
250
251 /**
252 * Tests whether the Log4j Logger used is enabled for {@code FATAL} priority.
253 */
254 @Override
255 public boolean isFatalEnabled() {
256 return getLogger().isEnabledFor(Level.FATAL);
257 }
258
259 /**
260 * Tests whether the Log4j Logger used is enabled for {@code INFO} priority.
261 */
262 @Override
263 public boolean isInfoEnabled() {
264 return getLogger().isInfoEnabled();
265 }
266
267 /**
268 * Tests whether the Log4j Logger used is enabled for {@code TRACE} priority.
269 * When using a Log4j version that does not support the TRACE level, this call
270 * will report whether {@code DEBUG} is enabled or not.
271 */
272 @Override
273 public boolean isTraceEnabled() {
274 return getLogger().isEnabledFor(TRACE_LEVEL);
275 }
276
277 /**
278 * Tests whether the Log4j Logger used is enabled for {@code WARN} priority.
279 */
280 @Override
281 public boolean isWarnEnabled() {
282 return getLogger().isEnabledFor(Level.WARN);
283 }
284
285 /**
286 * Logs a message with {@code org.apache.log4j.Priority.TRACE}.
287 * When using a Log4j version that does not support the {@code TRACE}
288 * level, the message will be logged at the {@code DEBUG} level.
289 *
290 * @param message to log
291 * @see org.apache.commons.logging.Log#trace(Object)
292 */
293 @Override
294 public void trace(final Object message) {
295 getLogger().log(FQCN, TRACE_LEVEL, message, null);
296 }
297
298 /**
299 * Logs a message with {@code org.apache.log4j.Priority.TRACE}.
300 * When using a Log4j version that does not support the {@code TRACE}
301 * level, the message will be logged at the {@code DEBUG} level.
302 *
303 * @param message to log
304 * @param t log this cause
305 * @see org.apache.commons.logging.Log#trace(Object, Throwable)
306 */
307 @Override
308 public void trace(final Object message, final Throwable t) {
309 getLogger().log(FQCN, TRACE_LEVEL, message, t);
310 }
311
312 /**
313 * Logs a message with {@code org.apache.log4j.Priority.WARN}.
314 *
315 * @param message to log
316 * @see org.apache.commons.logging.Log#warn(Object)
317 */
318 @Override
319 public void warn(final Object message) {
320 getLogger().log(FQCN, Level.WARN, message, null);
321 }
322
323 /**
324 * Logs a message with {@code org.apache.log4j.Priority.WARN}.
325 *
326 * @param message to log
327 * @param t log this cause
328 * @see org.apache.commons.logging.Log#warn(Object, Throwable)
329 */
330 @Override
331 public void warn(final Object message, final Throwable t) {
332 getLogger().log(FQCN, Level.WARN, message, t);
333 }
334
335 }