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  import org.slf4j.spi.LocationAwareLogger;
26  
27  /**
28   * Implementation of {@link Log org.apache.commons.logging.Log} interface which
29   * delegates all processing to a wrapped {@link Logger org.slf4j.Logger}
30   * instance.
31   * 
32   * <p>
33   * JCL's FATAL level is mapped to ERROR. All other levels map one to one.
34   * 
35   * @author Ceki G&uuml;lc&uuml;
36   */
37  public class SLF4JLocationAwareLog implements Log, Serializable {
38  
39      private static final long serialVersionUID = -2379157579039314822L;
40  
41      // used to store this logger's name to recreate it after serialization
42      protected String name;
43  
44      // in both Log4jLogger and Jdk14Logger classes in the original JCL, the
45      // logger instance is transient
46      private final transient LocationAwareLogger logger;
47  
48      private static final String FQCN = SLF4JLocationAwareLog.class.getName();
49  
50      public SLF4JLocationAwareLog(LocationAwareLogger logger) {
51          this.logger = logger;
52          this.name = logger.getName();
53      }
54  
55      /**
56       * Delegates to the <code>isTraceEnabled<code> method of the wrapped 
57       * <code>org.slf4j.Logger</code> instance.
58       */
59      public boolean isTraceEnabled() {
60          return logger.isTraceEnabled();
61      }
62  
63      /**
64       * Directly delegates to the wrapped <code>org.slf4j.Logger</code> instance.
65       */
66      public boolean isDebugEnabled() {
67          return logger.isDebugEnabled();
68      }
69  
70      /**
71       * Directly delegates to the wrapped <code>org.slf4j.Logger</code> instance.
72       */
73      public boolean isInfoEnabled() {
74          return logger.isInfoEnabled();
75      }
76  
77      /**
78       * Directly delegates to the wrapped <code>org.slf4j.Logger</code> instance.
79       */
80      public boolean isWarnEnabled() {
81          return logger.isWarnEnabled();
82      }
83  
84      /**
85       * Directly delegates to the wrapped <code>org.slf4j.Logger</code> instance.
86       */
87      public boolean isErrorEnabled() {
88          return logger.isErrorEnabled();
89      }
90  
91      /**
92       * Delegates to the <code>isErrorEnabled<code> method of the wrapped 
93       * <code>org.slf4j.Logger</code> instance.
94       */
95      public boolean isFatalEnabled() {
96          return logger.isErrorEnabled();
97      }
98  
99      /**
100      * Converts the input parameter to String and then delegates to the debug
101      * method of the wrapped <code>org.slf4j.Logger</code> instance.
102      * 
103      * @param message
104      *          the message to log. Converted to {@link String}
105      */
106     public void trace(Object message) {
107         if (isTraceEnabled()) {
108             logger.log(null, FQCN, LocationAwareLogger.TRACE_INT, String.valueOf(message), null, null);
109         }
110     }
111 
112     /**
113      * Converts the first input parameter to String and then delegates to the
114      * debug method of the wrapped <code>org.slf4j.Logger</code> instance.
115      * 
116      * @param message
117      *          the message to log. Converted to {@link String}
118      * @param t
119      *          the exception to log
120      */
121     public void trace(Object message, Throwable t) {
122         if (isTraceEnabled()) {
123             logger.log(null, FQCN, LocationAwareLogger.TRACE_INT, String.valueOf(message), null, t);
124         }
125     }
126 
127     /**
128      * Converts the input parameter to String and then delegates to the wrapped
129      * <code>org.slf4j.Logger</code> instance.
130      * 
131      * @param message
132      *          the message to log. Converted to {@link String}
133      */
134     public void debug(Object message) {
135         if (isDebugEnabled()) {
136             logger.log(null, FQCN, LocationAwareLogger.DEBUG_INT, String.valueOf(message), null, null);
137         }
138     }
139 
140     /**
141      * Converts the first input parameter to String and then delegates to the
142      * wrapped <code>org.slf4j.Logger</code> instance.
143      * 
144      * @param message
145      *          the message to log. Converted to {@link String}
146      * @param t
147      *          the exception to log
148      */
149     public void debug(Object message, Throwable t) {
150         if (isDebugEnabled()) {
151             logger.log(null, FQCN, LocationAwareLogger.DEBUG_INT, String.valueOf(message), null, t);
152         }
153     }
154 
155     /**
156      * Converts the input parameter to String and then delegates to the wrapped
157      * <code>org.slf4j.Logger</code> instance.
158      * 
159      * @param message
160      *          the message to log. Converted to {@link String}
161      */
162     public void info(Object message) {
163         if (isInfoEnabled()) {
164             logger.log(null, FQCN, LocationAwareLogger.INFO_INT, String.valueOf(message), null, null);
165         }
166     }
167 
168     /**
169      * Converts the first input parameter to String and then delegates to the
170      * wrapped <code>org.slf4j.Logger</code> instance.
171      * 
172      * @param message
173      *          the message to log. Converted to {@link String}
174      * @param t
175      *          the exception to log
176      */
177     public void info(Object message, Throwable t) {
178         if (isInfoEnabled()) {
179             logger.log(null, FQCN, LocationAwareLogger.INFO_INT, String.valueOf(message), null, t);
180         }
181     }
182 
183     /**
184      * Converts the input parameter to String and then delegates to the wrapped
185      * <code>org.slf4j.Logger</code> instance.
186      * 
187      * @param message
188      *          the message to log. Converted to {@link String}
189      */
190     public void warn(Object message) {
191         if (isWarnEnabled()) {
192             logger.log(null, FQCN, LocationAwareLogger.WARN_INT, String.valueOf(message), null, null);
193         }
194     }
195 
196     /**
197      * Converts the first input parameter to String and then delegates to the
198      * wrapped <code>org.slf4j.Logger</code> instance.
199      * 
200      * @param message
201      *          the message to log. Converted to {@link String}
202      * @param t
203      *          the exception to log
204      */
205     public void warn(Object message, Throwable t) {
206         if (isWarnEnabled()) {
207             logger.log(null, FQCN, LocationAwareLogger.WARN_INT, String.valueOf(message), null, t);
208         }
209     }
210 
211     /**
212      * Converts the input parameter to String and then delegates to the wrapped
213      * <code>org.slf4j.Logger</code> instance.
214      * 
215      * @param message
216      *          the message to log. Converted to {@link String}
217      */
218     public void error(Object message) {
219         if (isErrorEnabled()) {
220             logger.log(null, FQCN, LocationAwareLogger.ERROR_INT, String.valueOf(message), null, null);
221         }
222     }
223 
224     /**
225      * Converts the first input parameter to String and then delegates to the
226      * wrapped <code>org.slf4j.Logger</code> instance.
227      * 
228      * @param message
229      *          the message to log. Converted to {@link String}
230      * @param t
231      *          the exception to log
232      */
233     public void error(Object message, Throwable t) {
234         if (isErrorEnabled()) {
235             logger.log(null, FQCN, LocationAwareLogger.ERROR_INT, String.valueOf(message), null, t);
236         }
237     }
238 
239     /**
240      * Converts the input parameter to String and then delegates to the error
241      * method of the wrapped <code>org.slf4j.Logger</code> instance.
242      * 
243      * @param message
244      *          the message to log. Converted to {@link String}
245      */
246     public void fatal(Object message) {
247         if (isErrorEnabled()) {
248             logger.log(null, FQCN, LocationAwareLogger.ERROR_INT, String.valueOf(message), null, null);
249         }
250     }
251 
252     /**
253      * Converts the first input parameter to String and then delegates to the
254      * error method of the wrapped <code>org.slf4j.Logger</code> instance.
255      * 
256      * @param message
257      *          the message to log. Converted to {@link String}
258      * @param t
259      *          the exception to log
260      */
261     public void fatal(Object message, Throwable t) {
262         if (isErrorEnabled()) {
263             logger.log(null, FQCN, LocationAwareLogger.ERROR_INT, String.valueOf(message), null, t);
264         }
265     }
266 
267     /**
268      * Replace this instance with a homonymous (same name) logger returned by
269      * LoggerFactory. Note that this method is only called during deserialization.
270      * 
271      * @return logger with same name as returned by LoggerFactory
272      * @throws ObjectStreamException
273      */
274     protected Object readResolve() throws ObjectStreamException {
275         Logger logger = LoggerFactory.getLogger(this.name);
276         return new SLF4JLocationAwareLog((LocationAwareLogger) logger);
277     }
278 }