查看本类的 API文档回源码主页即时通讯网 - 即时通讯开发者社区!
1   /*
2    * Copyright 2012 The Netty Project
3    *
4    * The Netty Project licenses this file to you under the Apache License,
5    * version 2.0 (the "License"); you may not use this file except in compliance
6    * with the License. You may obtain a copy of the License at:
7    *
8    *   https://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, WITHOUT
12   * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13   * License for the specific language governing permissions and limitations
14   * under the License.
15   */
16  /**
17   * Copyright (c) 2004-2011 QOS.ch
18   * All rights reserved.
19   *
20   * Permission is hereby granted, free  of charge, to any person obtaining
21   * a  copy  of this  software  and  associated  documentation files  (the
22   * "Software"), to  deal in  the Software without  restriction, including
23   * without limitation  the rights to  use, copy, modify,  merge, publish,
24   * distribute,  sublicense, and/or sell  copies of  the Software,  and to
25   * permit persons to whom the Software  is furnished to do so, subject to
26   * the following conditions:
27   *
28   * The  above  copyright  notice  and  this permission  notice  shall  be
29   * included in all copies or substantial portions of the Software.
30   *
31   * THE  SOFTWARE IS  PROVIDED  "AS  IS", WITHOUT  WARRANTY  OF ANY  KIND,
32   * EXPRESS OR  IMPLIED, INCLUDING  BUT NOT LIMITED  TO THE  WARRANTIES OF
33   * MERCHANTABILITY,    FITNESS    FOR    A   PARTICULAR    PURPOSE    AND
34   * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
35   * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
36   * OF CONTRACT, TORT OR OTHERWISE,  ARISING FROM, OUT OF OR IN CONNECTION
37   * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
38   *
39   */
40  package io.netty.util.internal.logging;
41  
42  import org.apache.log4j.Level;
43  import org.apache.log4j.Logger;
44  
45  /**
46   * <a href="https://logging.apache.org/log4j/1.2/index.html">Apache Log4J</a>
47   * logger.
48   */
49  class Log4JLogger extends AbstractInternalLogger {
50  
51      private static final long serialVersionUID = 2851357342488183058L;
52  
53      final transient Logger logger;
54  
55      /**
56       * Following the pattern discussed in pages 162 through 168 of "The complete
57       * log4j manual".
58       */
59      static final String FQCN = Log4JLogger.class.getName();
60  
61      // Does the log4j version in use recognize the TRACE level?
62      // The trace level was introduced in log4j 1.2.12.
63      final boolean traceCapable;
64  
65      Log4JLogger(Logger logger) {
66          super(logger.getName());
67          this.logger = logger;
68          traceCapable = isTraceCapable();
69      }
70  
71      private boolean isTraceCapable() {
72          try {
73              logger.isTraceEnabled();
74              return true;
75          } catch (NoSuchMethodError ignored) {
76              return false;
77          }
78      }
79  
80      /**
81       * Is this logger instance enabled for the TRACE level?
82       *
83       * @return True if this Logger is enabled for level TRACE, false otherwise.
84       */
85      @Override
86      public boolean isTraceEnabled() {
87          if (traceCapable) {
88              return logger.isTraceEnabled();
89          } else {
90              return logger.isDebugEnabled();
91          }
92      }
93  
94      /**
95       * Log a message object at level TRACE.
96       *
97       * @param msg
98       *          - the message object to be logged
99       */
100     @Override
101     public void trace(String msg) {
102         logger.log(FQCN, traceCapable ? Level.TRACE : Level.DEBUG, msg, null);
103     }
104 
105     /**
106      * Log a message at level TRACE according to the specified format and
107      * argument.
108      *
109      * <p>
110      * This form avoids superfluous object creation when the logger is disabled
111      * for level TRACE.
112      * </p>
113      *
114      * @param format
115      *          the format string
116      * @param arg
117      *          the argument
118      */
119     @Override
120     public void trace(String format, Object arg) {
121         if (isTraceEnabled()) {
122             FormattingTuple ft = MessageFormatter.format(format, arg);
123             logger.log(FQCN, traceCapable ? Level.TRACE : Level.DEBUG, ft
124                     .getMessage(), ft.getThrowable());
125         }
126     }
127 
128     /**
129      * Log a message at level TRACE according to the specified format and
130      * arguments.
131      *
132      * <p>
133      * This form avoids superfluous object creation when the logger is disabled
134      * for the TRACE level.
135      * </p>
136      *
137      * @param format
138      *          the format string
139      * @param argA
140      *          the first argument
141      * @param argB
142      *          the second argument
143      */
144     @Override
145     public void trace(String format, Object argA, Object argB) {
146         if (isTraceEnabled()) {
147             FormattingTuple ft = MessageFormatter.format(format, argA, argB);
148             logger.log(FQCN, traceCapable ? Level.TRACE : Level.DEBUG, ft
149                     .getMessage(), ft.getThrowable());
150         }
151     }
152 
153     /**
154      * Log a message at level TRACE according to the specified format and
155      * arguments.
156      *
157      * <p>
158      * This form avoids superfluous object creation when the logger is disabled
159      * for the TRACE level.
160      * </p>
161      *
162      * @param format
163      *          the format string
164      * @param arguments
165      *          an array of arguments
166      */
167     @Override
168     public void trace(String format, Object... arguments) {
169         if (isTraceEnabled()) {
170             FormattingTuple ft = MessageFormatter.arrayFormat(format, arguments);
171             logger.log(FQCN, traceCapable ? Level.TRACE : Level.DEBUG, ft
172                     .getMessage(), ft.getThrowable());
173         }
174     }
175 
176     /**
177      * Log an exception (throwable) at level TRACE with an accompanying message.
178      *
179      * @param msg
180      *          the message accompanying the exception
181      * @param t
182      *          the exception (throwable) to log
183      */
184     @Override
185     public void trace(String msg, Throwable t) {
186         logger.log(FQCN, traceCapable ? Level.TRACE : Level.DEBUG, msg, t);
187     }
188 
189     /**
190      * Is this logger instance enabled for the DEBUG level?
191      *
192      * @return True if this Logger is enabled for level DEBUG, false otherwise.
193      */
194     @Override
195     public boolean isDebugEnabled() {
196         return logger.isDebugEnabled();
197     }
198 
199     /**
200      * Log a message object at level DEBUG.
201      *
202      * @param msg
203      *          - the message object to be logged
204      */
205     @Override
206     public void debug(String msg) {
207         logger.log(FQCN, Level.DEBUG, msg, null);
208     }
209 
210     /**
211      * Log a message at level DEBUG according to the specified format and
212      * argument.
213      *
214      * <p>
215      * This form avoids superfluous object creation when the logger is disabled
216      * for level DEBUG.
217      * </p>
218      *
219      * @param format
220      *          the format string
221      * @param arg
222      *          the argument
223      */
224     @Override
225     public void debug(String format, Object arg) {
226         if (logger.isDebugEnabled()) {
227             FormattingTuple ft = MessageFormatter.format(format, arg);
228             logger.log(FQCN, Level.DEBUG, ft.getMessage(), ft.getThrowable());
229         }
230     }
231 
232     /**
233      * Log a message at level DEBUG according to the specified format and
234      * arguments.
235      *
236      * <p>
237      * This form avoids superfluous object creation when the logger is disabled
238      * for the DEBUG level.
239      * </p>
240      *
241      * @param format
242      *          the format string
243      * @param argA
244      *          the first argument
245      * @param argB
246      *          the second argument
247      */
248     @Override
249     public void debug(String format, Object argA, Object argB) {
250         if (logger.isDebugEnabled()) {
251             FormattingTuple ft = MessageFormatter.format(format, argA, argB);
252             logger.log(FQCN, Level.DEBUG, ft.getMessage(), ft.getThrowable());
253         }
254     }
255 
256     /**
257      * Log a message at level DEBUG according to the specified format and
258      * arguments.
259      *
260      * <p>
261      * This form avoids superfluous object creation when the logger is disabled
262      * for the DEBUG level.
263      * </p>
264      *
265      * @param format
266      *          the format string
267      * @param arguments an array of arguments
268      */
269     @Override
270     public void debug(String format, Object... arguments) {
271         if (logger.isDebugEnabled()) {
272             FormattingTuple ft = MessageFormatter.arrayFormat(format, arguments);
273             logger.log(FQCN, Level.DEBUG, ft.getMessage(), ft.getThrowable());
274         }
275     }
276 
277     /**
278      * Log an exception (throwable) at level DEBUG with an accompanying message.
279      *
280      * @param msg
281      *          the message accompanying the exception
282      * @param t
283      *          the exception (throwable) to log
284      */
285     @Override
286     public void debug(String msg, Throwable t) {
287         logger.log(FQCN, Level.DEBUG, msg, t);
288     }
289 
290     /**
291      * Is this logger instance enabled for the INFO level?
292      *
293      * @return True if this Logger is enabled for the INFO level, false otherwise.
294      */
295     @Override
296     public boolean isInfoEnabled() {
297         return logger.isInfoEnabled();
298     }
299 
300     /**
301      * Log a message object at the INFO level.
302      *
303      * @param msg
304      *          - the message object to be logged
305      */
306     @Override
307     public void info(String msg) {
308         logger.log(FQCN, Level.INFO, msg, null);
309     }
310 
311     /**
312      * Log a message at level INFO according to the specified format and argument.
313      *
314      * <p>
315      * This form avoids superfluous object creation when the logger is disabled
316      * for the INFO level.
317      * </p>
318      *
319      * @param format
320      *          the format string
321      * @param arg
322      *          the argument
323      */
324     @Override
325     public void info(String format, Object arg) {
326         if (logger.isInfoEnabled()) {
327             FormattingTuple ft = MessageFormatter.format(format, arg);
328             logger.log(FQCN, Level.INFO, ft.getMessage(), ft.getThrowable());
329         }
330     }
331 
332     /**
333      * Log a message at the INFO level according to the specified format and
334      * arguments.
335      *
336      * <p>
337      * This form avoids superfluous object creation when the logger is disabled
338      * for the INFO level.
339      * </p>
340      *
341      * @param format
342      *          the format string
343      * @param argA
344      *          the first argument
345      * @param argB
346      *          the second argument
347      */
348     @Override
349     public void info(String format, Object argA, Object argB) {
350         if (logger.isInfoEnabled()) {
351             FormattingTuple ft = MessageFormatter.format(format, argA, argB);
352             logger.log(FQCN, Level.INFO, ft.getMessage(), ft.getThrowable());
353         }
354     }
355 
356     /**
357      * Log a message at level INFO according to the specified format and
358      * arguments.
359      *
360      * <p>
361      * This form avoids superfluous object creation when the logger is disabled
362      * for the INFO level.
363      * </p>
364      *
365      * @param format
366      *          the format string
367      * @param argArray
368      *          an array of arguments
369      */
370     @Override
371     public void info(String format, Object... argArray) {
372         if (logger.isInfoEnabled()) {
373             FormattingTuple ft = MessageFormatter.arrayFormat(format, argArray);
374             logger.log(FQCN, Level.INFO, ft.getMessage(), ft.getThrowable());
375         }
376     }
377 
378     /**
379      * Log an exception (throwable) at the INFO level with an accompanying
380      * message.
381      *
382      * @param msg
383      *          the message accompanying the exception
384      * @param t
385      *          the exception (throwable) to log
386      */
387     @Override
388     public void info(String msg, Throwable t) {
389         logger.log(FQCN, Level.INFO, msg, t);
390     }
391 
392     /**
393      * Is this logger instance enabled for the WARN level?
394      *
395      * @return True if this Logger is enabled for the WARN level, false otherwise.
396      */
397     @Override
398     public boolean isWarnEnabled() {
399         return logger.isEnabledFor(Level.WARN);
400     }
401 
402     /**
403      * Log a message object at the WARN level.
404      *
405      * @param msg
406      *          - the message object to be logged
407      */
408     @Override
409     public void warn(String msg) {
410         logger.log(FQCN, Level.WARN, msg, null);
411     }
412 
413     /**
414      * Log a message at the WARN level according to the specified format and
415      * argument.
416      *
417      * <p>
418      * This form avoids superfluous object creation when the logger is disabled
419      * for the WARN level.
420      * </p>
421      *
422      * @param format
423      *          the format string
424      * @param arg
425      *          the argument
426      */
427     @Override
428     public void warn(String format, Object arg) {
429         if (logger.isEnabledFor(Level.WARN)) {
430             FormattingTuple ft = MessageFormatter.format(format, arg);
431             logger.log(FQCN, Level.WARN, ft.getMessage(), ft.getThrowable());
432         }
433     }
434 
435     /**
436      * Log a message at the WARN level according to the specified format and
437      * arguments.
438      *
439      * <p>
440      * This form avoids superfluous object creation when the logger is disabled
441      * for the WARN level.
442      * </p>
443      *
444      * @param format
445      *          the format string
446      * @param argA
447      *          the first argument
448      * @param argB
449      *          the second argument
450      */
451     @Override
452     public void warn(String format, Object argA, Object argB) {
453         if (logger.isEnabledFor(Level.WARN)) {
454             FormattingTuple ft = MessageFormatter.format(format, argA, argB);
455             logger.log(FQCN, Level.WARN, ft.getMessage(), ft.getThrowable());
456         }
457     }
458 
459     /**
460      * Log a message at level WARN according to the specified format and
461      * arguments.
462      *
463      * <p>
464      * This form avoids superfluous object creation when the logger is disabled
465      * for the WARN level.
466      * </p>
467      *
468      * @param format
469      *          the format string
470      * @param argArray
471      *          an array of arguments
472      */
473     @Override
474     public void warn(String format, Object... argArray) {
475         if (logger.isEnabledFor(Level.WARN)) {
476             FormattingTuple ft = MessageFormatter.arrayFormat(format, argArray);
477             logger.log(FQCN, Level.WARN, ft.getMessage(), ft.getThrowable());
478         }
479     }
480 
481     /**
482      * Log an exception (throwable) at the WARN level with an accompanying
483      * message.
484      *
485      * @param msg
486      *          the message accompanying the exception
487      * @param t
488      *          the exception (throwable) to log
489      */
490     @Override
491     public void warn(String msg, Throwable t) {
492         logger.log(FQCN, Level.WARN, msg, t);
493     }
494 
495     /**
496      * Is this logger instance enabled for level ERROR?
497      *
498      * @return True if this Logger is enabled for level ERROR, false otherwise.
499      */
500     @Override
501     public boolean isErrorEnabled() {
502         return logger.isEnabledFor(Level.ERROR);
503     }
504 
505     /**
506      * Log a message object at the ERROR level.
507      *
508      * @param msg
509      *          - the message object to be logged
510      */
511     @Override
512     public void error(String msg) {
513         logger.log(FQCN, Level.ERROR, msg, null);
514     }
515 
516     /**
517      * Log a message at the ERROR level according to the specified format and
518      * argument.
519      *
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
526      *          the format string
527      * @param arg
528      *          the argument
529      */
530     @Override
531     public void error(String format, Object arg) {
532         if (logger.isEnabledFor(Level.ERROR)) {
533             FormattingTuple ft = MessageFormatter.format(format, arg);
534             logger.log(FQCN, Level.ERROR, ft.getMessage(), ft.getThrowable());
535         }
536     }
537 
538     /**
539      * Log a message at the ERROR level according to the specified format and
540      * arguments.
541      *
542      * <p>
543      * This form avoids superfluous object creation when the logger is disabled
544      * for the ERROR level.
545      * </p>
546      *
547      * @param format
548      *          the format string
549      * @param argA
550      *          the first argument
551      * @param argB
552      *          the second argument
553      */
554     @Override
555     public void error(String format, Object argA, Object argB) {
556         if (logger.isEnabledFor(Level.ERROR)) {
557             FormattingTuple ft = MessageFormatter.format(format, argA, argB);
558             logger.log(FQCN, Level.ERROR, ft.getMessage(), ft.getThrowable());
559         }
560     }
561 
562     /**
563      * Log a message at level ERROR according to the specified format and
564      * arguments.
565      *
566      * <p>
567      * This form avoids superfluous object creation when the logger is disabled
568      * for the ERROR level.
569      * </p>
570      *
571      * @param format
572      *          the format string
573      * @param argArray
574      *          an array of arguments
575      */
576     @Override
577     public void error(String format, Object... argArray) {
578         if (logger.isEnabledFor(Level.ERROR)) {
579             FormattingTuple ft = MessageFormatter.arrayFormat(format, argArray);
580             logger.log(FQCN, Level.ERROR, ft.getMessage(), ft.getThrowable());
581         }
582     }
583 
584     /**
585      * Log an exception (throwable) at the ERROR level with an accompanying
586      * message.
587      *
588      * @param msg
589      *          the message accompanying the exception
590      * @param t
591      *          the exception (throwable) to log
592      */
593     @Override
594     public void error(String msg, Throwable t) {
595         logger.log(FQCN, Level.ERROR, msg, t);
596     }
597 }