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