1 /* 2 * Licensed to the Apache Software Foundation (ASF) under one or more 3 * contributor license agreements. See the NOTICE file distributed with 4 * this work for additional information regarding copyright ownership. 5 * The ASF licenses this file to You under the Apache License, Version 2.0 6 * (the "License"); you may not use this file except in compliance with 7 * the License. You may obtain a copy of the License at 8 * 9 * http://www.apache.org/licenses/LICENSE-2.0 10 * 11 * Unless required by applicable law or agreed to in writing, software 12 * distributed under the License is distributed on an "AS IS" BASIS, 13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 * See the License for the specific language governing permissions and 15 * limitations under the License. 16 */ 17 18 package org.apache.commons.logging.impl; 19 20 import org.apache.avalon.framework.logger.Logger; 21 import org.apache.commons.logging.Log; 22 23 /** 24 * <p>Implementation of commons-logging Log interface that delegates all 25 * logging calls to the Avalon logging abstraction: the Logger interface. 26 * </p> 27 * <p> 28 * There are two ways in which this class can be used: 29 * </p> 30 * <ul> 31 * <li>the instance can be constructed with an Avalon logger 32 * (by calling {@link #AvalonLogger(Logger)}). In this case, it acts 33 * as a simple thin wrapping implementation over the logger. This is 34 * particularly useful when using a property setter. 35 * </li> 36 * <li>the {@link #setDefaultLogger} class property can be called which 37 * sets the ancesteral Avalon logger for this class. Any <code>AvalonLogger</code> 38 * instances created through the <code>LogFactory</code> mechanisms will output 39 * to child loggers of this <code>Logger</code>. 40 * </li> 41 * </ul> 42 * <p> 43 * <strong>Note:</strong> <code>AvalonLogger</code> does not implement Serializable 44 * because the constructors available for it make this impossible to achieve in all 45 * circumstances; there is no way to "reconnect" to an underlying Logger object on 46 * deserialization if one was just passed in to the constructor of the original 47 * object. This class <i>was</i> marked Serializable in the 1.0.4 release of 48 * commons-logging, but this never actually worked (a NullPointerException would 49 * be thrown as soon as the deserialized object was used), so removing this marker 50 * is not considered to be an incompatible change. 51 * </p> 52 * @author <a href="mailto:neeme@apache.org">Neeme Praks</a> 53 * @version $Revision: 424107 $ $Date: 2006-07-21 01:15:42 +0200 (fr, 21 jul 2006) $ 54 */ 55 public class AvalonLogger implements Log { 56 57 /** Ancesteral avalon logger */ 58 private static Logger defaultLogger = null; 59 /** Avalon logger used to perform log */ 60 private transient Logger logger = null; 61 62 /** 63 * Constructs an <code>AvalonLogger</code> that outputs to the given 64 * <code>Logger</code> instance. 65 * @param logger the avalon logger implementation to delegate to 66 */ 67 public AvalonLogger(Logger logger) { 68 this.logger = logger; 69 } 70 71 /** 72 * Constructs an <code>AvalonLogger</code> that will log to a child 73 * of the <code>Logger</code> set by calling {@link #setDefaultLogger}. 74 * @param name the name of the avalon logger implementation to delegate to 75 */ 76 public AvalonLogger(String name) { 77 if (defaultLogger == null) 78 throw new NullPointerException("default logger has to be specified if this constructor is used!"); 79 this.logger = defaultLogger.getChildLogger(name); 80 } 81 82 /** 83 * Gets the Avalon logger implementation used to perform logging. 84 * @return avalon logger implementation 85 */ 86 public Logger getLogger() { 87 return logger; 88 } 89 90 /** 91 * Sets the ancesteral Avalon logger from which the delegating loggers 92 * will descend. 93 * @param logger the default avalon logger, 94 * in case there is no logger instance supplied in constructor 95 */ 96 public static void setDefaultLogger(Logger logger) { 97 defaultLogger = logger; 98 } 99 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 }