View Javadoc
1   /*
2    * Copyright 2017 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.netty5.util.internal.logging;
17  
18  import org.slf4j.spi.LocationAwareLogger;
19  
20  import static org.slf4j.spi.LocationAwareLogger.DEBUG_INT;
21  import static org.slf4j.spi.LocationAwareLogger.ERROR_INT;
22  import static org.slf4j.spi.LocationAwareLogger.INFO_INT;
23  import static org.slf4j.spi.LocationAwareLogger.TRACE_INT;
24  import static org.slf4j.spi.LocationAwareLogger.WARN_INT;
25  
26  /**
27   * <a href="https://www.slf4j.org/">SLF4J</a> logger which is location aware and so will log the correct origin of the
28   * logging event by filter out the wrapper itself.
29   */
30  final class LocationAwareSlf4JLogger extends AbstractInternalLogger {
31  
32      // IMPORTANT: All our log methods first check if the log level is enabled before call the wrapped
33      // LocationAwareLogger.log(...) method. This is done to reduce GC creation that is caused by varargs.
34  
35      static final String FQCN = LocationAwareSlf4JLogger.class.getName();
36      private static final long serialVersionUID = -8292030083201538180L;
37  
38      private final transient LocationAwareLogger logger;
39  
40      LocationAwareSlf4JLogger(LocationAwareLogger logger) {
41          super(logger.getName());
42          this.logger = logger;
43      }
44  
45      private void log(final int level, final String message) {
46          logger.log(null, FQCN, level, message, null, null);
47      }
48  
49      private void log(final int level, final String message, Throwable cause) {
50          logger.log(null, FQCN, level, message, null, cause);
51      }
52  
53      private void log(final int level, final org.slf4j.helpers.FormattingTuple tuple) {
54          logger.log(null, FQCN, level, tuple.getMessage(), tuple.getArgArray(), tuple.getThrowable());
55      }
56  
57      @Override
58      public boolean isTraceEnabled() {
59          return logger.isTraceEnabled();
60      }
61  
62      @Override
63      public void trace(String msg) {
64          if (isTraceEnabled()) {
65              log(TRACE_INT, msg);
66          }
67      }
68  
69      @Override
70      public void trace(String format, Object arg) {
71          if (isTraceEnabled()) {
72              log(TRACE_INT, org.slf4j.helpers.MessageFormatter.format(format, arg));
73          }
74      }
75  
76      @Override
77      public void trace(String format, Object argA, Object argB) {
78          if (isTraceEnabled()) {
79              log(TRACE_INT, org.slf4j.helpers.MessageFormatter.format(format, argA, argB));
80          }
81      }
82  
83      @Override
84      public void trace(String format, Object... argArray) {
85          if (isTraceEnabled()) {
86              log(TRACE_INT, org.slf4j.helpers.MessageFormatter.arrayFormat(format, argArray));
87          }
88      }
89  
90      @Override
91      public void trace(String msg, Throwable t) {
92          if (isTraceEnabled()) {
93              log(TRACE_INT, msg, t);
94          }
95      }
96  
97      @Override
98      public boolean isDebugEnabled() {
99          return logger.isDebugEnabled();
100     }
101 
102     @Override
103     public void debug(String msg) {
104         if (isDebugEnabled()) {
105             log(DEBUG_INT, msg);
106         }
107     }
108 
109     @Override
110     public void debug(String format, Object arg) {
111         if (isDebugEnabled()) {
112             log(DEBUG_INT, org.slf4j.helpers.MessageFormatter.format(format, arg));
113         }
114     }
115 
116     @Override
117     public void debug(String format, Object argA, Object argB) {
118         if (isDebugEnabled()) {
119             log(DEBUG_INT, org.slf4j.helpers.MessageFormatter.format(format, argA, argB));
120         }
121     }
122 
123     @Override
124     public void debug(String format, Object... argArray) {
125         if (isDebugEnabled()) {
126             log(DEBUG_INT, org.slf4j.helpers.MessageFormatter.arrayFormat(format, argArray));
127         }
128     }
129 
130     @Override
131     public void debug(String msg, Throwable t) {
132         if (isDebugEnabled()) {
133             log(DEBUG_INT, msg, t);
134         }
135     }
136 
137     @Override
138     public boolean isInfoEnabled() {
139         return logger.isInfoEnabled();
140     }
141 
142     @Override
143     public void info(String msg) {
144         if (isInfoEnabled()) {
145             log(INFO_INT, msg);
146         }
147     }
148 
149     @Override
150     public void info(String format, Object arg) {
151         if (isInfoEnabled()) {
152             log(INFO_INT, org.slf4j.helpers.MessageFormatter.format(format, arg));
153         }
154     }
155 
156     @Override
157     public void info(String format, Object argA, Object argB) {
158         if (isInfoEnabled()) {
159             log(INFO_INT, org.slf4j.helpers.MessageFormatter.format(format, argA, argB));
160         }
161     }
162 
163     @Override
164     public void info(String format, Object... argArray) {
165         if (isInfoEnabled()) {
166             log(INFO_INT, org.slf4j.helpers.MessageFormatter.arrayFormat(format, argArray));
167         }
168     }
169 
170     @Override
171     public void info(String msg, Throwable t) {
172         if (isInfoEnabled()) {
173             log(INFO_INT, msg, t);
174         }
175     }
176 
177     @Override
178     public boolean isWarnEnabled() {
179         return logger.isWarnEnabled();
180     }
181 
182     @Override
183     public void warn(String msg) {
184         if (isWarnEnabled()) {
185             log(WARN_INT, msg);
186         }
187     }
188 
189     @Override
190     public void warn(String format, Object arg) {
191         if (isWarnEnabled()) {
192             log(WARN_INT, org.slf4j.helpers.MessageFormatter.format(format, arg));
193         }
194     }
195 
196     @Override
197     public void warn(String format, Object... argArray) {
198         if (isWarnEnabled()) {
199             log(WARN_INT, org.slf4j.helpers.MessageFormatter.arrayFormat(format, argArray));
200         }
201     }
202 
203     @Override
204     public void warn(String format, Object argA, Object argB) {
205         if (isWarnEnabled()) {
206             log(WARN_INT, org.slf4j.helpers.MessageFormatter.format(format, argA, argB));
207         }
208     }
209 
210     @Override
211     public void warn(String msg, Throwable t) {
212         if (isWarnEnabled()) {
213             log(WARN_INT, msg, t);
214         }
215     }
216 
217     @Override
218     public boolean isErrorEnabled() {
219         return logger.isErrorEnabled();
220     }
221 
222     @Override
223     public void error(String msg) {
224         if (isErrorEnabled()) {
225             log(ERROR_INT, msg);
226         }
227     }
228 
229     @Override
230     public void error(String format, Object arg) {
231         if (isErrorEnabled()) {
232             log(ERROR_INT, org.slf4j.helpers.MessageFormatter.format(format, arg));
233         }
234     }
235 
236     @Override
237     public void error(String format, Object argA, Object argB) {
238         if (isErrorEnabled()) {
239             log(ERROR_INT, org.slf4j.helpers.MessageFormatter.format(format, argA, argB));
240         }
241     }
242 
243     @Override
244     public void error(String format, Object... argArray) {
245         if (isErrorEnabled()) {
246             log(ERROR_INT, org.slf4j.helpers.MessageFormatter.arrayFormat(format, argArray));
247         }
248     }
249 
250     @Override
251     public void error(String msg, Throwable t) {
252         if (isErrorEnabled()) {
253             log(ERROR_INT, msg, t);
254         }
255     }
256 }