1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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 * @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 {
50
51
52
53 /*** The fully qualified name of the Log4JLogger class. */
54 private static final String FQCN = Log4JLogger.class.getName();
55
56 /*** Log to this logger */
57 private transient Logger logger = null;
58
59 /*** Logger name */
60 private String name = null;
61
62 private static Priority traceLevel;
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78 static {
79 if (!Priority.class.isAssignableFrom(Level.class)) {
80
81 throw new InstantiationError("Log4J 1.2 not available");
82 }
83
84
85
86
87
88 try {
89 traceLevel = (Priority) Level.class.getDeclaredField("TRACE").get(null);
90 } catch(Exception ex) {
91
92 traceLevel = Priority.DEBUG;
93 }
94 }
95
96
97
98
99 public Log4JLogger() {
100 }
101
102
103 /***
104 * Base constructor.
105 */
106 public Log4JLogger(String 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
121
122
123
124
125
126
127
128
129
130
131
132
133
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 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 message, Throwable 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 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 message, Throwable 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 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 message, Throwable 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 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 message, Throwable 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 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 message, Throwable 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 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 message, Throwable 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 }