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