Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
AccessibleLog4jLoggerAdapter |
|
| 1.7575757575757576;1.758 |
1 | /* | |
2 | * Copyright (c) MuleSoft, Inc. All rights reserved. http://www.mulesoft.com | |
3 | * The software in this package is published under the terms of the CPAL v1.0 | |
4 | * license, a copy of which has been included with this distribution in the | |
5 | * LICENSE.txt file. | |
6 | */ | |
7 | package org.mule.module.logging; | |
8 | ||
9 | import java.io.Serializable; | |
10 | ||
11 | import org.apache.log4j.Level; | |
12 | import org.slf4j.Marker; | |
13 | import org.slf4j.helpers.FormattingTuple; | |
14 | import org.slf4j.helpers.MarkerIgnoringBase; | |
15 | import org.slf4j.helpers.MessageFormatter; | |
16 | import org.slf4j.spi.LocationAwareLogger; | |
17 | ||
18 | /** | |
19 | * A copy & paste of the {@link AccessibleLog4jLoggerAdapter}, just making it | |
20 | * non-final. | |
21 | */ | |
22 | 0 | public class AccessibleLog4jLoggerAdapter extends MarkerIgnoringBase implements LocationAwareLogger, Serializable |
23 | { | |
24 | ||
25 | private static final long serialVersionUID = 6182834493563598289L; | |
26 | ||
27 | final transient org.apache.log4j.Logger logger; | |
28 | ||
29 | /** | |
30 | * Following the pattern discussed in pages 162 through 168 of "The complete | |
31 | * log4j manual". | |
32 | */ | |
33 | 0 | final static String FQCN = AccessibleLog4jLoggerAdapter.class.getName(); |
34 | ||
35 | // Does the log4j version in use recognize the TRACE level? | |
36 | // The trace level was introduced in log4j 1.2.12. | |
37 | final boolean traceCapable; | |
38 | ||
39 | // WARN: Log4jLoggerAdapter constructor should have only package access so | |
40 | // that | |
41 | // only Log4jLoggerFactory be able to create one. | |
42 | AccessibleLog4jLoggerAdapter(org.apache.log4j.Logger logger) | |
43 | 0 | { |
44 | 0 | this.logger = logger; |
45 | 0 | this.name = logger.getName(); |
46 | 0 | traceCapable = isTraceCapable(); |
47 | 0 | } |
48 | ||
49 | private boolean isTraceCapable() | |
50 | { | |
51 | try | |
52 | { | |
53 | 0 | logger.isTraceEnabled(); |
54 | 0 | return true; |
55 | } | |
56 | 0 | catch (NoSuchMethodError e) |
57 | { | |
58 | 0 | return false; |
59 | } | |
60 | } | |
61 | ||
62 | /** | |
63 | * Is this logger instance enabled for the TRACE level? | |
64 | * | |
65 | * @return True if this Logger is enabled for level TRACE, false otherwise. | |
66 | */ | |
67 | public boolean isTraceEnabled() | |
68 | { | |
69 | 0 | if (traceCapable) |
70 | { | |
71 | 0 | return logger.isTraceEnabled(); |
72 | } | |
73 | else | |
74 | { | |
75 | 0 | return logger.isDebugEnabled(); |
76 | } | |
77 | } | |
78 | ||
79 | /** | |
80 | * Log a message object at level TRACE. | |
81 | * | |
82 | * @param msg - the message object to be logged | |
83 | */ | |
84 | public void trace(String msg) | |
85 | { | |
86 | 0 | logger.log(FQCN, traceCapable ? Level.TRACE : Level.DEBUG, msg, null); |
87 | 0 | } |
88 | ||
89 | /** | |
90 | * Log a message at level TRACE according to the specified format and | |
91 | * argument. | |
92 | * <p/> | |
93 | * <p> | |
94 | * This form avoids superfluous object creation when the logger is disabled | |
95 | * for level TRACE. | |
96 | * </p> | |
97 | * | |
98 | * @param format the format string | |
99 | * @param arg the argument | |
100 | */ | |
101 | public void trace(String format, Object arg) | |
102 | { | |
103 | 0 | if (isTraceEnabled()) |
104 | { | |
105 | 0 | FormattingTuple ft = MessageFormatter.format(format, arg); |
106 | 0 | logger.log(FQCN, traceCapable ? Level.TRACE : Level.DEBUG, ft |
107 | .getMessage(), ft.getThrowable()); | |
108 | } | |
109 | 0 | } |
110 | ||
111 | /** | |
112 | * Log a message at level TRACE according to the specified format and | |
113 | * arguments. | |
114 | * <p/> | |
115 | * <p> | |
116 | * This form avoids superfluous object creation when the logger is disabled | |
117 | * for the TRACE level. | |
118 | * </p> | |
119 | * | |
120 | * @param format the format string | |
121 | * @param arg1 the first argument | |
122 | * @param arg2 the second argument | |
123 | */ | |
124 | public void trace(String format, Object arg1, Object arg2) | |
125 | { | |
126 | 0 | if (isTraceEnabled()) |
127 | { | |
128 | 0 | FormattingTuple ft = MessageFormatter.format(format, arg1, arg2); |
129 | 0 | logger.log(FQCN, traceCapable ? Level.TRACE : Level.DEBUG, ft |
130 | .getMessage(), ft.getThrowable()); | |
131 | } | |
132 | 0 | } |
133 | ||
134 | /** | |
135 | * Log a message at level TRACE according to the specified format and | |
136 | * arguments. | |
137 | * <p/> | |
138 | * <p> | |
139 | * This form avoids superfluous object creation when the logger is disabled | |
140 | * for the TRACE level. | |
141 | * </p> | |
142 | * | |
143 | * @param format the format string | |
144 | * @param argArray an array of arguments | |
145 | */ | |
146 | public void trace(String format, Object[] argArray) | |
147 | { | |
148 | 0 | if (isTraceEnabled()) |
149 | { | |
150 | 0 | FormattingTuple ft = MessageFormatter.arrayFormat(format, argArray); |
151 | 0 | logger.log(FQCN, traceCapable ? Level.TRACE : Level.DEBUG, ft |
152 | .getMessage(), ft.getThrowable()); | |
153 | } | |
154 | 0 | } |
155 | ||
156 | /** | |
157 | * Log an exception (throwable) at level TRACE with an accompanying message. | |
158 | * | |
159 | * @param msg the message accompanying the exception | |
160 | * @param t the exception (throwable) to log | |
161 | */ | |
162 | public void trace(String msg, Throwable t) | |
163 | { | |
164 | 0 | logger.log(FQCN, traceCapable ? Level.TRACE : Level.DEBUG, msg, t); |
165 | 0 | } |
166 | ||
167 | /** | |
168 | * Is this logger instance enabled for the DEBUG level? | |
169 | * | |
170 | * @return True if this Logger is enabled for level DEBUG, false otherwise. | |
171 | */ | |
172 | public boolean isDebugEnabled() | |
173 | { | |
174 | 0 | return logger.isDebugEnabled(); |
175 | } | |
176 | ||
177 | /** | |
178 | * Log a message object at level DEBUG. | |
179 | * | |
180 | * @param msg - the message object to be logged | |
181 | */ | |
182 | public void debug(String msg) | |
183 | { | |
184 | 0 | logger.log(FQCN, Level.DEBUG, msg, null); |
185 | 0 | } |
186 | ||
187 | /** | |
188 | * Log a message at level DEBUG according to the specified format and | |
189 | * argument. | |
190 | * <p/> | |
191 | * <p> | |
192 | * This form avoids superfluous object creation when the logger is disabled | |
193 | * for level DEBUG. | |
194 | * </p> | |
195 | * | |
196 | * @param format the format string | |
197 | * @param arg the argument | |
198 | */ | |
199 | public void debug(String format, Object arg) | |
200 | { | |
201 | 0 | if (logger.isDebugEnabled()) |
202 | { | |
203 | 0 | FormattingTuple ft = MessageFormatter.format(format, arg); |
204 | 0 | logger.log(FQCN, Level.DEBUG, ft.getMessage(), ft.getThrowable()); |
205 | } | |
206 | 0 | } |
207 | ||
208 | /** | |
209 | * Log a message at level DEBUG according to the specified format and | |
210 | * arguments. | |
211 | * <p/> | |
212 | * <p> | |
213 | * This form avoids superfluous object creation when the logger is disabled | |
214 | * for the DEBUG level. | |
215 | * </p> | |
216 | * | |
217 | * @param format the format string | |
218 | * @param arg1 the first argument | |
219 | * @param arg2 the second argument | |
220 | */ | |
221 | public void debug(String format, Object arg1, Object arg2) | |
222 | { | |
223 | 0 | if (logger.isDebugEnabled()) |
224 | { | |
225 | 0 | FormattingTuple ft = MessageFormatter.format(format, arg1, arg2); |
226 | 0 | logger.log(FQCN, Level.DEBUG, ft.getMessage(), ft.getThrowable()); |
227 | } | |
228 | 0 | } |
229 | ||
230 | /** | |
231 | * Log a message at level DEBUG according to the specified format and | |
232 | * arguments. | |
233 | * <p/> | |
234 | * <p> | |
235 | * This form avoids superfluous object creation when the logger is disabled | |
236 | * for the DEBUG level. | |
237 | * </p> | |
238 | * | |
239 | * @param format the format string | |
240 | * @param argArray an array of arguments | |
241 | */ | |
242 | public void debug(String format, Object[] argArray) | |
243 | { | |
244 | 0 | if (logger.isDebugEnabled()) |
245 | { | |
246 | 0 | FormattingTuple ft = MessageFormatter.arrayFormat(format, argArray); |
247 | 0 | logger.log(FQCN, Level.DEBUG, ft.getMessage(), ft.getThrowable()); |
248 | } | |
249 | 0 | } |
250 | ||
251 | /** | |
252 | * Log an exception (throwable) at level DEBUG with an accompanying message. | |
253 | * | |
254 | * @param msg the message accompanying the exception | |
255 | * @param t the exception (throwable) to log | |
256 | */ | |
257 | public void debug(String msg, Throwable t) | |
258 | { | |
259 | 0 | logger.log(FQCN, Level.DEBUG, msg, t); |
260 | 0 | } |
261 | ||
262 | /** | |
263 | * Is this logger instance enabled for the INFO level? | |
264 | * | |
265 | * @return True if this Logger is enabled for the INFO level, false otherwise. | |
266 | */ | |
267 | public boolean isInfoEnabled() | |
268 | { | |
269 | 0 | return logger.isInfoEnabled(); |
270 | } | |
271 | ||
272 | /** | |
273 | * Log a message object at the INFO level. | |
274 | * | |
275 | * @param msg - the message object to be logged | |
276 | */ | |
277 | public void info(String msg) | |
278 | { | |
279 | 0 | logger.log(FQCN, Level.INFO, msg, null); |
280 | 0 | } |
281 | ||
282 | /** | |
283 | * Log a message at level INFO according to the specified format and argument. | |
284 | * <p/> | |
285 | * <p> | |
286 | * This form avoids superfluous object creation when the logger is disabled | |
287 | * for the INFO level. | |
288 | * </p> | |
289 | * | |
290 | * @param format the format string | |
291 | * @param arg the argument | |
292 | */ | |
293 | public void info(String format, Object arg) | |
294 | { | |
295 | 0 | if (logger.isInfoEnabled()) |
296 | { | |
297 | 0 | FormattingTuple ft = MessageFormatter.format(format, arg); |
298 | 0 | logger.log(FQCN, Level.INFO, ft.getMessage(), ft.getThrowable()); |
299 | } | |
300 | 0 | } |
301 | ||
302 | /** | |
303 | * Log a message at the INFO level according to the specified format and | |
304 | * arguments. | |
305 | * <p/> | |
306 | * <p> | |
307 | * This form avoids superfluous object creation when the logger is disabled | |
308 | * for the INFO level. | |
309 | * </p> | |
310 | * | |
311 | * @param format the format string | |
312 | * @param arg1 the first argument | |
313 | * @param arg2 the second argument | |
314 | */ | |
315 | public void info(String format, Object arg1, Object arg2) | |
316 | { | |
317 | 0 | if (logger.isInfoEnabled()) |
318 | { | |
319 | 0 | FormattingTuple ft = MessageFormatter.format(format, arg1, arg2); |
320 | 0 | logger.log(FQCN, Level.INFO, ft.getMessage(), ft.getThrowable()); |
321 | } | |
322 | 0 | } |
323 | ||
324 | /** | |
325 | * Log a message at level INFO according to the specified format and | |
326 | * arguments. | |
327 | * <p/> | |
328 | * <p> | |
329 | * This form avoids superfluous object creation when the logger is disabled | |
330 | * for the INFO level. | |
331 | * </p> | |
332 | * | |
333 | * @param format the format string | |
334 | * @param argArray an array of arguments | |
335 | */ | |
336 | public void info(String format, Object[] argArray) | |
337 | { | |
338 | 0 | if (logger.isInfoEnabled()) |
339 | { | |
340 | 0 | FormattingTuple ft = MessageFormatter.arrayFormat(format, argArray); |
341 | 0 | logger.log(FQCN, Level.INFO, ft.getMessage(), ft.getThrowable()); |
342 | } | |
343 | 0 | } |
344 | ||
345 | /** | |
346 | * Log an exception (throwable) at the INFO level with an accompanying | |
347 | * message. | |
348 | * | |
349 | * @param msg the message accompanying the exception | |
350 | * @param t the exception (throwable) to log | |
351 | */ | |
352 | public void info(String msg, Throwable t) | |
353 | { | |
354 | 0 | logger.log(FQCN, Level.INFO, msg, t); |
355 | 0 | } |
356 | ||
357 | /** | |
358 | * Is this logger instance enabled for the WARN level? | |
359 | * | |
360 | * @return True if this Logger is enabled for the WARN level, false otherwise. | |
361 | */ | |
362 | public boolean isWarnEnabled() | |
363 | { | |
364 | 0 | return logger.isEnabledFor(Level.WARN); |
365 | } | |
366 | ||
367 | /** | |
368 | * Log a message object at the WARN level. | |
369 | * | |
370 | * @param msg - the message object to be logged | |
371 | */ | |
372 | public void warn(String msg) | |
373 | { | |
374 | 0 | logger.log(FQCN, Level.WARN, msg, null); |
375 | 0 | } |
376 | ||
377 | /** | |
378 | * Log a message at the WARN level according to the specified format and | |
379 | * argument. | |
380 | * <p/> | |
381 | * <p> | |
382 | * This form avoids superfluous object creation when the logger is disabled | |
383 | * for the WARN level. | |
384 | * </p> | |
385 | * | |
386 | * @param format the format string | |
387 | * @param arg the argument | |
388 | */ | |
389 | public void warn(String format, Object arg) | |
390 | { | |
391 | 0 | if (logger.isEnabledFor(Level.WARN)) |
392 | { | |
393 | 0 | FormattingTuple ft = MessageFormatter.format(format, arg); |
394 | 0 | logger.log(FQCN, Level.WARN, ft.getMessage(), ft.getThrowable()); |
395 | } | |
396 | 0 | } |
397 | ||
398 | /** | |
399 | * Log a message at the WARN level according to the specified format and | |
400 | * arguments. | |
401 | * <p/> | |
402 | * <p> | |
403 | * This form avoids superfluous object creation when the logger is disabled | |
404 | * for the WARN level. | |
405 | * </p> | |
406 | * | |
407 | * @param format the format string | |
408 | * @param arg1 the first argument | |
409 | * @param arg2 the second argument | |
410 | */ | |
411 | public void warn(String format, Object arg1, Object arg2) | |
412 | { | |
413 | 0 | if (logger.isEnabledFor(Level.WARN)) |
414 | { | |
415 | 0 | FormattingTuple ft = MessageFormatter.format(format, arg1, arg2); |
416 | 0 | logger.log(FQCN, Level.WARN, ft.getMessage(), ft.getThrowable()); |
417 | } | |
418 | 0 | } |
419 | ||
420 | /** | |
421 | * Log a message at level WARN according to the specified format and | |
422 | * arguments. | |
423 | * <p/> | |
424 | * <p> | |
425 | * This form avoids superfluous object creation when the logger is disabled | |
426 | * for the WARN level. | |
427 | * </p> | |
428 | * | |
429 | * @param format the format string | |
430 | * @param argArray an array of arguments | |
431 | */ | |
432 | public void warn(String format, Object[] argArray) | |
433 | { | |
434 | 0 | if (logger.isEnabledFor(Level.WARN)) |
435 | { | |
436 | 0 | FormattingTuple ft = MessageFormatter.arrayFormat(format, argArray); |
437 | 0 | logger.log(FQCN, Level.WARN, ft.getMessage(), ft.getThrowable()); |
438 | } | |
439 | 0 | } |
440 | ||
441 | /** | |
442 | * Log an exception (throwable) at the WARN level with an accompanying | |
443 | * message. | |
444 | * | |
445 | * @param msg the message accompanying the exception | |
446 | * @param t the exception (throwable) to log | |
447 | */ | |
448 | public void warn(String msg, Throwable t) | |
449 | { | |
450 | 0 | logger.log(FQCN, Level.WARN, msg, t); |
451 | 0 | } |
452 | ||
453 | /** | |
454 | * Is this logger instance enabled for level ERROR? | |
455 | * | |
456 | * @return True if this Logger is enabled for level ERROR, false otherwise. | |
457 | */ | |
458 | public boolean isErrorEnabled() | |
459 | { | |
460 | 0 | return logger.isEnabledFor(Level.ERROR); |
461 | } | |
462 | ||
463 | /** | |
464 | * Log a message object at the ERROR level. | |
465 | * | |
466 | * @param msg - the message object to be logged | |
467 | */ | |
468 | public void error(String msg) | |
469 | { | |
470 | 0 | logger.log(FQCN, Level.ERROR, msg, null); |
471 | 0 | } |
472 | ||
473 | /** | |
474 | * Log a message at the ERROR level according to the specified format and | |
475 | * argument. | |
476 | * <p/> | |
477 | * <p> | |
478 | * This form avoids superfluous object creation when the logger is disabled | |
479 | * for the ERROR level. | |
480 | * </p> | |
481 | * | |
482 | * @param format the format string | |
483 | * @param arg the argument | |
484 | */ | |
485 | public void error(String format, Object arg) | |
486 | { | |
487 | 0 | if (logger.isEnabledFor(Level.ERROR)) |
488 | { | |
489 | 0 | FormattingTuple ft = MessageFormatter.format(format, arg); |
490 | 0 | logger.log(FQCN, Level.ERROR, ft.getMessage(), ft.getThrowable()); |
491 | } | |
492 | 0 | } |
493 | ||
494 | /** | |
495 | * Log a message at the ERROR level according to the specified format and | |
496 | * arguments. | |
497 | * <p/> | |
498 | * <p> | |
499 | * This form avoids superfluous object creation when the logger is disabled | |
500 | * for the ERROR level. | |
501 | * </p> | |
502 | * | |
503 | * @param format the format string | |
504 | * @param arg1 the first argument | |
505 | * @param arg2 the second argument | |
506 | */ | |
507 | public void error(String format, Object arg1, Object arg2) | |
508 | { | |
509 | 0 | if (logger.isEnabledFor(Level.ERROR)) |
510 | { | |
511 | 0 | FormattingTuple ft = MessageFormatter.format(format, arg1, arg2); |
512 | 0 | logger.log(FQCN, Level.ERROR, ft.getMessage(), ft.getThrowable()); |
513 | } | |
514 | 0 | } |
515 | ||
516 | /** | |
517 | * Log a message at level ERROR according to the specified format and | |
518 | * arguments. | |
519 | * <p/> | |
520 | * <p> | |
521 | * This form avoids superfluous object creation when the logger is disabled | |
522 | * for the ERROR level. | |
523 | * </p> | |
524 | * | |
525 | * @param format the format string | |
526 | * @param argArray an array of arguments | |
527 | */ | |
528 | public void error(String format, Object[] argArray) | |
529 | { | |
530 | 0 | if (logger.isEnabledFor(Level.ERROR)) |
531 | { | |
532 | 0 | FormattingTuple ft = MessageFormatter.arrayFormat(format, argArray); |
533 | 0 | logger.log(FQCN, Level.ERROR, ft.getMessage(), ft.getThrowable()); |
534 | } | |
535 | 0 | } |
536 | ||
537 | /** | |
538 | * Log an exception (throwable) at the ERROR level with an accompanying | |
539 | * message. | |
540 | * | |
541 | * @param msg the message accompanying the exception | |
542 | * @param t the exception (throwable) to log | |
543 | */ | |
544 | public void error(String msg, Throwable t) | |
545 | { | |
546 | 0 | logger.log(FQCN, Level.ERROR, msg, t); |
547 | 0 | } |
548 | ||
549 | public void log(Marker marker, String callerFQCN, int level, String msg, | |
550 | Object[] argArray, Throwable t) | |
551 | { | |
552 | Level log4jLevel; | |
553 | 0 | switch (level) |
554 | { | |
555 | case LocationAwareLogger.TRACE_INT: | |
556 | 0 | log4jLevel = traceCapable ? Level.TRACE : Level.DEBUG; |
557 | 0 | break; |
558 | case LocationAwareLogger.DEBUG_INT: | |
559 | 0 | log4jLevel = Level.DEBUG; |
560 | 0 | break; |
561 | case LocationAwareLogger.INFO_INT: | |
562 | 0 | log4jLevel = Level.INFO; |
563 | 0 | break; |
564 | case LocationAwareLogger.WARN_INT: | |
565 | 0 | log4jLevel = Level.WARN; |
566 | 0 | break; |
567 | case LocationAwareLogger.ERROR_INT: | |
568 | 0 | log4jLevel = Level.ERROR; |
569 | 0 | break; |
570 | default: | |
571 | 0 | throw new IllegalStateException("Level number " + level |
572 | + " is not recognized."); | |
573 | } | |
574 | 0 | logger.log(callerFQCN, log4jLevel, msg, t); |
575 | 0 | } |
576 | ||
577 | } |