1
2
3
4
5
6
7
8
9
10
11
12
13
14
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
36
37
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
46 return true;
47 } catch (SecurityException ignore) {
48
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 }