001: /*
002: * Copyright 2001-2004,2006 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 org.apache.avalon.framework.logger.Logger;
020: import org.apache.commons.logging.Log;
021:
022: /**
023: * <p>Implementation of commons-logging Log interface that delegates all
024: * logging calls to the Avalon logging abstraction: the Logger interface.
025: * </p>
026: * <p>
027: * There are two ways in which this class can be used:
028: * </p>
029: * <ul>
030: * <li>the instance can be constructed with an Avalon logger
031: * (by calling {@link #AvalonLogger(Logger)}). In this case, it acts
032: * as a simple thin wrapping implementation over the logger. This is
033: * particularly useful when using a property setter.
034: * </li>
035: * <li>the {@link #setDefaultLogger} class property can be called which
036: * sets the ancesteral Avalon logger for this class. Any <code>AvalonLogger</code>
037: * instances created through the <code>LogFactory</code> mechanisms will output
038: * to child loggers of this <code>Logger</code>.
039: * </li>
040: * </ul>
041: * <p>
042: * <strong>Note:</strong> <code>AvalonLogger</code> does not implement Serializable
043: * because the constructors available for it make this impossible to achieve in all
044: * circumstances; there is no way to "reconnect" to an underlying Logger object on
045: * deserialization if one was just passed in to the constructor of the original
046: * object. This class <i>was</i> marked Serializable in the 1.0.4 release of
047: * commons-logging, but this never actually worked (a NullPointerException would
048: * be thrown as soon as the deserialized object was used), so removing this marker
049: * is not considered to be an incompatible change.
050: * </p>
051: * @author <a href="mailto:neeme@apache.org">Neeme Praks</a>
052: * @version $Revision: 399221 $ $Date: 2006-05-03 10:20:24 +0100 (Wed, 03 May 2006) $
053: */
054: public class AvalonLogger implements Log {
055:
056: /** Ancesteral avalon logger */
057: private static Logger defaultLogger = null;
058: /** Avalon logger used to perform log */
059: private transient Logger logger = null;
060:
061: /**
062: * Constructs an <code>AvalonLogger</code> that outputs to the given
063: * <code>Logger</code> instance.
064: * @param logger the avalon logger implementation to delegate to
065: */
066: public AvalonLogger(Logger logger) {
067: this .logger = logger;
068: }
069:
070: /**
071: * Constructs an <code>AvalonLogger</code> that will log to a child
072: * of the <code>Logger</code> set by calling {@link #setDefaultLogger}.
073: * @param name the name of the avalon logger implementation to delegate to
074: */
075: public AvalonLogger(String name) {
076: if (defaultLogger == null)
077: throw new NullPointerException(
078: "default logger has to be specified if this constructor is used!");
079: this .logger = defaultLogger.getChildLogger(name);
080: }
081:
082: /**
083: * Gets the Avalon logger implementation used to perform logging.
084: * @return avalon logger implementation
085: */
086: public Logger getLogger() {
087: return logger;
088: }
089:
090: /**
091: * Sets the ancesteral Avalon logger from which the delegating loggers
092: * will descend.
093: * @param logger the default avalon logger,
094: * in case there is no logger instance supplied in constructor
095: */
096: public static void setDefaultLogger(Logger logger) {
097: defaultLogger = logger;
098: }
099:
100: /**
101: * Logs a message with
102: * <code>org.apache.avalon.framework.logger.Logger.debug</code>.
103: *
104: * @param message to log
105: * @param t log this cause
106: * @see org.apache.commons.logging.Log#debug(Object, Throwable)
107: */
108: public void debug(Object message, Throwable t) {
109: if (getLogger().isDebugEnabled())
110: getLogger().debug(String.valueOf(message), t);
111: }
112:
113: /**
114: * Logs a message with
115: * <code>org.apache.avalon.framework.logger.Logger.debug</code>.
116: *
117: * @param message to log.
118: * @see org.apache.commons.logging.Log#debug(Object)
119: */
120: public void debug(Object message) {
121: if (getLogger().isDebugEnabled())
122: getLogger().debug(String.valueOf(message));
123: }
124:
125: /**
126: * Logs a message with
127: * <code>org.apache.avalon.framework.logger.Logger.error</code>.
128: *
129: * @param message to log
130: * @param t log this cause
131: * @see org.apache.commons.logging.Log#error(Object, Throwable)
132: */
133: public void error(Object message, Throwable t) {
134: if (getLogger().isErrorEnabled())
135: getLogger().error(String.valueOf(message), t);
136: }
137:
138: /**
139: * Logs a message with
140: * <code>org.apache.avalon.framework.logger.Logger.error</code>.
141: *
142: * @param message to log
143: * @see org.apache.commons.logging.Log#error(Object)
144: */
145: public void error(Object message) {
146: if (getLogger().isErrorEnabled())
147: getLogger().error(String.valueOf(message));
148: }
149:
150: /**
151: * Logs a message with
152: * <code>org.apache.avalon.framework.logger.Logger.fatalError</code>.
153: *
154: * @param message to log.
155: * @param t log this cause.
156: * @see org.apache.commons.logging.Log#fatal(Object, Throwable)
157: */
158: public void fatal(Object message, Throwable t) {
159: if (getLogger().isFatalErrorEnabled())
160: getLogger().fatalError(String.valueOf(message), t);
161: }
162:
163: /**
164: * Logs a message with
165: * <code>org.apache.avalon.framework.logger.Logger.fatalError</code>.
166: *
167: * @param message to log
168: * @see org.apache.commons.logging.Log#fatal(Object)
169: */
170: public void fatal(Object message) {
171: if (getLogger().isFatalErrorEnabled())
172: getLogger().fatalError(String.valueOf(message));
173: }
174:
175: /**
176: * Logs a message with
177: * <code>org.apache.avalon.framework.logger.Logger.info</code>.
178: *
179: * @param message to log
180: * @param t log this cause
181: * @see org.apache.commons.logging.Log#info(Object, Throwable)
182: */
183: public void info(Object message, Throwable t) {
184: if (getLogger().isInfoEnabled())
185: getLogger().info(String.valueOf(message), t);
186: }
187:
188: /**
189: * Logs a message with
190: * <code>org.apache.avalon.framework.logger.Logger.info</code>.
191: *
192: * @param message to log
193: * @see org.apache.commons.logging.Log#info(Object)
194: */
195: public void info(Object message) {
196: if (getLogger().isInfoEnabled())
197: getLogger().info(String.valueOf(message));
198: }
199:
200: /**
201: * Is logging to
202: * <code>org.apache.avalon.framework.logger.Logger.debug</code> enabled?
203: * @see org.apache.commons.logging.Log#isDebugEnabled()
204: */
205: public boolean isDebugEnabled() {
206: return getLogger().isDebugEnabled();
207: }
208:
209: /**
210: * Is logging to
211: * <code>org.apache.avalon.framework.logger.Logger.error</code> enabled?
212: * @see org.apache.commons.logging.Log#isErrorEnabled()
213: */
214: public boolean isErrorEnabled() {
215: return getLogger().isErrorEnabled();
216: }
217:
218: /**
219: * Is logging to
220: * <code>org.apache.avalon.framework.logger.Logger.fatalError</code> enabled?
221: * @see org.apache.commons.logging.Log#isFatalEnabled()
222: */
223: public boolean isFatalEnabled() {
224: return getLogger().isFatalErrorEnabled();
225: }
226:
227: /**
228: * Is logging to
229: * <code>org.apache.avalon.framework.logger.Logger.info</code> enabled?
230: * @see org.apache.commons.logging.Log#isInfoEnabled()
231: */
232: public boolean isInfoEnabled() {
233: return getLogger().isInfoEnabled();
234: }
235:
236: /**
237: * Is logging to
238: * <code>org.apache.avalon.framework.logger.Logger.debug</code> enabled?
239: * @see org.apache.commons.logging.Log#isTraceEnabled()
240: */
241: public boolean isTraceEnabled() {
242: return getLogger().isDebugEnabled();
243: }
244:
245: /**
246: * Is logging to
247: * <code>org.apache.avalon.framework.logger.Logger.warn</code> enabled?
248: * @see org.apache.commons.logging.Log#isWarnEnabled()
249: */
250: public boolean isWarnEnabled() {
251: return getLogger().isWarnEnabled();
252: }
253:
254: /**
255: * Logs a message with
256: * <code>org.apache.avalon.framework.logger.Logger.debug</code>.
257: *
258: * @param message to log.
259: * @param t log this cause.
260: * @see org.apache.commons.logging.Log#trace(Object, Throwable)
261: */
262: public void trace(Object message, Throwable t) {
263: if (getLogger().isDebugEnabled())
264: getLogger().debug(String.valueOf(message), t);
265: }
266:
267: /**
268: * Logs a message with
269: * <code>org.apache.avalon.framework.logger.Logger.debug</code>.
270: *
271: * @param message to log
272: * @see org.apache.commons.logging.Log#trace(Object)
273: */
274: public void trace(Object message) {
275: if (getLogger().isDebugEnabled())
276: getLogger().debug(String.valueOf(message));
277: }
278:
279: /**
280: * Logs a message with
281: * <code>org.apache.avalon.framework.logger.Logger.warn</code>.
282: *
283: * @param message to log
284: * @param t log this cause
285: * @see org.apache.commons.logging.Log#warn(Object, Throwable)
286: */
287: public void warn(Object message, Throwable t) {
288: if (getLogger().isWarnEnabled())
289: getLogger().warn(String.valueOf(message), t);
290: }
291:
292: /**
293: * Logs a message with
294: * <code>org.apache.avalon.framework.logger.Logger.warn</code>.
295: *
296: * @param message to log
297: * @see org.apache.commons.logging.Log#warn(Object)
298: */
299: public void warn(Object message) {
300: if (getLogger().isWarnEnabled())
301: getLogger().warn(String.valueOf(message));
302: }
303:
304: }
|