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