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