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