View Javadoc

1   /**
2    * Copyright (c) 2004-2011 QOS.ch
3    * All rights reserved.
4    *
5    * Permission is hereby granted, free  of charge, to any person obtaining
6    * a  copy  of this  software  and  associated  documentation files  (the
7    * "Software"), to  deal in  the Software without  restriction, including
8    * without limitation  the rights to  use, copy, modify,  merge, publish,
9    * distribute,  sublicense, and/or sell  copies of  the Software,  and to
10   * permit persons to whom the Software  is furnished to do so, subject to
11   * the following conditions:
12   *
13   * The  above  copyright  notice  and  this permission  notice  shall  be
14   * included in all copies or substantial portions of the Software.
15   *
16   * THE  SOFTWARE IS  PROVIDED  "AS  IS", WITHOUT  WARRANTY  OF ANY  KIND,
17   * EXPRESS OR  IMPLIED, INCLUDING  BUT NOT LIMITED  TO THE  WARRANTIES OF
18   * MERCHANTABILITY,    FITNESS    FOR    A   PARTICULAR    PURPOSE    AND
19   * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20   * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21   * OF CONTRACT, TORT OR OTHERWISE,  ARISING FROM, OUT OF OR IN CONNECTION
22   * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23   *
24   */
25  package org.slf4j.helpers;
26  
27  import org.slf4j.Logger;
28  import org.slf4j.Marker;
29  
30  /**
31   * A logger implementation which logs via a delegate logger. By default, the delegate is a
32   * {@link NOPLogger}. However, a different delegate can be set at anytime.
33   * <p/>
34   * See also the <a href="http://www.slf4j.org/codes.html#substituteLogger">relevant
35   * error code</a> documentation.
36   *
37   * @author Chetan Mehrotra
38   */
39  public class SubstituteLogger implements Logger {
40  
41      private final String name;
42  
43      private volatile Logger _delegate;
44  
45      public SubstituteLogger(String name) {
46          this.name = name;
47      }
48  
49      public String getName() {
50          return name;
51      }
52  
53      public boolean isTraceEnabled() {
54          return delegate().isTraceEnabled();
55      }
56  
57      public void trace(String msg) {
58          delegate().trace(msg);
59      }
60  
61      public void trace(String format, Object arg) {
62          delegate().trace(format, arg);
63      }
64  
65      public void trace(String format, Object arg1, Object arg2) {
66          delegate().trace(format, arg1, arg2);
67      }
68  
69      public void trace(String format, Object... arguments) {
70          delegate().trace(format, arguments);
71      }
72  
73      public void trace(String msg, Throwable t) {
74          delegate().trace(msg, t);
75      }
76  
77      public boolean isTraceEnabled(Marker marker) {
78          return delegate().isTraceEnabled(marker);
79      }
80  
81      public void trace(Marker marker, String msg) {
82          delegate().trace(marker, msg);
83      }
84  
85      public void trace(Marker marker, String format, Object arg) {
86          delegate().trace(marker, format, arg);
87      }
88  
89      public void trace(Marker marker, String format, Object arg1, Object arg2) {
90          delegate().trace(marker, format, arg1, arg2);
91      }
92  
93      public void trace(Marker marker, String format, Object... arguments) {
94          delegate().trace(marker, format, arguments);
95      }
96  
97      public void trace(Marker marker, String msg, Throwable t) {
98          delegate().trace(marker, msg, t);
99      }
100 
101     public boolean isDebugEnabled() {
102         return delegate().isDebugEnabled();
103     }
104 
105     public void debug(String msg) {
106         delegate().debug(msg);
107     }
108 
109     public void debug(String format, Object arg) {
110         delegate().debug(format, arg);
111     }
112 
113     public void debug(String format, Object arg1, Object arg2) {
114         delegate().debug(format, arg1, arg2);
115     }
116 
117     public void debug(String format, Object... arguments) {
118         delegate().debug(format, arguments);
119     }
120 
121     public void debug(String msg, Throwable t) {
122         delegate().debug(msg, t);
123     }
124 
125     public boolean isDebugEnabled(Marker marker) {
126         return delegate().isDebugEnabled(marker);
127     }
128 
129     public void debug(Marker marker, String msg) {
130         delegate().debug(marker, msg);
131     }
132 
133     public void debug(Marker marker, String format, Object arg) {
134         delegate().debug(marker, format, arg);
135     }
136 
137     public void debug(Marker marker, String format, Object arg1, Object arg2) {
138         delegate().debug(marker, format, arg1, arg2);
139     }
140 
141     public void debug(Marker marker, String format, Object... arguments) {
142         delegate().debug(marker, format, arguments);
143     }
144 
145     public void debug(Marker marker, String msg, Throwable t) {
146         delegate().debug(marker, msg, t);
147     }
148 
149     public boolean isInfoEnabled() {
150         return delegate().isInfoEnabled();
151     }
152 
153     public void info(String msg) {
154         delegate().info(msg);
155     }
156 
157     public void info(String format, Object arg) {
158         delegate().info(format, arg);
159     }
160 
161     public void info(String format, Object arg1, Object arg2) {
162         delegate().info(format, arg1, arg2);
163     }
164 
165     public void info(String format, Object... arguments) {
166         delegate().info(format, arguments);
167     }
168 
169     public void info(String msg, Throwable t) {
170         delegate().info(msg, t);
171     }
172 
173     public boolean isInfoEnabled(Marker marker) {
174         return delegate().isInfoEnabled(marker);
175     }
176 
177     public void info(Marker marker, String msg) {
178         delegate().info(marker, msg);
179     }
180 
181     public void info(Marker marker, String format, Object arg) {
182         delegate().info(marker, format, arg);
183     }
184 
185     public void info(Marker marker, String format, Object arg1, Object arg2) {
186         delegate().info(marker, format, arg1, arg2);
187     }
188 
189     public void info(Marker marker, String format, Object... arguments) {
190         delegate().info(marker, format, arguments);
191     }
192 
193     public void info(Marker marker, String msg, Throwable t) {
194         delegate().info(marker, msg, t);
195     }
196 
197     public boolean isWarnEnabled() {
198         return delegate().isWarnEnabled();
199     }
200 
201     public void warn(String msg) {
202         delegate().warn(msg);
203     }
204 
205     public void warn(String format, Object arg) {
206         delegate().warn(format, arg);
207     }
208 
209     public void warn(String format, Object arg1, Object arg2) {
210         delegate().warn(format, arg1, arg2);
211     }
212 
213     public void warn(String format, Object... arguments) {
214         delegate().warn(format, arguments);
215     }
216 
217     public void warn(String msg, Throwable t) {
218         delegate().warn(msg, t);
219     }
220 
221     public boolean isWarnEnabled(Marker marker) {
222         return delegate().isWarnEnabled(marker);
223     }
224 
225     public void warn(Marker marker, String msg) {
226         delegate().warn(marker, msg);
227     }
228 
229     public void warn(Marker marker, String format, Object arg) {
230         delegate().warn(marker, format, arg);
231     }
232 
233     public void warn(Marker marker, String format, Object arg1, Object arg2) {
234         delegate().warn(marker, format, arg1, arg2);
235     }
236 
237     public void warn(Marker marker, String format, Object... arguments) {
238         delegate().warn(marker, format, arguments);
239     }
240 
241     public void warn(Marker marker, String msg, Throwable t) {
242         delegate().warn(marker, msg, t);
243     }
244 
245     public boolean isErrorEnabled() {
246         return delegate().isErrorEnabled();
247     }
248 
249     public void error(String msg) {
250         delegate().error(msg);
251     }
252 
253     public void error(String format, Object arg) {
254         delegate().error(format, arg);
255     }
256 
257     public void error(String format, Object arg1, Object arg2) {
258         delegate().error(format, arg1, arg2);
259     }
260 
261     public void error(String format, Object... arguments) {
262         delegate().error(format, arguments);
263     }
264 
265     public void error(String msg, Throwable t) {
266         delegate().error(msg, t);
267     }
268 
269     public boolean isErrorEnabled(Marker marker) {
270         return delegate().isErrorEnabled(marker);
271     }
272 
273     public void error(Marker marker, String msg) {
274         delegate().error(marker, msg);
275     }
276 
277     public void error(Marker marker, String format, Object arg) {
278         delegate().error(marker, format, arg);
279     }
280 
281     public void error(Marker marker, String format, Object arg1, Object arg2) {
282         delegate().error(marker, format, arg1, arg2);
283     }
284 
285     public void error(Marker marker, String format, Object... arguments) {
286         delegate().error(marker, format, arguments);
287     }
288 
289     public void error(Marker marker, String msg, Throwable t) {
290         delegate().error(marker, msg, t);
291     }
292 
293     @Override
294     public boolean equals(Object o) {
295         if (this == o) return true;
296         if (o == null || getClass() != o.getClass()) return false;
297 
298         SubstituteLogger that = (SubstituteLogger) o;
299 
300         if (!name.equals(that.name)) return false;
301 
302         return true;
303     }
304 
305     @Override
306     public int hashCode() {
307         return name.hashCode();
308     }
309 
310     /**
311      * Return the delegate logger instance if set. Otherwise, return a {@link NOPLogger}
312      * instance.
313      */
314     Logger delegate() {
315         return _delegate != null ? _delegate : NOPLogger.NOP_LOGGER;
316     }
317 
318     /**
319      * Typically called after the {@link org.slf4j.LoggerFactory} initialization phase is completed.
320      * @param delegate
321      */
322     public void setDelegate(Logger delegate) {
323         this._delegate = delegate;
324     }
325 }