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 transient LocationAwareLogger logger;
47  
48    private static final String FQCN = SLF4JLocationAwareLog.class.getName();
49  
50    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     logger.log(null, FQCN, LocationAwareLogger.TRACE_INT, String
108         .valueOf(message), null);
109   }
110 
111   /**
112    * Converts the first input parameter to String and then delegates to the
113    * debug method of the wrapped <code>org.slf4j.Logger</code> instance.
114    * 
115    * @param message
116    *          the message to log. Converted to {@link String}
117    * @param t
118    *          the exception to log
119    */
120   public void trace(Object message, Throwable t) {
121     logger.log(null, FQCN, LocationAwareLogger.TRACE_INT, String
122         .valueOf(message), t);
123   }
124 
125   /**
126    * Converts the input parameter to String and then delegates to the wrapped
127    * <code>org.slf4j.Logger</code> instance.
128    * 
129    * @param message
130    *          the message to log. Converted to {@link String}
131    */
132   public void debug(Object message) {
133     logger.log(null, FQCN, LocationAwareLogger.DEBUG_INT, String
134         .valueOf(message), null);
135   }
136 
137   /**
138    * Converts the first input parameter to String and then delegates to the
139    * wrapped <code>org.slf4j.Logger</code> instance.
140    * 
141    * @param message
142    *          the message to log. Converted to {@link String}
143    * @param t
144    *          the exception to log
145    */
146   public void debug(Object message, Throwable t) {
147     logger.log(null, FQCN, LocationAwareLogger.DEBUG_INT, String
148         .valueOf(message), t);
149   }
150 
151   /**
152    * Converts the input parameter to String and then delegates to the wrapped
153    * <code>org.slf4j.Logger</code> instance.
154    * 
155    * @param message
156    *          the message to log. Converted to {@link String}
157    */
158   public void info(Object message) {
159     logger.log(null, FQCN, LocationAwareLogger.INFO_INT, String
160         .valueOf(message), null);
161   }
162 
163   /**
164    * Converts the first input parameter to String and then delegates to the
165    * wrapped <code>org.slf4j.Logger</code> instance.
166    * 
167    * @param message
168    *          the message to log. Converted to {@link String}
169    * @param t
170    *          the exception to log
171    */
172   public void info(Object message, Throwable t) {
173     logger.log(null, FQCN, LocationAwareLogger.INFO_INT, String
174         .valueOf(message), t);
175   }
176 
177   /**
178    * Converts the input parameter to String and then delegates to the wrapped
179    * <code>org.slf4j.Logger</code> instance.
180    * 
181    * @param message
182    *          the message to log. Converted to {@link String}
183    */
184   public void warn(Object message) {
185     logger.log(null, FQCN, LocationAwareLogger.WARN_INT, String
186         .valueOf(message), null);
187   }
188 
189   /**
190    * Converts the first input parameter to String and then delegates to the
191    * wrapped <code>org.slf4j.Logger</code> instance.
192    * 
193    * @param message
194    *          the message to log. Converted to {@link String}
195    * @param t
196    *          the exception to log
197    */
198   public void warn(Object message, Throwable t) {
199     logger.log(null, FQCN, LocationAwareLogger.WARN_INT, String
200         .valueOf(message), t);
201   }
202 
203   /**
204    * Converts the input parameter to String and then delegates to the wrapped
205    * <code>org.slf4j.Logger</code> instance.
206    * 
207    * @param message
208    *          the message to log. Converted to {@link String}
209    */
210   public void error(Object message) {
211     logger.log(null, FQCN, LocationAwareLogger.ERROR_INT, String
212         .valueOf(message), null);
213   }
214 
215   /**
216    * Converts the first input parameter to String and then delegates to the
217    * wrapped <code>org.slf4j.Logger</code> instance.
218    * 
219    * @param message
220    *          the message to log. Converted to {@link String}
221    * @param t
222    *          the exception to log
223    */
224   public void error(Object message, Throwable t) {
225     logger.log(null, FQCN, LocationAwareLogger.ERROR_INT, String
226         .valueOf(message), t);
227   }
228 
229   /**
230    * Converts the input parameter to String and then delegates to the error
231    * method of the wrapped <code>org.slf4j.Logger</code> instance.
232    * 
233    * @param message
234    *          the message to log. Converted to {@link String}
235    */
236   public void fatal(Object message) {
237     logger.log(null, FQCN, LocationAwareLogger.ERROR_INT, String
238         .valueOf(message), null);
239   }
240 
241   /**
242    * Converts the first input parameter to String and then delegates to the
243    * error method of the wrapped <code>org.slf4j.Logger</code> instance.
244    * 
245    * @param message
246    *          the message to log. Converted to {@link String}
247    * @param t
248    *          the exception to log
249    */
250   public void fatal(Object message, Throwable t) {
251     logger.log(null, FQCN, LocationAwareLogger.ERROR_INT, String
252         .valueOf(message), t);
253   }
254 
255   /**
256    * Replace this instance with a homonymous (same name) logger returned by
257    * LoggerFactory. Note that this method is only called during deserialization.
258    * 
259    * @return logger with same name as returned by LoggerFactory
260    * @throws ObjectStreamException
261    */
262   protected Object readResolve() throws ObjectStreamException {
263     Logger logger = LoggerFactory.getLogger(this.name);
264     return new SLF4JLocationAwareLog((LocationAwareLogger) logger);
265   }
266 }