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.Serializable;
20  
21  import org.apache.commons.logging.Log;
22  import org.slf4j.Logger;
23  import org.slf4j.spi.LocationAwareLogger;
24  
25  /**
26   * Implementation of {@link Log org.apache.commons.logging.Log} interface which 
27   * delegates all processing to a wrapped {@link Logger org.slf4j.Logger} instance.
28   * 
29   * <p>JCL's FATAL level is mapped to ERROR. All other levels map one to one.
30   * 
31   * @author Ceki G&uuml;lc&uuml;
32   */
33  public class SLF4JLocationAwareLog implements Log, Serializable {
34  
35    private static final long serialVersionUID = -2379157579039314822L;
36    
37    // in both Log4jLogger and Jdk14Logger classes in the original JCL, the 
38    // logger instance is transient
39    private transient LocationAwareLogger logger;
40  
41    private static final String FQCN = SLF4JLocationAwareLog.class.getName();
42    
43    SLF4JLocationAwareLog(LocationAwareLogger logger) {
44      this.logger = logger;
45    }
46  
47    /**
48     * Delegates to the <code>isTraceEnabled<code> method of the wrapped 
49     * <code>org.slf4j.Logger</code> instance.
50     */
51    public boolean isTraceEnabled() {
52      return logger.isTraceEnabled();
53    }
54    
55    /**
56     * Directly delegates to the wrapped <code>org.slf4j.Logger</code> instance.
57     */
58    public boolean isDebugEnabled() {
59      return logger.isDebugEnabled();
60    }
61    
62    /**
63     * Directly delegates to the wrapped <code>org.slf4j.Logger</code> instance.
64     */
65    public boolean isInfoEnabled() {
66      return logger.isInfoEnabled();
67    }
68  
69    /**
70     * Directly delegates to the wrapped <code>org.slf4j.Logger</code> instance.
71     */
72    public boolean isWarnEnabled() {
73      return logger.isWarnEnabled();
74    }
75    
76    /**
77     * Directly delegates to the wrapped <code>org.slf4j.Logger</code> instance.
78     */
79    public boolean isErrorEnabled() {
80      return logger.isErrorEnabled();
81    }
82  
83    /**
84     * Delegates to the <code>isErrorEnabled<code> method of the wrapped 
85     * <code>org.slf4j.Logger</code> instance.
86     */
87    public boolean isFatalEnabled() {
88      return logger.isErrorEnabled();
89    }
90  
91  
92    /**
93     * Converts the input parameter to String and then delegates to 
94     * the debug method of the wrapped <code>org.slf4j.Logger</code> instance.
95     * 
96     * @param message the message to log. Converted to {@link String}  
97     */
98    public void trace(Object message) {
99      logger.log(null, FQCN, LocationAwareLogger.TRACE_INT, String.valueOf(message), null);
100   }
101 
102   /**
103    * Converts the first input parameter to String and then delegates to 
104    * the debug method of the wrapped <code>org.slf4j.Logger</code> instance.
105    * 
106    * @param message the message to log. Converted to {@link String}  
107    * @param t the exception to log
108    */
109   public void trace(Object message, Throwable t) {
110     logger.log(null, FQCN, LocationAwareLogger.TRACE_INT, String.valueOf(message), t);
111   }
112 
113   /**
114    * Converts the input parameter to String and then delegates to the wrapped 
115    * <code>org.slf4j.Logger</code> instance.
116    * 
117    * @param message the message to log. Converted to {@link String} 
118    */
119   public void debug(Object message) {
120     logger.log(null, FQCN, LocationAwareLogger.DEBUG_INT, String.valueOf(message), null);
121   }
122 
123   /**
124    * Converts the first input parameter to String and then delegates to 
125    * the wrapped <code>org.slf4j.Logger</code> instance.
126    * 
127    * @param message the message to log. Converted to {@link String}  
128    * @param t the exception to log
129    */
130   public void debug(Object message, Throwable t) {
131     logger.log(null, FQCN, LocationAwareLogger.DEBUG_INT, String.valueOf(message), t);
132   }
133 
134   /**
135    * Converts the input parameter to String and then delegates to the wrapped 
136    * <code>org.slf4j.Logger</code> instance.
137    * 
138    * @param message the message to log. Converted to {@link String} 
139    */
140   public void info(Object message) {
141     logger.log(null, FQCN, LocationAwareLogger.INFO_INT, String.valueOf(message), null);
142   }
143 
144   /**
145    * Converts the first input parameter to String and then delegates to 
146    * the wrapped <code>org.slf4j.Logger</code> instance.
147    * 
148    * @param message the message to log. Converted to {@link String}  
149    * @param t the exception to log
150    */
151   public void info(Object message, Throwable t) {
152     logger.log(null, FQCN, LocationAwareLogger.INFO_INT, String.valueOf(message), t);
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 the message to log. Converted to {@link String}  
160    */
161   public void warn(Object message) {
162     logger.log(null, FQCN, LocationAwareLogger.WARN_INT, String.valueOf(message), null);
163   }
164 
165   /**
166    * Converts the first input parameter to String and then delegates to 
167    * the wrapped <code>org.slf4j.Logger</code> instance.
168    * 
169    * @param message the message to log. Converted to {@link String}  
170    * @param t the exception to log
171    */
172   public void warn(Object message, Throwable t) {
173     logger.log(null, FQCN, LocationAwareLogger.WARN_INT, String.valueOf(message), t);
174   }
175 
176   /**
177    * Converts the input parameter to String and then delegates to the wrapped 
178    * <code>org.slf4j.Logger</code> instance.
179    * 
180    * @param message the message to log. Converted to {@link String}  
181    */
182   public void error(Object message) {
183     logger.log(null, FQCN, LocationAwareLogger.ERROR_INT, String.valueOf(message), null);
184   }
185 
186   /**
187    * Converts the first input parameter to String and then delegates to 
188    * the wrapped <code>org.slf4j.Logger</code> instance.
189    * 
190    * @param message the message to log. Converted to {@link String}  
191    * @param t the exception to log
192    */
193   public void error(Object message, Throwable t) {
194     logger.log(null, FQCN, LocationAwareLogger.ERROR_INT, String.valueOf(message), t);
195   }
196 
197 
198  
199   /**
200    * Converts the input parameter to String and then delegates to 
201    * the error method of the wrapped <code>org.slf4j.Logger</code> instance.
202    * 
203    * @param message the message to log. Converted to {@link String}  
204    */
205   public void fatal(Object message) {
206     logger.log(null, FQCN, LocationAwareLogger.ERROR_INT, String.valueOf(message), null);
207   }
208 
209   /**
210    * Converts the first input parameter to String and then delegates to 
211    * the error method of the wrapped <code>org.slf4j.Logger</code> instance.
212    * 
213    * @param message the message to log. Converted to {@link String}  
214    * @param t the exception to log
215    */
216   public void fatal(Object message, Throwable t) {
217     logger.log(null, FQCN, LocationAwareLogger.ERROR_INT, String.valueOf(message), t);
218   }
219 
220 }