View Javadoc

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
35   * marker.
36   * 
37   * @author S&eacute;bastien Pennec
38   * @author Ceki G&uuml;lc&uuml;
39   */
40  public class Category {
41  
42    private static final String CATEGORY_FQCN = Category.class.getName();
43  
44    private String name;
45  
46    protected org.slf4j.Logger slf4jLogger;
47    private org.slf4j.spi.LocationAwareLogger locationAwareLogger;
48  
49    private static Marker FATAL_MARKER = MarkerFactory.getMarker("FATAL");
50  
51    Category(String name) {
52      this.name = name;
53      slf4jLogger = LoggerFactory.getLogger(name);
54      if (slf4jLogger instanceof LocationAwareLogger) {
55        locationAwareLogger = (LocationAwareLogger) slf4jLogger;
56      }
57    }
58  
59    public static Category getInstance(Class clazz) {
60      return Log4jLoggerFactory.getLogger(clazz.getName());
61    }
62  
63    public static Category getInstance(String name) {
64      return Log4jLoggerFactory.getLogger(name);
65    }
66  
67    /**
68     * Returns the obvious.
69     * 
70     * @return
71     */
72    public String getName() {
73      return name;
74    }
75  
76    /**
77     * Return the level in effect for this category/logger.
78     * 
79     * <p>
80     * The result is computed by simulation.
81     * 
82     * @return
83     */
84    public Level getEffectiveLevel() {
85      if (slf4jLogger.isTraceEnabled()) {
86        return Level.TRACE;
87      }
88      if (slf4jLogger.isDebugEnabled()) {
89        return Level.DEBUG;
90      }
91      if (slf4jLogger.isInfoEnabled()) {
92        return Level.INFO;
93      }
94      if (slf4jLogger.isWarnEnabled()) {
95        return Level.WARN;
96      }
97      return Level.ERROR;
98    }
99  
100   /**
101    * Returns the assigned {@link Level}, if any, for this Category. This
102    * implementation always returns null.
103    * 
104    * @return Level - the assigned Level, can be <code>null</code>.
105    */
106   final public Level getLevel() {
107     return null;
108   }
109 
110   /**
111    * @deprecated Please use {@link #getLevel} instead.
112    */
113   final public Level getPriority() {
114     return null;
115   }
116 
117   /**
118    * Delegates to {@link org.slf4j.Logger#isDebugEnabled} method in SLF4J
119    */
120   public boolean isDebugEnabled() {
121     return slf4jLogger.isDebugEnabled();
122   }
123 
124   /**
125    * Delegates to {@link org.slf4j.Logger#isInfoEnabled} method in SLF4J
126    */
127   public boolean isInfoEnabled() {
128     return slf4jLogger.isInfoEnabled();
129   }
130 
131   /**
132    * Delegates tob {@link org.slf4j.Logger#isWarnEnabled} method in SLF4J
133    */
134   public boolean isWarnEnabled() {
135     return slf4jLogger.isWarnEnabled();
136   }
137 
138   /**
139    * Delegates to {@link org.slf4j.Logger#isErrorEnabled} method in SLF4J
140    */
141   public boolean isErrorEnabled() {
142     return slf4jLogger.isErrorEnabled();
143   }
144 
145   /**
146    * Determines whether the priority passed as parameter is enabled in the
147    * underlying SLF4J logger. Each log4j priority is mapped directly to its
148    * SLF4J equivalent, except for FATAL which is mapped as ERROR.
149    * 
150    * @param p
151    *                the priority to check against
152    * @return true if this logger is enabled for the given level, false
153    *         otherwise.
154    */
155   public boolean isEnabledFor(Priority p) {
156     switch (p.level) {
157     case Level.TRACE_INT:
158       return slf4jLogger.isTraceEnabled();
159     case Level.DEBUG_INT:
160       return slf4jLogger.isDebugEnabled();
161     case Level.INFO_INT:
162       return slf4jLogger.isInfoEnabled();
163     case Level.WARN_INT:
164       return slf4jLogger.isWarnEnabled();
165     case Level.ERROR_INT:
166       return slf4jLogger.isErrorEnabled();
167     case Priority.FATAL_INT:
168       return slf4jLogger.isErrorEnabled();
169     }
170     return false;
171   }
172 
173   void differentiatedLog(Marker marker, String fqcn, int level, Object message,
174       Throwable t) {
175     String m = convertToString(message);
176     if (locationAwareLogger != null) {
177       locationAwareLogger.log(marker, fqcn, level, m, 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   
285   public void log(String FQCN, Priority p, Object msg, Throwable t) {
286     int levelInt = priorityToLevelInt(p);
287     if (locationAwareLogger != null) {
288       locationAwareLogger.log(null, FQCN, levelInt, convertToString(msg), t);
289     } else {
290       throw new UnsupportedOperationException("The logger [" + slf4jLogger
291           + "] does not seem to be location aware.");
292     }
293   }
294 
295   public void log(Priority p, Object message, Throwable t) {
296     int levelInt = priorityToLevelInt(p);
297     differentiatedLog(null, CATEGORY_FQCN, levelInt,
298         message, t);
299   }
300 
301   public void log(Priority p, Object message) {
302     int levelInt = priorityToLevelInt(p);
303     differentiatedLog(null, CATEGORY_FQCN, levelInt,
304         message, null);
305   }
306 
307 
308   private int priorityToLevelInt(Priority p) {
309     switch (p.level) {
310     case Level.TRACE_INT:
311       return LocationAwareLogger.TRACE_INT;
312     case Priority.DEBUG_INT:
313       return LocationAwareLogger.DEBUG_INT;
314     case Priority.INFO_INT:
315       return LocationAwareLogger.INFO_INT;
316     case Priority.WARN_INT:
317       return LocationAwareLogger.WARN_INT;
318     case Priority.ERROR_INT:
319       return LocationAwareLogger.ERROR_INT;
320     case Priority.FATAL_INT:
321       return LocationAwareLogger.ERROR_INT;
322     default:
323       throw new IllegalStateException("Unknown Priority " + p);
324     }
325   }
326 
327   protected final String convertToString(Object message) {
328     if (message == null) {
329       return (String) message;
330     } else {
331       return message.toString();
332     }
333   }
334 
335 }