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