View Javadoc
1   /*
2    * Copyright 2016 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    *   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, 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  package io.netty.util.internal.logging;
17  
18  
19  import org.apache.logging.log4j.Level;
20  import org.apache.logging.log4j.Logger;
21  import org.apache.logging.log4j.spi.ExtendedLogger;
22  import org.apache.logging.log4j.spi.ExtendedLoggerWrapper;
23  
24  class Log4J2Logger extends ExtendedLoggerWrapper implements InternalLogger {
25  
26      private static final long serialVersionUID = 5485418394879791397L;
27  
28      /** {@linkplain AbstractInternalLogger#EXCEPTION_MESSAGE} */
29      private static final String EXCEPTION_MESSAGE = "Unexpected exception:";
30  
31      Log4J2Logger(Logger logger) {
32          super((ExtendedLogger) logger, logger.getName(), logger.getMessageFactory());
33      }
34  
35      @Override
36      public String name() {
37          return getName();
38      }
39  
40      @Override
41      public void trace(Throwable t) {
42          log(Level.TRACE, EXCEPTION_MESSAGE, t);
43      }
44  
45      @Override
46      public void debug(Throwable t) {
47          log(Level.DEBUG, EXCEPTION_MESSAGE, t);
48      }
49  
50      @Override
51      public void info(Throwable t) {
52          log(Level.INFO, EXCEPTION_MESSAGE, t);
53      }
54  
55      @Override
56      public void warn(Throwable t) {
57          log(Level.WARN, EXCEPTION_MESSAGE, t);
58      }
59  
60      @Override
61      public void error(Throwable t) {
62          log(Level.ERROR, EXCEPTION_MESSAGE, t);
63      }
64  
65      @Override
66      public boolean isEnabled(InternalLogLevel level) {
67          return isEnabled(toLevel(level));
68      }
69  
70      @Override
71      public void log(InternalLogLevel level, String msg) {
72          log(toLevel(level), msg);
73      }
74  
75      @Override
76      public void log(InternalLogLevel level, String format, Object arg) {
77          log(toLevel(level), format, arg);
78      }
79  
80      @Override
81      public void log(InternalLogLevel level, String format, Object argA, Object argB) {
82          log(toLevel(level), format, argA, argB);
83      }
84  
85      @Override
86      public void log(InternalLogLevel level, String format, Object... arguments) {
87          log(toLevel(level), format, arguments);
88      }
89  
90      @Override
91      public void log(InternalLogLevel level, String msg, Throwable t) {
92          log(toLevel(level), msg, t);
93      }
94  
95      @Override
96      public void log(InternalLogLevel level, Throwable t) {
97          log(toLevel(level), EXCEPTION_MESSAGE, t);
98      }
99  
100     protected Level toLevel(InternalLogLevel level) {
101         switch (level) {
102             case INFO:
103                 return Level.INFO;
104             case DEBUG:
105                 return Level.DEBUG;
106             case WARN:
107                 return Level.WARN;
108             case ERROR:
109                 return Level.ERROR;
110             case TRACE:
111                 return Level.TRACE;
112             default:
113                 throw new Error();
114         }
115     }
116 }