KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > commons > logging > impl > Log4JLogger


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
18 package org.apache.commons.logging.impl;
19
20 import java.io.Serializable JavaDoc;
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  * @author <a HREF="mailto:sanders@apache.org">Scott Sanders</a>
44  * @author Rod Waldhoff
45  * @author Robert Burrell Donkin
46  * @version $Id: Log4JLogger.java 370672 2006-01-19 23:52:23Z skitching $
47  */

48
49 public class Log4JLogger implements Log, Serializable JavaDoc {
50
51     // ------------------------------------------------------------- Attributes
52

53     /** The fully qualified name of the Log4JLogger class. */
54     private static final String JavaDoc FQCN = Log4JLogger.class.getName();
55     
56     /** Log to this logger */
57     private transient Logger logger = null;
58
59     /** Logger name */
60     private String JavaDoc name = null;
61
62     private static Priority traceLevel;
63     
64     // ------------------------------------------------------------
65
// Static Initializer.
66
//
67
// Note that this must come after the static variable declarations
68
// otherwise initialiser expressions associated with those variables
69
// will override any settings done here.
70
//
71
// Verify that log4j is available, and that it is version 1.2.
72
// If an ExceptionInInitializerError is generated, then LogFactoryImpl
73
// will treat that as meaning that the appropriate underlying logging
74
// library is just not present - if discovery is in progress then
75
// discovery will continue.
76
// ------------------------------------------------------------
77

78     static {
79         if (!Priority.class.isAssignableFrom(Level.class)) {
80             // nope, this is log4j 1.3, so force an ExceptionInInitializerError
81
throw new InstantiationError JavaDoc("Log4J 1.2 not available");
82         }
83         
84         // Releases of log4j1.2 >= 1.2.12 have Priority.TRACE available, earlier
85
// versions do not. If TRACE is not available, then we have to map
86
// calls to Log.trace(...) onto the DEBUG level.
87

88         try {
89             traceLevel = (Priority) Level.class.getDeclaredField("TRACE").get(null);
90         } catch(Exception JavaDoc ex) {
91             // ok, trace not available
92
traceLevel = Priority.DEBUG;
93         }
94     }
95
96     
97     // ------------------------------------------------------------ Constructor
98

99     public Log4JLogger() {
100     }
101
102
103     /**
104      * Base constructor.
105      */

106     public Log4JLogger(String JavaDoc name) {
107         this.name = name;
108         this.logger = getLogger();
109     }
110
111     /** For use with a log4j factory.
112      */

113     public Log4JLogger(Logger logger ) {
114         this.name = logger.getName();
115         this.logger=logger;
116     }
117
118
119     // ---------------------------------------------------------
120
// Implementation
121
//
122
// Note that in the methods below the Priority class is used to define
123
// levels even though the Level class is supported in 1.2. This is done
124
// so that at compile time the call definitely resolves to a call to
125
// a method that takes a Priority rather than one that takes a Level.
126
//
127
// The Category class (and hence its subclass Logger) in version 1.2 only
128
// has methods that take Priority objects. The Category class (and hence
129
// Logger class) in version 1.3 has methods that take both Priority and
130
// Level objects. This means that if we use Level here, and compile
131
// against log4j 1.3 then calls would be bound to the versions of
132
// methods taking Level objects and then would fail to run against
133
// version 1.2 of log4j.
134
// ---------------------------------------------------------
135

136
137     /**
138      * Logs a message with <code>org.apache.log4j.Priority.TRACE</code>.
139      * When using a log4j version that does not support the <code>TRACE</code>
140      * level, the message will be logged at the <code>DEBUG</code> level.
141      *
142      * @param message to log
143      * @see org.apache.commons.logging.Log#trace(Object)
144      */

145     public void trace(Object JavaDoc message) {
146         getLogger().log(FQCN, traceLevel, message, null );
147     }
148
149
150     /**
151      * Logs a message with <code>org.apache.log4j.Priority.TRACE</code>.
152      * When using a log4j version that does not support the <code>TRACE</code>
153      * level, the message will be logged at the <code>DEBUG</code> level.
154      *
155      * @param message to log
156      * @param t log this cause
157      * @see org.apache.commons.logging.Log#trace(Object, Throwable)
158      */

159     public void trace(Object JavaDoc message, Throwable JavaDoc t) {
160         getLogger().log(FQCN, traceLevel, message, t );
161     }
162
163
164     /**
165      * Logs a message with <code>org.apache.log4j.Priority.DEBUG</code>.
166      *
167      * @param message to log
168      * @see org.apache.commons.logging.Log#debug(Object)
169      */

170     public void debug(Object JavaDoc message) {
171         getLogger().log(FQCN, Priority.DEBUG, message, null );
172     }
173
174     /**
175      * Logs a message with <code>org.apache.log4j.Priority.DEBUG</code>.
176      *
177      * @param message to log
178      * @param t log this cause
179      * @see org.apache.commons.logging.Log#debug(Object, Throwable)
180      */

181     public void debug(Object JavaDoc message, Throwable JavaDoc t) {
182         getLogger().log(FQCN, Priority.DEBUG, message, t );
183     }
184
185
186     /**
187      * Logs a message with <code>org.apache.log4j.Priority.INFO</code>.
188      *
189      * @param message to log
190      * @see org.apache.commons.logging.Log#info(Object)
191      */

192     public void info(Object JavaDoc message) {
193         getLogger().log(FQCN, Priority.INFO, message, null );
194     }
195
196
197     /**
198      * Logs a message with <code>org.apache.log4j.Priority.INFO</code>.
199      *
200      * @param message to log
201      * @param t log this cause
202      * @see org.apache.commons.logging.Log#info(Object, Throwable)
203      */

204     public void info(Object JavaDoc message, Throwable JavaDoc t) {
205         getLogger().log(FQCN, Priority.INFO, message, t );
206     }
207
208
209     /**
210      * Logs a message with <code>org.apache.log4j.Priority.WARN</code>.
211      *
212      * @param message to log
213      * @see org.apache.commons.logging.Log#warn(Object)
214      */

215     public void warn(Object JavaDoc message) {
216         getLogger().log(FQCN, Priority.WARN, message, null );
217     }
218
219
220     /**
221      * Logs a message with <code>org.apache.log4j.Priority.WARN</code>.
222      *
223      * @param message to log
224      * @param t log this cause
225      * @see org.apache.commons.logging.Log#warn(Object, Throwable)
226      */

227     public void warn(Object JavaDoc message, Throwable JavaDoc t) {
228         getLogger().log(FQCN, Priority.WARN, message, t );
229     }
230
231
232     /**
233      * Logs a message with <code>org.apache.log4j.Priority.ERROR</code>.
234      *
235      * @param message to log
236      * @see org.apache.commons.logging.Log#error(Object)
237      */

238     public void error(Object JavaDoc message) {
239         getLogger().log(FQCN, Priority.ERROR, message, null );
240     }
241
242
243     /**
244      * Logs a message with <code>org.apache.log4j.Priority.ERROR</code>.
245      *
246      * @param message to log
247      * @param t log this cause
248      * @see org.apache.commons.logging.Log#error(Object, Throwable)
249      */

250     public void error(Object JavaDoc message, Throwable JavaDoc t) {
251         getLogger().log(FQCN, Priority.ERROR, message, t );
252     }
253
254
255     /**
256      * Logs a message with <code>org.apache.log4j.Priority.FATAL</code>.
257      *
258      * @param message to log
259      * @see org.apache.commons.logging.Log#fatal(Object)
260      */

261     public void fatal(Object JavaDoc message) {
262         getLogger().log(FQCN, Priority.FATAL, message, null );
263     }
264
265
266     /**
267      * Logs a message with <code>org.apache.log4j.Priority.FATAL</code>.
268      *
269      * @param message to log
270      * @param t log this cause
271      * @see org.apache.commons.logging.Log#fatal(Object, Throwable)
272      */

273     public void fatal(Object JavaDoc message, Throwable JavaDoc t) {
274         getLogger().log(FQCN, Priority.FATAL, message, t );
275     }
276
277
278     /**
279      * Return the native Logger instance we are using.
280      */

281     public Logger getLogger() {
282         if (logger == null) {
283             logger = Logger.getLogger(name);
284         }
285         return (this.logger);
286     }
287
288
289     /**
290      * Check whether the Log4j Logger used is enabled for <code>DEBUG</code> priority.
291      */

292     public boolean isDebugEnabled() {
293         return getLogger().isDebugEnabled();
294     }
295
296
297      /**
298      * Check whether the Log4j Logger used is enabled for <code>ERROR</code> priority.
299      */

300     public boolean isErrorEnabled() {
301         return getLogger().isEnabledFor(Priority.ERROR);
302     }
303
304
305     /**
306      * Check whether the Log4j Logger used is enabled for <code>FATAL</code> priority.
307      */

308     public boolean isFatalEnabled() {
309         return getLogger().isEnabledFor(Priority.FATAL);
310     }
311
312
313     /**
314      * Check whether the Log4j Logger used is enabled for <code>INFO</code> priority.
315      */

316     public boolean isInfoEnabled() {
317         return getLogger().isInfoEnabled();
318     }
319
320
321     /**
322      * Check whether the Log4j Logger used is enabled for <code>TRACE</code> priority.
323      * When using a log4j version that does not support the TRACE level, this call
324      * will report whether <code>DEBUG</code> is enabled or not.
325      */

326     public boolean isTraceEnabled() {
327         return getLogger().isEnabledFor(traceLevel);
328     }
329
330     /**
331      * Check whether the Log4j Logger used is enabled for <code>WARN</code> priority.
332      */

333     public boolean isWarnEnabled() {
334         return getLogger().isEnabledFor(Priority.WARN);
335     }
336 }
337
Popular Tags