001: /*
002: * Copyright 2001-2004 The Apache Software Foundation.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016: package org.apache.log4j;
017:
018: import org.slf4j.LoggerFactory;
019: import org.slf4j.Marker;
020: import org.slf4j.MarkerFactory;
021: import org.slf4j.spi.LocationAwareLogger;
022:
023: /**
024: * <p>
025: * This class is a minimal implementation of the original
026: * <code>org.apache.log4j.Logger</code> class by delegation of all calls
027: * to a {@link org.slf4j.Logger.Logger} instance.
028: * </p>
029: *
030: * <p>
031: * Log4j's <code>trace</code>, <code>debug()</code>, <code>info()</code>,
032: * <code>warn()</code>, <code>error()</code> printing methods are directly
033: * mapped to their SLF4J equivalents. Log4j's <code>fatal()</code>
034: * printing method is mapped to SLF4J's <code>error()</code> method
035: * with a FATAL marker.
036: *
037: * @author Sébastien Pennec
038: * @author Ceki Gülcü
039: */
040:
041: public class Category {
042:
043: private String name;
044:
045: private org.slf4j.Logger slf4jLogger;
046: private org.slf4j.spi.LocationAwareLogger locationAwareLogger;
047:
048: private static Marker FATAL_MARKER = MarkerFactory
049: .getMarker("FATAL");
050:
051: Category(String name) {
052: this .name = name;
053: slf4jLogger = LoggerFactory.getLogger(name);
054: if (slf4jLogger instanceof LocationAwareLogger) {
055: locationAwareLogger = (LocationAwareLogger) slf4jLogger;
056: }
057: }
058:
059: public static Logger getLogger(String name) {
060: return Log4jLoggerFactory.getLogger(name);
061: }
062:
063: public static Logger getLogger(Class clazz) {
064: return getLogger(clazz.getName());
065: }
066:
067: /**
068: * Does the obvious.
069: *
070: * @return
071: */
072: public static Logger getRootLogger() {
073: return getLogger(org.slf4j.Logger.ROOT_LOGGER_NAME);
074: }
075:
076: /**
077: * Returns the obvious.
078: *
079: * @return
080: */
081: public String getName() {
082: return name;
083: }
084:
085: /**
086: * Delegates to {@link org.slf4j.Logger#isTraceEnabled}
087: * method of SLF4J.
088: */
089: public boolean isTraceEnabled() {
090: return slf4jLogger.isTraceEnabled();
091: }
092:
093: /**
094: * Delegates to {@link org.slf4j.Logger#isDebugEnabled} method in SLF4J
095: */
096: public boolean isDebugEnabled() {
097: return slf4jLogger.isDebugEnabled();
098: }
099:
100: /**
101: * Delegates to {@link org.slf4j.Logger#isInfoEnabled} method in SLF4J
102: */
103: public boolean isInfoEnabled() {
104: return slf4jLogger.isInfoEnabled();
105: }
106:
107: /**
108: * Delegates to {@link org.slf4j.Logger#isWarnEnabled} method in SLF4J
109: */
110: public boolean isWarnEnabled() {
111: return slf4jLogger.isWarnEnabled();
112: }
113:
114: /**
115: * Delegates to {@link org.slf4j.Logger#isErrorEnabled} method in SLF4J
116: */
117: public boolean isErrorEnabled() {
118: return slf4jLogger.isErrorEnabled();
119: }
120:
121: /**
122: * Delegates to {@link #isEnabledFor(Level)}.
123: *
124: * @param p the priority to check against
125: * @return true if this logger is enabled for the given priority, false otehrwise.
126: */
127: public boolean isEnabledFor(Priority p) {
128: return isEnabledFor(Level.toLevel(p.level));
129: }
130:
131: /**
132: * Determines whether the level passes as parameter is enabled in
133: * the underlying SLF4J logger. Each log4j level is mapped directly to
134: * its SLF4J equivalent, except for FATAL which is mapped as ERROR.
135: *
136: * @param l the level to check against
137: * @return true if this logger is enabled for the given level, false otehrwise.
138: */
139: public boolean isEnabledFor(Level l) {
140: switch (l.level) {
141: case Level.TRACE_INT:
142: return slf4jLogger.isTraceEnabled();
143: case Level.DEBUG_INT:
144: return slf4jLogger.isDebugEnabled();
145: case Level.INFO_INT:
146: return slf4jLogger.isInfoEnabled();
147: case Level.WARN_INT:
148: return slf4jLogger.isWarnEnabled();
149: case Level.ERROR_INT:
150: return slf4jLogger.isErrorEnabled();
151: case Priority.FATAL_INT:
152: return slf4jLogger.isErrorEnabled();
153: }
154: return false;
155: }
156:
157: /**
158: * Delegates to {@link org.slf4j.Logger#trace(String)} method in SLF4J.
159: */
160: public void trace(Object message) {
161: // casting to String as SLF4J only accepts String instances, not Object
162: // instances.
163: slf4jLogger.trace(convertToString(message));
164: }
165:
166: /**
167: * Delegates to {@link org.slf4j.Logger#trace(String,Throwable)}
168: * method in SLF4J.
169: */
170: public void trace(Object message, Throwable t) {
171: slf4jLogger.trace(convertToString(message), t);
172: }
173:
174: /**
175: * Delegates to {@link org.slf4j.Logger#debug(String)} method of
176: * SLF4J.
177: */
178: public void debug(Object message) {
179: // casting to String as SLF4J only accepts String instances, not Object
180: // instances.
181: slf4jLogger.debug(convertToString(message));
182: }
183:
184: /**
185: * Delegates to {@link org.slf4j.Logger#debug(String,Throwable)}
186: * method in SLF4J.
187: */
188: public void debug(Object message, Throwable t) {
189: slf4jLogger.debug(convertToString(message), t);
190: }
191:
192: /**
193: * Delegates to {@link org.slf4j.Logger#info(String)}
194: * method in SLF4J.
195: */
196: public void info(Object message) {
197: slf4jLogger.info(convertToString(message));
198: }
199:
200: /**
201: * Delegates to {@link org.slf4j.Logger#info(String,Throwable)}
202: * method in SLF4J.
203: */
204: public void info(Object message, Throwable t) {
205: slf4jLogger.info(convertToString(message), t);
206: }
207:
208: /**
209: * Delegates to {@link org.slf4j.Logger#warn(String)}
210: * method in SLF4J.
211: */
212: public void warn(Object message) {
213: slf4jLogger.warn(convertToString(message));
214: }
215:
216: /**
217: * Delegates to {@link org.slf4j.Logger#warn(String,Throwable)}
218: * method in SLF4J.
219: */
220: public void warn(Object message, Throwable t) {
221: slf4jLogger.warn(convertToString(message), t);
222: }
223:
224: /**
225: * Delegates to {@link org.slf4j.Logger#error(String)}
226: * method in SLF4J.
227: */
228: public void error(Object message) {
229: slf4jLogger.error(convertToString(message));
230: }
231:
232: /**
233: * Delegates to {@link org.slf4j.Logger#error(String,Throwable)}
234: * method in SLF4J.
235: */
236: public void error(Object message, Throwable t) {
237: slf4jLogger.error(convertToString(message), t);
238: }
239:
240: /**
241: * Delegates to {@link org.slf4j.Logger#error(String)}
242: * method in SLF4J.
243: */
244: public void fatal(Object message) {
245: slf4jLogger.error(FATAL_MARKER, convertToString(message));
246: }
247:
248: /**
249: * Delegates to {@link org.slf4j.Logger#error(String,Throwable)}
250: * method in SLF4J. In addition, the call is marked with a marker named "FATAL".
251: */
252: public void fatal(Object message, Throwable t) {
253: slf4jLogger.error(FATAL_MARKER, convertToString(message), t);
254: }
255:
256: public void log(String FQCN, Priority p, Object msg, Throwable t) {
257: int levelInt = priorityToLevelInt(p);
258: if (locationAwareLogger != null) {
259: if (msg != null) {
260: locationAwareLogger.log(null, FQCN, levelInt, msg
261: .toString(), t);
262: } else {
263: locationAwareLogger.log(null, FQCN, levelInt, null, t);
264: }
265: } else {
266: throw new UnsupportedOperationException("The logger ["
267: + slf4jLogger
268: + "] does not seem to be location aware.");
269: }
270:
271: }
272:
273: private int priorityToLevelInt(Priority p) {
274: switch (p.level) {
275: case Level.TRACE_INT:
276: return LocationAwareLogger.TRACE_INT;
277: case Priority.DEBUG_INT:
278: return LocationAwareLogger.DEBUG_INT;
279: case Priority.INFO_INT:
280: return LocationAwareLogger.INFO_INT;
281: case Priority.WARN_INT:
282: return LocationAwareLogger.WARN_INT;
283: case Priority.ERROR_INT:
284: return LocationAwareLogger.ERROR_INT;
285: case Priority.FATAL_INT:
286: return LocationAwareLogger.ERROR_INT;
287: default:
288: throw new IllegalStateException("Unknown Priority " + p);
289: }
290: }
291:
292: private final String convertToString(Object message) {
293: if (message == null) {
294: return (String) message;
295: } else {
296: return message.toString();
297: }
298: }
299:
300: }
|