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    *   http://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.StringUtil;
19  
20  import java.io.ObjectStreamException;
21  import java.io.Serializable;
22  
23  /**
24   * A skeletal implementation of {@link InternalLogger}.  This class implements
25   * all methods that have a {@link InternalLogLevel} parameter by default to call
26   * specific logger methods such as {@link #info(String)} or {@link #isInfoEnabled()}.
27   */
28  public abstract class AbstractInternalLogger implements InternalLogger, Serializable {
29  
30      private static final long serialVersionUID = -6382972526573193470L;
31  
32      private final String name;
33  
34      /**
35       * Creates a new instance.
36       */
37      protected AbstractInternalLogger(String name) {
38          if (name == null) {
39              throw new NullPointerException("name");
40          }
41          this.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 log(InternalLogLevel level, String msg, Throwable cause) {
69          switch (level) {
70          case TRACE:
71              trace(msg, cause);
72              break;
73          case DEBUG:
74              debug(msg, cause);
75              break;
76          case INFO:
77              info(msg, cause);
78              break;
79          case WARN:
80              warn(msg, cause);
81              break;
82          case ERROR:
83              error(msg, cause);
84              break;
85          default:
86              throw new Error();
87          }
88      }
89  
90      @Override
91      public void log(InternalLogLevel level, String msg) {
92          switch (level) {
93          case TRACE:
94              trace(msg);
95              break;
96          case DEBUG:
97              debug(msg);
98              break;
99          case INFO:
100             info(msg);
101             break;
102         case WARN:
103             warn(msg);
104             break;
105         case ERROR:
106             error(msg);
107             break;
108         default:
109             throw new Error();
110         }
111     }
112 
113     @Override
114     public void log(InternalLogLevel level, String format, Object arg) {
115         switch (level) {
116         case TRACE:
117             trace(format, arg);
118             break;
119         case DEBUG:
120             debug(format, arg);
121             break;
122         case INFO:
123             info(format, arg);
124             break;
125         case WARN:
126             warn(format, arg);
127             break;
128         case ERROR:
129             error(format, arg);
130             break;
131         default:
132             throw new Error();
133         }
134     }
135 
136     @Override
137     public void log(InternalLogLevel level, String format, Object argA, Object argB) {
138         switch (level) {
139         case TRACE:
140             trace(format, argA, argB);
141             break;
142         case DEBUG:
143             debug(format, argA, argB);
144             break;
145         case INFO:
146             info(format, argA, argB);
147             break;
148         case WARN:
149             warn(format, argA, argB);
150             break;
151         case ERROR:
152             error(format, argA, argB);
153             break;
154         default:
155             throw new Error();
156         }
157     }
158 
159     @Override
160     public void log(InternalLogLevel level, String format, Object... arguments) {
161         switch (level) {
162         case TRACE:
163             trace(format, arguments);
164             break;
165         case DEBUG:
166             debug(format, arguments);
167             break;
168         case INFO:
169             info(format, arguments);
170             break;
171         case WARN:
172             warn(format, arguments);
173             break;
174         case ERROR:
175             error(format, arguments);
176             break;
177         default:
178             throw new Error();
179         }
180     }
181 
182     protected Object readResolve() throws ObjectStreamException {
183         return InternalLoggerFactory.getInstance(name());
184     }
185 
186     @Override
187     public String toString() {
188         return StringUtil.simpleClassName(this) + '(' + name() + ')';
189     }
190 }