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.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     String m = convertToString(message);
175     if (locationAwareLogger != null) {
176       locationAwareLogger.log(marker, fqcn, level, m, t);
177     } else {
178       switch (level) {
179       case LocationAwareLogger.TRACE_INT:
180         slf4jLogger.trace(marker, m);
181         break;
182       case LocationAwareLogger.DEBUG_INT:
183         slf4jLogger.debug(marker, m);
184         break;
185       case LocationAwareLogger.INFO_INT:
186         slf4jLogger.info(marker, m);
187         break;
188       case LocationAwareLogger.WARN_INT:
189         slf4jLogger.warn(marker, m);
190         break;
191       case LocationAwareLogger.ERROR_INT:
192         slf4jLogger.error(marker, m);
193         break;
194       }
195     }
196   }
197 
198   /**
199    * Delegates to {@link org.slf4j.Logger#debug(String)} method of SLF4J.
200    */
201   public void debug(Object message) {
202     differentiatedLog(null, CATEGORY_FQCN, LocationAwareLogger.DEBUG_INT, message, null);
203   }
204 
205   /**
206    * Delegates to {@link org.slf4j.Logger#debug(String,Throwable)} method in
207    * SLF4J.
208    */
209   public void debug(Object message, Throwable t) {
210     differentiatedLog(null, CATEGORY_FQCN, LocationAwareLogger.DEBUG_INT, message, t);
211   }
212 
213   /**
214    * Delegates to {@link org.slf4j.Logger#info(String)} method in SLF4J.
215    */
216   public void info(Object message) {
217     differentiatedLog(null, CATEGORY_FQCN, LocationAwareLogger.INFO_INT, message, null);
218   }
219 
220   /**
221    * Delegates to {@link org.slf4j.Logger#info(String,Throwable)} method in
222    * SLF4J.
223    */
224   public void info(Object message, Throwable t) {
225     differentiatedLog(null, CATEGORY_FQCN, LocationAwareLogger.INFO_INT, message, t);
226   }
227 
228   /**
229    * Delegates to {@link org.slf4j.Logger#warn(String)} method in SLF4J.
230    */
231   public void warn(Object message) {
232     differentiatedLog(null, CATEGORY_FQCN, LocationAwareLogger.WARN_INT, message, null);
233   }
234 
235   /**
236    * Delegates to {@link org.slf4j.Logger#warn(String,Throwable)} method in
237    * SLF4J.
238    */
239   public void warn(Object message, Throwable t) {
240     differentiatedLog(null, CATEGORY_FQCN, LocationAwareLogger.WARN_INT, message, t);
241   }
242 
243   /**
244    * Delegates to {@link org.slf4j.Logger#error(String)} method in SLF4J.
245    */
246   public void error(Object message) {
247     differentiatedLog(null, CATEGORY_FQCN, LocationAwareLogger.ERROR_INT, message, null);
248   }
249 
250   /**
251    * Delegates to {@link org.slf4j.Logger#error(String,Throwable)} method in
252    * SLF4J.
253    */
254   public void error(Object message, Throwable t) {
255     differentiatedLog(null, CATEGORY_FQCN, LocationAwareLogger.ERROR_INT, message, t);
256   }
257 
258   /**
259    * Delegates to {@link org.slf4j.Logger#error(String)} method in SLF4J.
260    */
261   public void fatal(Object message) {
262     differentiatedLog(FATAL_MARKER, CATEGORY_FQCN, LocationAwareLogger.ERROR_INT, message, null);
263   }
264 
265   /**
266    * Delegates to {@link org.slf4j.Logger#error(String,Throwable)} method in
267    * SLF4J. In addition, the call is marked with a marker named "FATAL".
268    */
269   public void fatal(Object message, Throwable t) {
270     differentiatedLog(FATAL_MARKER, CATEGORY_FQCN, LocationAwareLogger.ERROR_INT, message, t);
271   }
272 
273   public void log(String FQCN, Priority p, Object msg, Throwable t) {
274     int levelInt = priorityToLevelInt(p);
275     if (locationAwareLogger != null) {
276       locationAwareLogger.log(null, FQCN, levelInt, convertToString(msg), t);
277     } else {
278       throw new UnsupportedOperationException("The logger [" + slf4jLogger
279           + "] does not seem to be location aware.");
280     }
281   }
282 
283   private int priorityToLevelInt(Priority p) {
284     switch (p.level) {
285     case Level.TRACE_INT:
286       return LocationAwareLogger.TRACE_INT;
287     case Priority.DEBUG_INT:
288       return LocationAwareLogger.DEBUG_INT;
289     case Priority.INFO_INT:
290       return LocationAwareLogger.INFO_INT;
291     case Priority.WARN_INT:
292       return LocationAwareLogger.WARN_INT;
293     case Priority.ERROR_INT:
294       return LocationAwareLogger.ERROR_INT;
295     case Priority.FATAL_INT:
296       return LocationAwareLogger.ERROR_INT;
297     default:
298       throw new IllegalStateException("Unknown Priority " + p);
299     }
300   }
301 
302   protected final String convertToString(Object message) {
303     if (message == null) {
304       return (String) message;
305     } else {
306       return message.toString();
307     }
308   }
309 
310 }