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  package org.apache.log4j;
17  
18  import org.slf4j.LoggerFactory;
19  import org.slf4j.Marker;
20  import org.slf4j.MarkerFactory;
21  import org.slf4j.spi.LocationAwareLogger;
22  
23  /**
24   * <p>
25   * This class is a minimal implementation of the origianl
26   * org.apache.log4j.Logger class delegating all calls to a
27   * {@link org.slf4j.Logger.Logger} instance.
28   * </p>
29   * 
30   * <p>
31   * Log4j's <code>debug()</code>, <code>info()</code>, <code>warn()</code>,
32   * <code>error()</code> printing methods are directly mapped to their logback
33   * equivalents. Log4j's <code>trace()</code> printing method is mapped to
34   * logback's <code>debug()</code> method with a TRACE marker. Log4j's
35   * <code>fatal()</code> printing method is mapped to logback's
36   * <code>error()</code> method with a FATAL marker.
37   * 
38   * @author S&eacute;bastien Pennec
39   * @author Ceki G&uuml;lc&uuml;
40   */
41  
42  public class Category {
43  
44    private String name;
45  
46    private org.slf4j.Logger lbLogger;
47    private org.slf4j.spi.LocationAwareLogger locationAwareLogger;
48    
49    private static Marker TRACE_MARKER = MarkerFactory.getMarker("TRACE");
50    private static Marker FATAL_MARKER = MarkerFactory.getMarker("FATAL");
51  
52    Category(String name) {
53      this.name = name;
54      lbLogger = LoggerFactory.getLogger(name);
55      if(lbLogger instanceof LocationAwareLogger) {
56        locationAwareLogger = (LocationAwareLogger) lbLogger;
57      }
58    }
59  
60    public static Logger getLogger(String name) {
61      return Log4jLoggerFactory.getLogger(name);
62    }
63  
64    public static Logger getLogger(Class clazz) {
65      return getLogger(clazz.getName());
66    }
67  
68    /**
69     * Does the obvious.
70     * 
71     * @return
72     */
73    public static Logger getRootLogger() {
74      return getLogger("root");
75    }
76  
77    /**
78     * Returns the obvious.
79     * 
80     * @return
81     */
82    public String getName() {
83      return name;
84    }
85  
86    /**
87     * Delegates to {@link ch.qos.logback.classic.Logger#isTraceEnabled} 
88     * method of logback.
89     */
90    public boolean isTraceEnabled() {
91      return lbLogger.isTraceEnabled();
92    }
93  
94    /**
95     * Delegates to {@link ch.qos.logback.classic.Logger#isDebugEnabled} method of logback
96     */
97    public boolean isDebugEnabled() {
98      return lbLogger.isDebugEnabled();
99    }
100 
101   /**
102    * Delegates to {@link ch.qos.logback.classic.Logger#isInfoEnabled} method of logback
103    */
104   public boolean isInfoEnabled() {
105     return lbLogger.isInfoEnabled();
106   }
107 
108   /**
109    * Delegates to {@link ch.qos.logback.classic.Logger#isWarnEnabled} method of logback
110    */
111   public boolean isWarnEnabled() {
112     return lbLogger.isWarnEnabled();
113   }
114   
115   public boolean isEnabledFor(Priority p) {
116     return isEnabledFor(Level.toLevel(p.level));
117   }
118 
119   public boolean isEnabledFor(Level l) {
120     switch (l.level) {
121     case Level.TRACE_INT:
122       return lbLogger.isTraceEnabled();
123     case Level.DEBUG_INT:
124       return lbLogger.isDebugEnabled();
125     case Level.INFO_INT:
126       return lbLogger.isInfoEnabled();
127     case Level.WARN_INT:
128       return lbLogger.isWarnEnabled();
129     case Level.ERROR_INT:
130       return lbLogger.isErrorEnabled();
131     case Priority.FATAL_INT:
132       return lbLogger.isErrorEnabled();
133     }
134     return false;
135   }
136 
137   /**
138    * Delegates to {@link ch.qos.logback.classic.Logger#isErrorEnabled} method of logback
139    */
140   public boolean isErrorEnabled() {
141     return lbLogger.isErrorEnabled();
142   }
143 
144   /**
145    * Delegates to {@link ch.qos.logback.classic.Logger#debug(String)} method of logback, 
146    * in addition, the call is marked with a marker named "TRACE".
147    */
148   public void trace(Object message) {
149     lbLogger.debug(TRACE_MARKER, convertToString(message));
150   }
151 
152   /**
153    * Delegates to {@link ch.qos.logback.classic.Logger#debug(String,Throwable)} 
154    * method of logback in addition, the call is marked with a marker named "TRACE".
155    */
156   public void trace(Object message, Throwable t) {
157     lbLogger.debug(TRACE_MARKER, convertToString(message), t);
158   }
159   
160   /**
161    * Delegates to {@link ch.qos.logback.classic.Logger#debug(String,Object)} 
162    * method of logback in addition, the call is marked with a marker named "TRACE".
163    */
164   public void trace(Object message, Object o) {
165     lbLogger.debug(TRACE_MARKER, convertToString(message), o);
166   }
167 
168   /**
169    * Delegates to {@link ch.qos.logback.classic.Logger#debug(String,Object,Object)} 
170    * method of logback in addition, the call is marked with a marker named "TRACE".
171    */
172   public void trace(String message, Object arg1, Object arg2) {
173     lbLogger.debug(TRACE_MARKER, message, arg1, arg2);
174   }
175   
176   /**
177    * Delegates to {@link ch.qos.logback.classic.Logger#debug(String)} method of
178    * logback.
179    */
180   public void debug(Object message) {
181     // casting to String as SLF4J only accepts String instances, not Object
182     // instances.
183     lbLogger.debug(convertToString(message));
184   }
185 
186   /**
187    * Delegates to {@link ch.qos.logback.classic.Logger#debug(String,Throwable)} 
188    * method of logback.
189    */
190   public void debug(Object message, Throwable t) {
191     lbLogger.debug(convertToString(message), t);
192   }
193 
194   /**
195    * Delegates to {@link ch.qos.logback.classic.Logger#debug(String,Object)} 
196    * method of logback.
197    */
198   public void debug(Object message, Object o) {
199     lbLogger.debug(convertToString(message), o);
200   }
201   
202   /**
203    * Delegates to {@link ch.qos.logback.classic.Logger#debug(String,Object,Object)} 
204    * method of logback.
205    */
206   public void debug(String message, Object arg1, Object arg2) {
207     lbLogger.debug(message, arg1, arg2);
208   }
209   
210   /**
211    * Delegates to {@link ch.qos.logback.classic.Logger#info(String)} 
212    * method of logback.
213    */
214   public void info(Object message) {
215     lbLogger.info(convertToString(message));
216   }
217 
218   /**
219    * Delegates to {@link ch.qos.logback.classic.Logger#info(String,Throwable)} 
220    * method of logback.
221    */
222   public void info(Object message, Throwable t) {
223     lbLogger.info(convertToString(message), t);
224   }
225 
226   /**
227    * Delegates to {@link ch.qos.logback.classic.Logger#info(String,Object)} 
228    * method of logback.
229    */
230   public void info(Object message, Object o) {
231     lbLogger.info(convertToString(message), o);
232   }  
233   
234   /**
235    * Delegates to {@link ch.qos.logback.classic.Logger#info(String,Object,Object)} 
236    * method of logback.
237    */
238   public void info(String message, Object arg1, Object arg2) {
239     lbLogger.info(message, arg1, arg2);
240   }
241   
242   /**
243    * Delegates to {@link ch.qos.logback.classic.Logger#warn(String)} 
244    * method of logback.
245    */
246   public void warn(Object message) {
247     lbLogger.warn(convertToString(message));
248   }
249 
250   /**
251    * Delegates to {@link ch.qos.logback.classic.Logger#warn(String,Throwable)} 
252    * method of logback.
253    */
254   public void warn(Object message, Throwable t) {
255     lbLogger.warn(convertToString(message), t);
256   }
257 
258   /**
259    * Delegates to {@link ch.qos.logback.classic.Logger#warn(String,Object)} 
260    * method of logback.
261    */
262   public void warn(Object message, Object o) {
263     lbLogger.warn(convertToString(message), o);
264   }
265   
266   /**
267    * Delegates to {@link ch.qos.logback.classic.Logger#warn(String,Object,Object)} 
268    * method of logback.
269    */
270   public void warn(String message, Object arg1, Object arg2) {
271     lbLogger.warn(message, arg1, arg2);
272   }
273   
274   /**
275    * Delegates to {@link ch.qos.logback.classic.Logger#error(String)} 
276    * method of logback.
277    */
278   public void error(Object message) {
279     lbLogger.error(convertToString(message));
280   }
281 
282   /**
283    * Delegates to {@link ch.qos.logback.classic.Logger#error(String,Throwable)} 
284    * method of logback.
285    */
286   public void error(Object message, Throwable t) {
287     lbLogger.error(convertToString(message), t);
288   }
289 
290   /**
291    * Delegates to {@link ch.qos.logback.classic.Logger#error(String,Object)} 
292    * method of logback.
293    */
294   public void error(Object message, Object o) {
295     lbLogger.error(convertToString(message), o);
296   }
297   
298   /**
299    * Delegates to {@link ch.qos.logback.classic.Logger#error(String,Object,Object)} 
300    * method of logback.
301    */
302   public void error(String message, Object arg1, Object arg2) {
303     lbLogger.error(message, arg1, arg2);
304   }
305   
306   /**
307    * Delegates to {@link ch.qos.logback.classic.Logger#error(String)} 
308    * method of logback.
309    */
310   public void fatal(Object message) {
311     lbLogger.error(FATAL_MARKER, convertToString(message));
312   }
313 
314   /**
315    * Delegates to {@link ch.qos.logback.classic.Logger#error(String,Throwable)} 
316    * method of logback in addition, the call is marked with a marker named "FATAL".
317    */
318   public void fatal(Object message, Throwable t) {
319     lbLogger.error(FATAL_MARKER, convertToString(message), t);
320   }
321 
322   /**
323    * Delegates to {@link ch.qos.logback.classic.Logger#error(String,Object)} 
324    * method of logback in addition, the call is marked with a marker named "FATAL".
325    */
326   public void fatal(Object message, Object o) {
327     lbLogger.error(FATAL_MARKER, convertToString(message), o);
328   } 
329   
330   /**
331    * Delegates to {@link ch.qos.logback.classic.Logger#error(String,Object,Object)} 
332    * method of logback in addition, the call is marked with a marker named "FATAL".
333    */
334   public void fatal(String message, Object arg1, Object arg2) {
335     lbLogger.error(FATAL_MARKER, message, arg1, arg2);
336   } 
337   
338   public void log(String FQCN, Priority p, Object msg, Throwable t) {
339     int levelInt = priorityToLevelInt(p);
340     if(locationAwareLogger != null) {
341       if(msg != null) {
342         locationAwareLogger.log(null, FQCN, levelInt, msg.toString(), t); 
343       } else {
344         locationAwareLogger.log(null, FQCN, levelInt, null, t); 
345       }
346     } else {
347       throw new UnsupportedOperationException("The logger ["+lbLogger+"] does not seem to be location aware.");
348     }
349    
350   }
351   
352   private int priorityToLevelInt(Priority p) {
353     switch (p.level) {
354     case Level.TRACE_INT:
355       return LocationAwareLogger.TRACE_INT;
356     case Priority.DEBUG_INT:
357       return LocationAwareLogger.DEBUG_INT;
358     case Priority.INFO_INT:
359       return LocationAwareLogger.INFO_INT;
360     case Priority.WARN_INT:
361       return LocationAwareLogger.WARN_INT;
362     case Priority.ERROR_INT:
363       return LocationAwareLogger.ERROR_INT;
364     case Priority.FATAL_INT:
365       return LocationAwareLogger.ERROR_INT;
366     default:
367       throw new IllegalStateException("Unknown Priority " + p);
368     }
369   }
370   
371   private final String convertToString(Object message) {
372     if (message == null) {
373       return (String)message;
374     } else {
375       return message.toString();
376     }
377   }
378 
379 }