View Javadoc
1   /*
2    * Copyright 2001-2004 The Apache Software Foundation.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * 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,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  
17  package org.apache.commons.logging.impl;
18  
19  import java.io.ObjectStreamException;
20  import java.io.Serializable;
21  
22  import org.apache.commons.logging.Log;
23  import org.slf4j.Logger;
24  import org.slf4j.LoggerFactory;
25  
26  /**
27   * Implementation of {@link Log org.apache.commons.logging.Log} interface which
28   * delegates all processing to a wrapped {@link Logger org.slf4j.Logger}
29   * instance.
30   * 
31   * <p>
32   * JCL's FATAL level is mapped to ERROR. All other levels map one to one.
33   * 
34   * @author Ceki G&uuml;lc&uuml;
35   */
36  public class SLF4JLog implements Log, Serializable {
37  
38      private static final long serialVersionUID = 680728617011167209L;
39  
40      // used to store this logger's name to recreate it after serialization
41      protected String name;
42  
43      // in both Log4jLogger and Jdk14Logger classes in the original JCL, the
44      // logger instance is transient
45      private final transient Logger logger;
46  
47      public SLF4JLog(Logger logger) {
48          this.logger = logger;
49          this.name = logger.getName();
50      }
51  
52      /**
53       * Directly delegates to the wrapped <code>org.slf4j.Logger</code> instance.
54       */
55      public boolean isDebugEnabled() {
56          return logger.isDebugEnabled();
57      }
58  
59      /**
60       * Directly delegates to the wrapped <code>org.slf4j.Logger</code> instance.
61       */
62      public boolean isErrorEnabled() {
63          return logger.isErrorEnabled();
64      }
65  
66      /**
67       * Delegates to the <code>isErrorEnabled<code> method of the wrapped 
68       * <code>org.slf4j.Logger</code> instance.
69       */
70      public boolean isFatalEnabled() {
71          return logger.isErrorEnabled();
72      }
73  
74      /**
75       * Directly delegates to the wrapped <code>org.slf4j.Logger</code> instance.
76       */
77      public boolean isInfoEnabled() {
78          return logger.isInfoEnabled();
79      }
80  
81      /**
82       * Delegates to the <code>isDebugEnabled<code> method of the wrapped 
83       * <code>org.slf4j.Logger</code> instance.
84       */
85      public boolean isTraceEnabled() {
86          return logger.isTraceEnabled();
87      }
88  
89      /**
90       * Directly delegates to the wrapped <code>org.slf4j.Logger</code> instance.
91       */
92      public boolean isWarnEnabled() {
93          return logger.isWarnEnabled();
94      }
95  
96      /**
97       * Converts the input parameter to String and then delegates to the debug
98       * method of the wrapped <code>org.slf4j.Logger</code> instance.
99       * 
100      * @param message
101      *          the message to log. Converted to {@link String}
102      */
103     public void trace(Object message) {
104         logger.trace(String.valueOf(message));
105     }
106 
107     /**
108      * Converts the first input parameter to String and then delegates to the
109      * debug method of the wrapped <code>org.slf4j.Logger</code> instance.
110      * 
111      * @param message
112      *          the message to log. Converted to {@link String}
113      * @param t
114      *          the exception to log
115      */
116     public void trace(Object message, Throwable t) {
117         logger.trace(String.valueOf(message), t);
118     }
119 
120     /**
121      * Converts the input parameter to String and then delegates to the wrapped
122      * <code>org.slf4j.Logger</code> instance.
123      * 
124      * @param message
125      *          the message to log. Converted to {@link String}
126      */
127     public void debug(Object message) {
128         logger.debug(String.valueOf(message));
129     }
130 
131     /**
132      * Converts the first input parameter to String and then delegates to the
133      * wrapped <code>org.slf4j.Logger</code> instance.
134      * 
135      * @param message
136      *          the message to log. Converted to {@link String}
137      * @param t
138      *          the exception to log
139      */
140     public void debug(Object message, Throwable t) {
141         logger.debug(String.valueOf(message), t);
142     }
143 
144     /**
145      * Converts the input parameter to String and then delegates to the wrapped
146      * <code>org.slf4j.Logger</code> instance.
147      * 
148      * @param message
149      *          the message to log. Converted to {@link String}
150      */
151     public void info(Object message) {
152         logger.info(String.valueOf(message));
153     }
154 
155     /**
156      * Converts the first input parameter to String and then delegates to the
157      * wrapped <code>org.slf4j.Logger</code> instance.
158      * 
159      * @param message
160      *          the message to log. Converted to {@link String}
161      * @param t
162      *          the exception to log
163      */
164     public void info(Object message, Throwable t) {
165         logger.info(String.valueOf(message), t);
166     }
167 
168     /**
169      * Converts the input parameter to String and then delegates to the wrapped
170      * <code>org.slf4j.Logger</code> instance.
171      * 
172      * @param message
173      *          the message to log. Converted to {@link String}
174      */
175     public void warn(Object message) {
176         logger.warn(String.valueOf(message));
177     }
178 
179     /**
180      * Converts the first input parameter to String and then delegates to the
181      * wrapped <code>org.slf4j.Logger</code> instance.
182      * 
183      * @param message
184      *          the message to log. Converted to {@link String}
185      * @param t
186      *          the exception to log
187      */
188     public void warn(Object message, Throwable t) {
189         logger.warn(String.valueOf(message), t);
190     }
191 
192     /**
193      * Converts the input parameter to String and then delegates to the wrapped
194      * <code>org.slf4j.Logger</code> instance.
195      * 
196      * @param message
197      *          the message to log. Converted to {@link String}
198      */
199     public void error(Object message) {
200         logger.error(String.valueOf(message));
201     }
202 
203     /**
204      * Converts the first input parameter to String and then delegates to the
205      * wrapped <code>org.slf4j.Logger</code> instance.
206      * 
207      * @param message
208      *          the message to log. Converted to {@link String}
209      * @param t
210      *          the exception to log
211      */
212     public void error(Object message, Throwable t) {
213         logger.error(String.valueOf(message), t);
214     }
215 
216     /**
217      * Converts the input parameter to String and then delegates to the error
218      * method of the wrapped <code>org.slf4j.Logger</code> instance.
219      * 
220      * @param message
221      *          the message to log. Converted to {@link String}
222      */
223     public void fatal(Object message) {
224         logger.error(String.valueOf(message));
225     }
226 
227     /**
228      * Converts the first input parameter to String and then delegates to the
229      * error method of the wrapped <code>org.slf4j.Logger</code> instance.
230      * 
231      * @param message
232      *          the message to log. Converted to {@link String}
233      * @param t
234      *          the exception to log
235      */
236     public void fatal(Object message, Throwable t) {
237         logger.error(String.valueOf(message), t);
238     }
239 
240     /**
241      * Replace this instance with a homonymous (same name) logger returned by
242      * LoggerFactory. Note that this method is only called during deserialization.
243      * 
244      * @return logger with same name as returned by LoggerFactory
245      * @throws ObjectStreamException
246      */
247     protected Object readResolve() throws ObjectStreamException {
248         Logger logger = LoggerFactory.getLogger(this.name);
249         return new SLF4JLog(logger);
250     }
251 }