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:
017: package org.apache.commons.logging.impl;
018:
019: import java.io.Serializable;
020: import org.apache.commons.logging.Log;
021: import org.apache.log4j.Logger;
022: import org.apache.log4j.Priority;
023: import org.apache.log4j.Level;
024:
025: /**
026: * Implementation of {@link Log} that maps directly to a
027: * <strong>Logger</strong> for log4J version 1.2.
028: * <p>
029: * Initial configuration of the corresponding Logger instances should be done
030: * in the usual manner, as outlined in the Log4J documentation.
031: * <p>
032: * The reason this logger is distinct from the 1.3 logger is that in version 1.2
033: * of Log4J:
034: * <ul>
035: * <li>class Logger takes Priority parameters not Level parameters.
036: * <li>class Level extends Priority
037: * </ul>
038: * Log4J1.3 is expected to change Level so it no longer extends Priority, which is
039: * a non-binary-compatible change. The class generated by compiling this code against
040: * log4j 1.2 will therefore not run against log4j 1.3.
041: *
042: * @author <a href="mailto:sanders@apache.org">Scott Sanders</a>
043: * @author Rod Waldhoff
044: * @author Robert Burrell Donkin
045: * @version $Id: Log4JLogger.java 370672 2006-01-19 23:52:23Z skitching $
046: */
047:
048: public class Log4JLogger implements Log, Serializable {
049:
050: // ------------------------------------------------------------- Attributes
051:
052: /** The fully qualified name of the Log4JLogger class. */
053: private static final String FQCN = Log4JLogger.class.getName();
054:
055: /** Log to this logger */
056: private transient Logger logger = null;
057:
058: /** Logger name */
059: private String name = null;
060:
061: private static Priority traceLevel;
062:
063: // ------------------------------------------------------------
064: // Static Initializer.
065: //
066: // Note that this must come after the static variable declarations
067: // otherwise initialiser expressions associated with those variables
068: // will override any settings done here.
069: //
070: // Verify that log4j is available, and that it is version 1.2.
071: // If an ExceptionInInitializerError is generated, then LogFactoryImpl
072: // will treat that as meaning that the appropriate underlying logging
073: // library is just not present - if discovery is in progress then
074: // discovery will continue.
075: // ------------------------------------------------------------
076:
077: static {
078: if (!Priority.class.isAssignableFrom(Level.class)) {
079: // nope, this is log4j 1.3, so force an ExceptionInInitializerError
080: throw new InstantiationError("Log4J 1.2 not available");
081: }
082:
083: // Releases of log4j1.2 >= 1.2.12 have Priority.TRACE available, earlier
084: // versions do not. If TRACE is not available, then we have to map
085: // calls to Log.trace(...) onto the DEBUG level.
086:
087: try {
088: traceLevel = (Priority) Level.class.getDeclaredField(
089: "TRACE").get(null);
090: } catch (Exception ex) {
091: // ok, trace not available
092: traceLevel = Priority.DEBUG;
093: }
094: }
095:
096: // ------------------------------------------------------------ Constructor
097:
098: public Log4JLogger() {
099: }
100:
101: /**
102: * Base constructor.
103: */
104: public Log4JLogger(String name) {
105: this .name = name;
106: this .logger = getLogger();
107: }
108:
109: /** For use with a log4j factory.
110: */
111: public Log4JLogger(Logger logger) {
112: this .name = logger.getName();
113: this .logger = logger;
114: }
115:
116: // ---------------------------------------------------------
117: // Implementation
118: //
119: // Note that in the methods below the Priority class is used to define
120: // levels even though the Level class is supported in 1.2. This is done
121: // so that at compile time the call definitely resolves to a call to
122: // a method that takes a Priority rather than one that takes a Level.
123: //
124: // The Category class (and hence its subclass Logger) in version 1.2 only
125: // has methods that take Priority objects. The Category class (and hence
126: // Logger class) in version 1.3 has methods that take both Priority and
127: // Level objects. This means that if we use Level here, and compile
128: // against log4j 1.3 then calls would be bound to the versions of
129: // methods taking Level objects and then would fail to run against
130: // version 1.2 of log4j.
131: // ---------------------------------------------------------
132:
133: /**
134: * Logs a message with <code>org.apache.log4j.Priority.TRACE</code>.
135: * When using a log4j version that does not support the <code>TRACE</code>
136: * level, the message will be logged at the <code>DEBUG</code> level.
137: *
138: * @param message to log
139: * @see org.apache.commons.logging.Log#trace(Object)
140: */
141: public void trace(Object message) {
142: getLogger().log(FQCN, traceLevel, message, null);
143: }
144:
145: /**
146: * Logs a message with <code>org.apache.log4j.Priority.TRACE</code>.
147: * When using a log4j version that does not support the <code>TRACE</code>
148: * level, the message will be logged at the <code>DEBUG</code> level.
149: *
150: * @param message to log
151: * @param t log this cause
152: * @see org.apache.commons.logging.Log#trace(Object, Throwable)
153: */
154: public void trace(Object message, Throwable t) {
155: getLogger().log(FQCN, traceLevel, message, t);
156: }
157:
158: /**
159: * Logs a message with <code>org.apache.log4j.Priority.DEBUG</code>.
160: *
161: * @param message to log
162: * @see org.apache.commons.logging.Log#debug(Object)
163: */
164: public void debug(Object message) {
165: getLogger().log(FQCN, Priority.DEBUG, message, null);
166: }
167:
168: /**
169: * Logs a message with <code>org.apache.log4j.Priority.DEBUG</code>.
170: *
171: * @param message to log
172: * @param t log this cause
173: * @see org.apache.commons.logging.Log#debug(Object, Throwable)
174: */
175: public void debug(Object message, Throwable t) {
176: getLogger().log(FQCN, Priority.DEBUG, message, t);
177: }
178:
179: /**
180: * Logs a message with <code>org.apache.log4j.Priority.INFO</code>.
181: *
182: * @param message to log
183: * @see org.apache.commons.logging.Log#info(Object)
184: */
185: public void info(Object message) {
186: getLogger().log(FQCN, Priority.INFO, message, null);
187: }
188:
189: /**
190: * Logs a message with <code>org.apache.log4j.Priority.INFO</code>.
191: *
192: * @param message to log
193: * @param t log this cause
194: * @see org.apache.commons.logging.Log#info(Object, Throwable)
195: */
196: public void info(Object message, Throwable t) {
197: getLogger().log(FQCN, Priority.INFO, message, t);
198: }
199:
200: /**
201: * Logs a message with <code>org.apache.log4j.Priority.WARN</code>.
202: *
203: * @param message to log
204: * @see org.apache.commons.logging.Log#warn(Object)
205: */
206: public void warn(Object message) {
207: getLogger().log(FQCN, Priority.WARN, message, null);
208: }
209:
210: /**
211: * Logs a message with <code>org.apache.log4j.Priority.WARN</code>.
212: *
213: * @param message to log
214: * @param t log this cause
215: * @see org.apache.commons.logging.Log#warn(Object, Throwable)
216: */
217: public void warn(Object message, Throwable t) {
218: getLogger().log(FQCN, Priority.WARN, message, t);
219: }
220:
221: /**
222: * Logs a message with <code>org.apache.log4j.Priority.ERROR</code>.
223: *
224: * @param message to log
225: * @see org.apache.commons.logging.Log#error(Object)
226: */
227: public void error(Object message) {
228: getLogger().log(FQCN, Priority.ERROR, message, null);
229: }
230:
231: /**
232: * Logs a message with <code>org.apache.log4j.Priority.ERROR</code>.
233: *
234: * @param message to log
235: * @param t log this cause
236: * @see org.apache.commons.logging.Log#error(Object, Throwable)
237: */
238: public void error(Object message, Throwable t) {
239: getLogger().log(FQCN, Priority.ERROR, message, t);
240: }
241:
242: /**
243: * Logs a message with <code>org.apache.log4j.Priority.FATAL</code>.
244: *
245: * @param message to log
246: * @see org.apache.commons.logging.Log#fatal(Object)
247: */
248: public void fatal(Object message) {
249: getLogger().log(FQCN, Priority.FATAL, message, null);
250: }
251:
252: /**
253: * Logs a message with <code>org.apache.log4j.Priority.FATAL</code>.
254: *
255: * @param message to log
256: * @param t log this cause
257: * @see org.apache.commons.logging.Log#fatal(Object, Throwable)
258: */
259: public void fatal(Object message, Throwable t) {
260: getLogger().log(FQCN, Priority.FATAL, message, t);
261: }
262:
263: /**
264: * Return the native Logger instance we are using.
265: */
266: public Logger getLogger() {
267: if (logger == null) {
268: logger = Logger.getLogger(name);
269: }
270: return (this .logger);
271: }
272:
273: /**
274: * Check whether the Log4j Logger used is enabled for <code>DEBUG</code> priority.
275: */
276: public boolean isDebugEnabled() {
277: return getLogger().isDebugEnabled();
278: }
279:
280: /**
281: * Check whether the Log4j Logger used is enabled for <code>ERROR</code> priority.
282: */
283: public boolean isErrorEnabled() {
284: return getLogger().isEnabledFor(Priority.ERROR);
285: }
286:
287: /**
288: * Check whether the Log4j Logger used is enabled for <code>FATAL</code> priority.
289: */
290: public boolean isFatalEnabled() {
291: return getLogger().isEnabledFor(Priority.FATAL);
292: }
293:
294: /**
295: * Check whether the Log4j Logger used is enabled for <code>INFO</code> priority.
296: */
297: public boolean isInfoEnabled() {
298: return getLogger().isInfoEnabled();
299: }
300:
301: /**
302: * Check whether the Log4j Logger used is enabled for <code>TRACE</code> priority.
303: * When using a log4j version that does not support the TRACE level, this call
304: * will report whether <code>DEBUG</code> is enabled or not.
305: */
306: public boolean isTraceEnabled() {
307: return getLogger().isEnabledFor(traceLevel);
308: }
309:
310: /**
311: * Check whether the Log4j Logger used is enabled for <code>WARN</code> priority.
312: */
313: public boolean isWarnEnabled() {
314: return getLogger().isEnabledFor(Priority.WARN);
315: }
316: }
|