查看本类的 API文档回源码主页即时通讯网 - 即时通讯开发者社区!
1   /*
2    * Copyright 2012 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 io.netty.util.internal.ObjectUtil;
19  import io.netty.util.internal.StringUtil;
20  
21  import java.io.ObjectStreamException;
22  import java.io.Serializable;
23  
24  /**
25   * A skeletal implementation of {@link InternalLogger}.  This class implements
26   * all methods that have a {@link InternalLogLevel} parameter by default to call
27   * specific logger methods such as {@link #info(String)} or {@link #isInfoEnabled()}.
28   */
29  public abstract class AbstractInternalLogger implements InternalLogger, Serializable {
30  
31      private static final long serialVersionUID = -6382972526573193470L;
32  
33      static final String EXCEPTION_MESSAGE = "Unexpected exception:";
34  
35      private final String name;
36  
37      /**
38       * Creates a new instance.
39       */
40      protected AbstractInternalLogger(String name) {
41          this.name = ObjectUtil.checkNotNull(name, "name");
42      }
43  
44      @Override
45      public String name() {
46          return name;
47      }
48  
49      @Override
50      public boolean isEnabled(InternalLogLevel level) {
51          switch (level) {
52          case TRACE:
53              return isTraceEnabled();
54          case DEBUG:
55              return isDebugEnabled();
56          case INFO:
57              return isInfoEnabled();
58          case WARN:
59              return isWarnEnabled();
60          case ERROR:
61              return isErrorEnabled();
62          default:
63              throw new Error();
64          }
65      }
66  
67      @Override
68      public void trace(Throwable t) {
69          trace(EXCEPTION_MESSAGE, t);
70      }
71  
72      @Override
73      public void debug(Throwable t) {
74          debug(EXCEPTION_MESSAGE, t);
75      }
76  
77      @Override
78      public void info(Throwable t) {
79          info(EXCEPTION_MESSAGE, t);
80      }
81  
82      @Override
83      public void warn(Throwable t) {
84          warn(EXCEPTION_MESSAGE, t);
85      }
86  
87      @Override
88      public void error(Throwable t) {
89          error(EXCEPTION_MESSAGE, t);
90      }
91  
92      @Override
93      public void log(InternalLogLevel level, String msg, Throwable cause) {
94          switch (level) {
95          case TRACE:
96              trace(msg, cause);
97              break;
98          case DEBUG:
99              debug(msg, cause);
100             break;
101         case INFO:
102             info(msg, cause);
103             break;
104         case WARN:
105             warn(msg, cause);
106             break;
107         case ERROR:
108             error(msg, cause);
109             break;
110         default:
111             throw new Error();
112         }
113     }
114 
115     @Override
116     public void log(InternalLogLevel level, Throwable cause) {
117         switch (level) {
118             case TRACE:
119                 trace(cause);
120                 break;
121             case DEBUG:
122                 debug(cause);
123                 break;
124             case INFO:
125                 info(cause);
126                 break;
127             case WARN:
128                 warn(cause);
129                 break;
130             case ERROR:
131                 error(cause);
132                 break;
133             default:
134                 throw new Error();
135         }
136     }
137 
138     @Override
139     public void log(InternalLogLevel level, String msg) {
140         switch (level) {
141         case TRACE:
142             trace(msg);
143             break;
144         case DEBUG:
145             debug(msg);
146             break;
147         case INFO:
148             info(msg);
149             break;
150         case WARN:
151             warn(msg);
152             break;
153         case ERROR:
154             error(msg);
155             break;
156         default:
157             throw new Error();
158         }
159     }
160 
161     @Override
162     public void log(InternalLogLevel level, String format, Object arg) {
163         switch (level) {
164         case TRACE:
165             trace(format, arg);
166             break;
167         case DEBUG:
168             debug(format, arg);
169             break;
170         case INFO:
171             info(format, arg);
172             break;
173         case WARN:
174             warn(format, arg);
175             break;
176         case ERROR:
177             error(format, arg);
178             break;
179         default:
180             throw new Error();
181         }
182     }
183 
184     @Override
185     public void log(InternalLogLevel level, String format, Object argA, Object argB) {
186         switch (level) {
187         case TRACE:
188             trace(format, argA, argB);
189             break;
190         case DEBUG:
191             debug(format, argA, argB);
192             break;
193         case INFO:
194             info(format, argA, argB);
195             break;
196         case WARN:
197             warn(format, argA, argB);
198             break;
199         case ERROR:
200             error(format, argA, argB);
201             break;
202         default:
203             throw new Error();
204         }
205     }
206 
207     @Override
208     public void log(InternalLogLevel level, String format, Object... arguments) {
209         switch (level) {
210         case TRACE:
211             trace(format, arguments);
212             break;
213         case DEBUG:
214             debug(format, arguments);
215             break;
216         case INFO:
217             info(format, arguments);
218             break;
219         case WARN:
220             warn(format, arguments);
221             break;
222         case ERROR:
223             error(format, arguments);
224             break;
225         default:
226             throw new Error();
227         }
228     }
229 
230     protected Object readResolve() throws ObjectStreamException {
231         return InternalLoggerFactory.getInstance(name());
232     }
233 
234     @Override
235     public String toString() {
236         return StringUtil.simpleClassName(this) + '(' + name() + ')';
237     }
238 }