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