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