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    *   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  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  import java.security.AccessController;
25  import java.security.PrivilegedAction;
26  
27  import static io.netty.util.internal.logging.AbstractInternalLogger.EXCEPTION_MESSAGE;
28  
29  class Log4J2Logger extends ExtendedLoggerWrapper implements InternalLogger {
30  
31      private static final long serialVersionUID = 5485418394879791397L;
32      private static final boolean VARARGS_ONLY;
33  
34      static {
35          // Older Log4J2 versions have only log methods that takes the format + varargs. So we should not use
36          // Log4J2 if the version is too old.
37          // See https://github.com/netty/netty/issues/8217
38          VARARGS_ONLY = AccessController.doPrivileged(new PrivilegedAction<Boolean>() {
39              @Override
40              public Boolean run() {
41                  try {
42                      Logger.class.getMethod("debug", String.class, Object.class);
43                      return false;
44                  } catch (NoSuchMethodException ignore) {
45                      // Log4J2 version too old.
46                      return true;
47                  } catch (SecurityException ignore) {
48                      // We could not detect the version so we will use Log4J2 if its on the classpath.
49                      return false;
50                  }
51              }
52          });
53      }
54  
55      Log4J2Logger(Logger logger) {
56          super((ExtendedLogger) logger, logger.getName(), logger.getMessageFactory());
57          if (VARARGS_ONLY) {
58              throw new UnsupportedOperationException("Log4J2 version mismatch");
59          }
60      }
61  
62      @Override
63      public String name() {
64          return getName();
65      }
66  
67      @Override
68      public void trace(Throwable t) {
69          log(Level.TRACE, EXCEPTION_MESSAGE, t);
70      }
71  
72      @Override
73      public void debug(Throwable t) {
74          log(Level.DEBUG, EXCEPTION_MESSAGE, t);
75      }
76  
77      @Override
78      public void info(Throwable t) {
79          log(Level.INFO, EXCEPTION_MESSAGE, t);
80      }
81  
82      @Override
83      public void warn(Throwable t) {
84          log(Level.WARN, EXCEPTION_MESSAGE, t);
85      }
86  
87      @Override
88      public void error(Throwable t) {
89          log(Level.ERROR, EXCEPTION_MESSAGE, t);
90      }
91  
92      @Override
93      public boolean isEnabled(InternalLogLevel level) {
94          return isEnabled(toLevel(level));
95      }
96  
97      @Override
98      public void log(InternalLogLevel level, String msg) {
99          log(toLevel(level), msg);
100     }
101 
102     @Override
103     public void log(InternalLogLevel level, String format, Object arg) {
104         log(toLevel(level), format, arg);
105     }
106 
107     @Override
108     public void log(InternalLogLevel level, String format, Object argA, Object argB) {
109         log(toLevel(level), format, argA, argB);
110     }
111 
112     @Override
113     public void log(InternalLogLevel level, String format, Object... arguments) {
114         log(toLevel(level), format, arguments);
115     }
116 
117     @Override
118     public void log(InternalLogLevel level, String msg, Throwable t) {
119         log(toLevel(level), msg, t);
120     }
121 
122     @Override
123     public void log(InternalLogLevel level, Throwable t) {
124         log(toLevel(level), EXCEPTION_MESSAGE, t);
125     }
126 
127     private static Level toLevel(InternalLogLevel level) {
128         switch (level) {
129             case INFO:
130                 return Level.INFO;
131             case DEBUG:
132                 return Level.DEBUG;
133             case WARN:
134                 return Level.WARN;
135             case ERROR:
136                 return Level.ERROR;
137             case TRACE:
138                 return Level.TRACE;
139             default:
140                 throw new Error();
141         }
142     }
143 }