001: /*
002: * Copyright (c) 2004-2005 SLF4J.ORG
003: * Copyright (c) 2004-2005 QOS.ch
004: *
005: * All rights reserved.
006: *
007: * Permission is hereby granted, free of charge, to any person obtaining
008: * a copy of this software and associated documentation files (the
009: * "Software"), to deal in the Software without restriction, including
010: * without limitation the rights to use, copy, modify, merge, publish,
011: * distribute, and/or sell copies of the Software, and to permit persons
012: * to whom the Software is furnished to do so, provided that the above
013: * copyright notice(s) and this permission notice appear in all copies of
014: * the Software and that both the above copyright notice(s) and this
015: * permission notice appear in supporting documentation.
016: *
017: * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
018: * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
019: * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT
020: * OF THIRD PARTY RIGHTS. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
021: * HOLDERS INCLUDED IN THIS NOTICE BE LIABLE FOR ANY CLAIM, OR ANY
022: * SPECIAL INDIRECT OR CONSEQUENTIAL DAMAGES, OR ANY DAMAGES WHATSOEVER
023: * RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF
024: * CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
025: * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
026: *
027: * Except as contained in this notice, the name of a copyright holder
028: * shall not be used in advertising or otherwise to promote the sale, use
029: * or other dealings in this Software without prior written authorization
030: * of the copyright holder.
031: *
032: */
033:
034: package org.slf4j.impl;
035:
036: import org.apache.log4j.Level;
037: import org.slf4j.Logger;
038: import org.slf4j.Marker;
039: import org.slf4j.helpers.MarkerIgnoringBase;
040: import org.slf4j.helpers.MessageFormatter;
041: import org.slf4j.spi.LocationAwareLogger;
042:
043: /**
044: * A wrapper over {@link org.apache.log4j.Logger
045: * org.apache.log4j.Logger} in conformance with the {@link Logger}
046: * interface. Note that the logging levels mentioned in this class
047: * refer to those defined in the
048: * <a href="http://logging.apache.org/log4j/docs/api/org/apache/log4j/Level.html"><code>org.apache.log4j.Level</code></a>
049: * class.
050:
051: * @author Ceki Gülcü
052: */
053: public final class Log4jLoggerAdapter extends MarkerIgnoringBase
054: implements LocationAwareLogger {
055: final org.apache.log4j.Logger logger;
056:
057: /**
058: * Following the pattern discussed in pages 162 through 168 of
059: * "The complete log4j manual".
060: */
061: final static String FQCN = Log4jLoggerAdapter.class.getName();
062:
063: // WARN: Log4jLoggerAdapter constructor should have only package access so that
064: // only Log4jLoggerFactory be able to create one.
065: Log4jLoggerAdapter(org.apache.log4j.Logger logger) {
066: this .logger = logger;
067: }
068:
069: public String getName() {
070: return logger.getName();
071: }
072:
073: /**
074: * Is this logger instance enabled for the TRACE level?
075: *
076: * @return True if this Logger is enabled for level TRACE, false
077: * otherwise.
078: */
079: public boolean isTraceEnabled() {
080: return logger.isTraceEnabled();
081: }
082:
083: /**
084: * Log a message object at level TRACE.
085: * @param msg - the message object to be logged
086: */
087: public void trace(String msg) {
088: logger.log(FQCN, Level.TRACE, msg, null);
089: }
090:
091: /**
092: * Log a message at level TRACE according to the specified format and
093: * argument.
094: *
095: * <p>This form avoids superfluous object creation when the logger
096: * is disabled for level TRACE. </p>
097: *
098: * @param format the format string
099: * @param arg the argument
100: */
101: public void trace(String format, Object arg) {
102: if (logger.isTraceEnabled()) {
103: String msgStr = MessageFormatter.format(format, arg);
104: logger.log(FQCN, Level.TRACE, msgStr, null);
105: }
106: }
107:
108: /**
109: * Log a message at level TRACE according to the specified format and
110: * arguments.
111: *
112: * <p>This form avoids superfluous object creation when the logger
113: * is disabled for the TRACE level. </p>
114: *
115: * @param format the format string
116: * @param arg1 the first argument
117: * @param arg2 the second argument
118: */
119: public void trace(String format, Object arg1, Object arg2) {
120: if (logger.isTraceEnabled()) {
121: String msgStr = MessageFormatter.format(format, arg1, arg2);
122: logger.log(FQCN, Level.TRACE, msgStr, null);
123: }
124: }
125:
126: /**
127: * Log a message at level TRACE according to the specified format and
128: * arguments.
129: *
130: * <p>This form avoids superfluous object creation when the logger
131: * is disabled for the TRACE level. </p>
132: *
133: * @param format the format string
134: * @param argArray an array of arguments
135: */
136: public void trace(String format, Object[] argArray) {
137: if (logger.isTraceEnabled()) {
138: String msgStr = MessageFormatter.arrayFormat(format,
139: argArray);
140: logger.log(FQCN, Level.TRACE, msgStr, null);
141: }
142: }
143:
144: /**
145: * Log an exception (throwable) at level TRACE with an
146: * accompanying message.
147: *
148: * @param msg the message accompanying the exception
149: * @param t the exception (throwable) to log
150: */
151: public void trace(String msg, Throwable t) {
152: logger.log(FQCN, Level.TRACE, msg, t);
153: }
154:
155: /**
156: * Is this logger instance enabled for the DEBUG level?
157: *
158: * @return True if this Logger is enabled for level DEBUG, false
159: * otherwise.
160: */
161: public boolean isDebugEnabled() {
162: return logger.isDebugEnabled();
163: }
164:
165: /**
166: * Log a message object at level DEBUG.
167: * @param msg - the message object to be logged
168: */
169: public void debug(String msg) {
170: logger.log(FQCN, Level.DEBUG, msg, null);
171: }
172:
173: /**
174: * Log a message at level DEBUG according to the specified format and
175: * argument.
176: *
177: * <p>This form avoids superfluous object creation when the logger
178: * is disabled for level DEBUG. </p>
179: *
180: * @param format the format string
181: * @param arg the argument
182: */
183: public void debug(String format, Object arg) {
184: if (logger.isDebugEnabled()) {
185: String msgStr = MessageFormatter.format(format, arg);
186: logger.log(FQCN, Level.DEBUG, msgStr, null);
187: }
188: }
189:
190: /**
191: * Log a message at level DEBUG according to the specified format and
192: * arguments.
193: *
194: * <p>This form avoids superfluous object creation when the logger
195: * is disabled for the DEBUG level. </p>
196: *
197: * @param format the format string
198: * @param arg1 the first argument
199: * @param arg2 the second argument
200: */
201: public void debug(String format, Object arg1, Object arg2) {
202: if (logger.isDebugEnabled()) {
203: String msgStr = MessageFormatter.format(format, arg1, arg2);
204: logger.log(FQCN, Level.DEBUG, msgStr, null);
205: }
206: }
207:
208: /**
209: * Log a message at level DEBUG according to the specified format and
210: * arguments.
211: *
212: * <p>This form avoids superfluous object creation when the logger
213: * is disabled for the DEBUG level. </p>
214: *
215: * @param format the format string
216: * @param argArray an array of arguments
217: */
218: public void debug(String format, Object[] argArray) {
219: if (logger.isDebugEnabled()) {
220: String msgStr = MessageFormatter.arrayFormat(format,
221: argArray);
222: logger.log(FQCN, Level.DEBUG, msgStr, null);
223: }
224: }
225:
226: /**
227: * Log an exception (throwable) at level DEBUG with an
228: * accompanying message.
229: *
230: * @param msg the message accompanying the exception
231: * @param t the exception (throwable) to log
232: */
233: public void debug(String msg, Throwable t) {
234: logger.log(FQCN, Level.DEBUG, msg, t);
235: }
236:
237: /**
238: * Is this logger instance enabled for the INFO level?
239: *
240: * @return True if this Logger is enabled for the INFO level, false
241: * otherwise.
242: */
243: public boolean isInfoEnabled() {
244: return logger.isInfoEnabled();
245: }
246:
247: /**
248: * Log a message object at the INFO level.
249: *
250: * @param msg - the message object to be logged
251: */
252: public void info(String msg) {
253: logger.log(FQCN, Level.INFO, msg, null);
254: }
255:
256: /**
257: * Log a message at level INFO according to the specified format and
258: * argument.
259: *
260: * <p>This form avoids superfluous object creation when the logger
261: * is disabled for the INFO level. </p>
262: *
263: * @param format the format string
264: * @param arg the argument
265: */
266: public void info(String format, Object arg) {
267: if (logger.isInfoEnabled()) {
268: String msgStr = MessageFormatter.format(format, arg);
269: logger.log(FQCN, Level.INFO, msgStr, null);
270: }
271: }
272:
273: /**
274: * Log a message at the INFO level according to the specified format
275: * and arguments.
276: *
277: * <p>This form avoids superfluous object creation when the logger
278: * is disabled for the INFO level. </p>
279: *
280: * @param format the format string
281: * @param arg1 the first argument
282: * @param arg2 the second argument
283: */
284: public void info(String format, Object arg1, Object arg2) {
285: if (logger.isInfoEnabled()) {
286: String msgStr = MessageFormatter.format(format, arg1, arg2);
287: logger.log(FQCN, Level.INFO, msgStr, null);
288: }
289: }
290:
291: /**
292: * Log a message at level INFO according to the specified format and
293: * arguments.
294: *
295: * <p>This form avoids superfluous object creation when the logger
296: * is disabled for the INFO level. </p>
297: *
298: * @param format the format string
299: * @param argArray an array of arguments
300: */
301: public void info(String format, Object[] argArray) {
302: if (logger.isInfoEnabled()) {
303: String msgStr = MessageFormatter.arrayFormat(format,
304: argArray);
305: logger.log(FQCN, Level.INFO, msgStr, null);
306: }
307: }
308:
309: /**
310: * Log an exception (throwable) at the INFO level with an
311: * accompanying message.
312: *
313: * @param msg the message accompanying the exception
314: * @param t the exception (throwable) to log
315: */
316: public void info(String msg, Throwable t) {
317: logger.log(FQCN, Level.INFO, msg, t);
318: }
319:
320: /**
321: * Is this logger instance enabled for the WARN level?
322: *
323: * @return True if this Logger is enabled for the WARN level,
324: * false otherwise.
325: */
326: public boolean isWarnEnabled() {
327: return logger.isEnabledFor(Level.WARN);
328: }
329:
330: /**
331: * Log a message object at the WARN level.
332: *
333: * @param msg - the message object to be logged
334: */
335: public void warn(String msg) {
336: logger.log(FQCN, Level.WARN, msg, null);
337: }
338:
339: /**
340: * Log a message at the WARN level according to the specified
341: * format and argument.
342: *
343: * <p>This form avoids superfluous object creation when the logger
344: * is disabled for the WARN level. </p>
345: *
346: * @param format the format string
347: * @param arg the argument
348: */
349: public void warn(String format, Object arg) {
350: if (logger.isEnabledFor(Level.WARN)) {
351: String msgStr = MessageFormatter.format(format, arg);
352: logger.log(FQCN, Level.WARN, msgStr, null);
353: }
354: }
355:
356: /**
357: * Log a message at the WARN level according to the specified
358: * format and arguments.
359: *
360: * <p>This form avoids superfluous object creation when the logger
361: * is disabled for the WARN level. </p>
362: *
363: * @param format the format string
364: * @param arg1 the first argument
365: * @param arg2 the second argument
366: */
367: public void warn(String format, Object arg1, Object arg2) {
368: if (logger.isEnabledFor(Level.WARN)) {
369: String msgStr = MessageFormatter.format(format, arg1, arg2);
370: logger.log(FQCN, Level.WARN, msgStr, null);
371: }
372: }
373:
374: /**
375: * Log a message at level WARN according to the specified format and
376: * arguments.
377: *
378: * <p>This form avoids superfluous object creation when the logger
379: * is disabled for the WARN level. </p>
380: *
381: * @param format the format string
382: * @param argArray an array of arguments
383: */
384: public void warn(String format, Object[] argArray) {
385: if (logger.isEnabledFor(Level.WARN)) {
386: String msgStr = MessageFormatter.arrayFormat(format,
387: argArray);
388: logger.log(FQCN, Level.WARN, msgStr, null);
389: }
390: }
391:
392: /**
393: * Log an exception (throwable) at the WARN level with an
394: * accompanying message.
395: *
396: * @param msg the message accompanying the exception
397: * @param t the exception (throwable) to log
398: */
399: public void warn(String msg, Throwable t) {
400: logger.log(FQCN, Level.WARN, msg, t);
401: }
402:
403: /**
404: * Is this logger instance enabled for level ERROR?
405: *
406: * @return True if this Logger is enabled for level ERROR, false
407: * otherwise.
408: */
409: public boolean isErrorEnabled() {
410: return logger.isEnabledFor(Level.ERROR);
411: }
412:
413: /**
414: * Log a message object at the ERROR level.
415: *
416: * @param msg - the message object to be logged
417: */
418: public void error(String msg) {
419: logger.log(FQCN, Level.ERROR, msg, null);
420: }
421:
422: /**
423: * Log a message at the ERROR level according to the specified
424: * format and argument.
425: *
426: * <p>This form avoids superfluous object creation when the logger
427: * is disabled for the ERROR level. </p>
428: *
429: * @param format the format string
430: * @param arg the argument
431: */
432: public void error(String format, Object arg) {
433: if (logger.isEnabledFor(Level.ERROR)) {
434: String msgStr = MessageFormatter.format(format, arg);
435: logger.log(FQCN, Level.ERROR, msgStr, null);
436: }
437: }
438:
439: /**
440: * Log a message at the ERROR level according to the specified
441: * format and arguments.
442: *
443: * <p>This form avoids superfluous object creation when the logger
444: * is disabled for the ERROR level. </p>
445: *
446: * @param format the format string
447: * @param arg1 the first argument
448: * @param arg2 the second argument
449: */
450: public void error(String format, Object arg1, Object arg2) {
451: if (logger.isEnabledFor(Level.ERROR)) {
452: String msgStr = MessageFormatter.format(format, arg1, arg2);
453: logger.log(FQCN, Level.ERROR, msgStr, null);
454: }
455: }
456:
457: /**
458: * Log a message at level ERROR according to the specified format and
459: * arguments.
460: *
461: * <p>This form avoids superfluous object creation when the logger
462: * is disabled for the ERROR level. </p>
463: *
464: * @param format the format string
465: * @param argArray an array of arguments
466: */
467: public void error(String format, Object[] argArray) {
468: if (logger.isEnabledFor(Level.ERROR)) {
469: String msgStr = MessageFormatter.arrayFormat(format,
470: argArray);
471: logger.log(FQCN, Level.ERROR, msgStr, null);
472: }
473: }
474:
475: /**
476: * Log an exception (throwable) at the ERROR level with an
477: * accompanying message.
478: *
479: * @param msg the message accompanying the exception
480: * @param t the exception (throwable) to log
481: */
482: public void error(String msg, Throwable t) {
483: logger.log(FQCN, Level.ERROR, msg, t);
484: }
485:
486: public void log(Marker marker, String callerFQCN, int level,
487: String msg, Throwable t) {
488: Level log4jLevel;
489: switch (level) {
490: case LocationAwareLogger.TRACE_INT:
491: log4jLevel = Level.TRACE;
492: break;
493: case LocationAwareLogger.DEBUG_INT:
494: log4jLevel = Level.DEBUG;
495: break;
496: case LocationAwareLogger.INFO_INT:
497: log4jLevel = Level.INFO;
498: break;
499: case LocationAwareLogger.WARN_INT:
500: log4jLevel = Level.WARN;
501: break;
502: case LocationAwareLogger.ERROR_INT:
503: log4jLevel = Level.ERROR;
504: break;
505: default:
506: throw new IllegalStateException("Level number " + level
507: + " is not recognized.");
508: }
509: logger.log(callerFQCN, log4jLevel, msg, t);
510: }
511: }
|