001 /*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements. See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License. You may obtain a copy of the License at
008 *
009 * http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017
018 package org.apache.commons.logging.impl;
019
020 import org.apache.avalon.framework.logger.Logger;
021 import org.apache.commons.logging.Log;
022
023 /**
024 * <p>Implementation of commons-logging Log interface that delegates all
025 * logging calls to the Avalon logging abstraction: the Logger interface.
026 * </p>
027 * <p>
028 * There are two ways in which this class can be used:
029 * </p>
030 * <ul>
031 * <li>the instance can be constructed with an Avalon logger
032 * (by calling {@link #AvalonLogger(Logger)}). In this case, it acts
033 * as a simple thin wrapping implementation over the logger. This is
034 * particularly useful when using a property setter.
035 * </li>
036 * <li>the {@link #setDefaultLogger} class property can be called which
037 * sets the ancesteral Avalon logger for this class. Any <code>AvalonLogger</code>
038 * instances created through the <code>LogFactory</code> mechanisms will output
039 * to child loggers of this <code>Logger</code>.
040 * </li>
041 * </ul>
042 * <p>
043 * <strong>Note:</strong> <code>AvalonLogger</code> does not implement Serializable
044 * because the constructors available for it make this impossible to achieve in all
045 * circumstances; there is no way to "reconnect" to an underlying Logger object on
046 * deserialization if one was just passed in to the constructor of the original
047 * object. This class <i>was</i> marked Serializable in the 1.0.4 release of
048 * commons-logging, but this never actually worked (a NullPointerException would
049 * be thrown as soon as the deserialized object was used), so removing this marker
050 * is not considered to be an incompatible change.
051 * </p>
052 * @author <a href="mailto:neeme@apache.org">Neeme Praks</a>
053 * @version $Revision: 424107 $ $Date: 2006-07-21 01:15:42 +0200 (fr, 21 jul 2006) $
054 */
055 public class AvalonLogger implements Log {
056
057 /** Ancesteral avalon logger */
058 private static Logger defaultLogger = null;
059 /** Avalon logger used to perform log */
060 private transient Logger logger = null;
061
062 /**
063 * Constructs an <code>AvalonLogger</code> that outputs to the given
064 * <code>Logger</code> instance.
065 * @param logger the avalon logger implementation to delegate to
066 */
067 public AvalonLogger(Logger logger) {
068 this.logger = logger;
069 }
070
071 /**
072 * Constructs an <code>AvalonLogger</code> that will log to a child
073 * of the <code>Logger</code> set by calling {@link #setDefaultLogger}.
074 * @param name the name of the avalon logger implementation to delegate to
075 */
076 public AvalonLogger(String name) {
077 if (defaultLogger == null)
078 throw new NullPointerException("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()) getLogger().debug(String.valueOf(message), t);
110 }
111
112 /**
113 * Logs a message with
114 * <code>org.apache.avalon.framework.logger.Logger.debug</code>.
115 *
116 * @param message to log.
117 * @see org.apache.commons.logging.Log#debug(Object)
118 */
119 public void debug(Object message) {
120 if (getLogger().isDebugEnabled()) getLogger().debug(String.valueOf(message));
121 }
122
123 /**
124 * Logs a message with
125 * <code>org.apache.avalon.framework.logger.Logger.error</code>.
126 *
127 * @param message to log
128 * @param t log this cause
129 * @see org.apache.commons.logging.Log#error(Object, Throwable)
130 */
131 public void error(Object message, Throwable t) {
132 if (getLogger().isErrorEnabled()) getLogger().error(String.valueOf(message), t);
133 }
134
135 /**
136 * Logs a message with
137 * <code>org.apache.avalon.framework.logger.Logger.error</code>.
138 *
139 * @param message to log
140 * @see org.apache.commons.logging.Log#error(Object)
141 */
142 public void error(Object message) {
143 if (getLogger().isErrorEnabled()) getLogger().error(String.valueOf(message));
144 }
145
146 /**
147 * Logs a message with
148 * <code>org.apache.avalon.framework.logger.Logger.fatalError</code>.
149 *
150 * @param message to log.
151 * @param t log this cause.
152 * @see org.apache.commons.logging.Log#fatal(Object, Throwable)
153 */
154 public void fatal(Object message, Throwable t) {
155 if (getLogger().isFatalErrorEnabled()) getLogger().fatalError(String.valueOf(message), t);
156 }
157
158 /**
159 * Logs a message with
160 * <code>org.apache.avalon.framework.logger.Logger.fatalError</code>.
161 *
162 * @param message to log
163 * @see org.apache.commons.logging.Log#fatal(Object)
164 */
165 public void fatal(Object message) {
166 if (getLogger().isFatalErrorEnabled()) getLogger().fatalError(String.valueOf(message));
167 }
168
169 /**
170 * Logs a message with
171 * <code>org.apache.avalon.framework.logger.Logger.info</code>.
172 *
173 * @param message to log
174 * @param t log this cause
175 * @see org.apache.commons.logging.Log#info(Object, Throwable)
176 */
177 public void info(Object message, Throwable t) {
178 if (getLogger().isInfoEnabled()) getLogger().info(String.valueOf(message), t);
179 }
180
181 /**
182 * Logs a message with
183 * <code>org.apache.avalon.framework.logger.Logger.info</code>.
184 *
185 * @param message to log
186 * @see org.apache.commons.logging.Log#info(Object)
187 */
188 public void info(Object message) {
189 if (getLogger().isInfoEnabled()) getLogger().info(String.valueOf(message));
190 }
191
192 /**
193 * Is logging to
194 * <code>org.apache.avalon.framework.logger.Logger.debug</code> enabled?
195 * @see org.apache.commons.logging.Log#isDebugEnabled()
196 */
197 public boolean isDebugEnabled() {
198 return getLogger().isDebugEnabled();
199 }
200
201 /**
202 * Is logging to
203 * <code>org.apache.avalon.framework.logger.Logger.error</code> enabled?
204 * @see org.apache.commons.logging.Log#isErrorEnabled()
205 */
206 public boolean isErrorEnabled() {
207 return getLogger().isErrorEnabled();
208 }
209
210 /**
211 * Is logging to
212 * <code>org.apache.avalon.framework.logger.Logger.fatalError</code> enabled?
213 * @see org.apache.commons.logging.Log#isFatalEnabled()
214 */
215 public boolean isFatalEnabled() {
216 return getLogger().isFatalErrorEnabled();
217 }
218
219 /**
220 * Is logging to
221 * <code>org.apache.avalon.framework.logger.Logger.info</code> enabled?
222 * @see org.apache.commons.logging.Log#isInfoEnabled()
223 */
224 public boolean isInfoEnabled() {
225 return getLogger().isInfoEnabled();
226 }
227
228 /**
229 * Is logging to
230 * <code>org.apache.avalon.framework.logger.Logger.debug</code> enabled?
231 * @see org.apache.commons.logging.Log#isTraceEnabled()
232 */
233 public boolean isTraceEnabled() {
234 return getLogger().isDebugEnabled();
235 }
236
237 /**
238 * Is logging to
239 * <code>org.apache.avalon.framework.logger.Logger.warn</code> enabled?
240 * @see org.apache.commons.logging.Log#isWarnEnabled()
241 */
242 public boolean isWarnEnabled() {
243 return getLogger().isWarnEnabled();
244 }
245
246 /**
247 * Logs a message with
248 * <code>org.apache.avalon.framework.logger.Logger.debug</code>.
249 *
250 * @param message to log.
251 * @param t log this cause.
252 * @see org.apache.commons.logging.Log#trace(Object, Throwable)
253 */
254 public void trace(Object message, Throwable t) {
255 if (getLogger().isDebugEnabled()) getLogger().debug(String.valueOf(message), t);
256 }
257
258 /**
259 * Logs a message with
260 * <code>org.apache.avalon.framework.logger.Logger.debug</code>.
261 *
262 * @param message to log
263 * @see org.apache.commons.logging.Log#trace(Object)
264 */
265 public void trace(Object message) {
266 if (getLogger().isDebugEnabled()) getLogger().debug(String.valueOf(message));
267 }
268
269 /**
270 * Logs a message with
271 * <code>org.apache.avalon.framework.logger.Logger.warn</code>.
272 *
273 * @param message to log
274 * @param t log this cause
275 * @see org.apache.commons.logging.Log#warn(Object, Throwable)
276 */
277 public void warn(Object message, Throwable t) {
278 if (getLogger().isWarnEnabled()) getLogger().warn(String.valueOf(message), t);
279 }
280
281 /**
282 * Logs a message with
283 * <code>org.apache.avalon.framework.logger.Logger.warn</code>.
284 *
285 * @param message to log
286 * @see org.apache.commons.logging.Log#warn(Object)
287 */
288 public void warn(Object message) {
289 if (getLogger().isWarnEnabled()) getLogger().warn(String.valueOf(message));
290 }
291
292 }