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 018package org.apache.commons.logging.impl; 019 020import org.apache.avalon.framework.logger.Logger; 021import org.apache.commons.logging.Log; 022 023/** 024 * Implementation of commons-logging Log interface that delegates all 025 * logging calls to the Avalon logging abstraction: the Logger interface. 026 * <p> 027 * There are two ways in which this class can be used: 028 * <ul> 029 * <li>the instance can be constructed with an Avalon logger 030 * (by calling {@link #AvalonLogger(Logger)}). In this case, it acts 031 * as a simple thin wrapping implementation over the logger. This is 032 * particularly useful when using a property setter. 033 * </li> 034 * <li>the {@link #setDefaultLogger} class property can be called which 035 * sets the ancestral Avalon logger for this class. Any <code>AvalonLogger</code> 036 * instances created through the <code>LogFactory</code> mechanisms will output 037 * to child loggers of this <code>Logger</code>. 038 * </li> 039 * </ul> 040 * <p> 041 * <strong>Note:</strong> <code>AvalonLogger</code> does not implement Serializable 042 * because the constructors available for it make this impossible to achieve in all 043 * circumstances; there is no way to "reconnect" to an underlying Logger object on 044 * deserialization if one was just passed in to the constructor of the original 045 * object. This class <i>was</i> marked Serializable in the 1.0.4 release of 046 * commons-logging, but this never actually worked (a NullPointerException would 047 * be thrown as soon as the deserialized object was used), so removing this marker 048 * is not considered to be an incompatible change. 049 * 050 * @version $Id: AvalonLogger.html 915605 2014-07-09 20:22:43Z tn $ 051 */ 052public class AvalonLogger implements Log { 053 054 /** Ancestral Avalon logger. */ 055 private static volatile Logger defaultLogger = null; 056 /** Avalon logger used to perform log. */ 057 private final transient Logger logger; 058 059 /** 060 * Constructs an <code>AvalonLogger</code> that outputs to the given 061 * <code>Logger</code> instance. 062 * 063 * @param logger the Avalon logger implementation to delegate to 064 */ 065 public AvalonLogger(Logger logger) { 066 this.logger = logger; 067 } 068 069 /** 070 * Constructs an <code>AvalonLogger</code> that will log to a child 071 * of the <code>Logger</code> set by calling {@link #setDefaultLogger}. 072 * 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("default logger has to be specified if this constructor is used!"); 078 } 079 this.logger = defaultLogger.getChildLogger(name); 080 } 081 082 /** 083 * Gets the Avalon logger implementation used to perform logging. 084 * 085 * @return avalon logger implementation 086 */ 087 public Logger getLogger() { 088 return logger; 089 } 090 091 /** 092 * Sets the ancestral Avalon logger from which the delegating loggers will descend. 093 * 094 * @param logger the default avalon logger, 095 * in case there is no logger instance supplied in constructor 096 */ 097 public static void setDefaultLogger(Logger logger) { 098 defaultLogger = logger; 099 } 100 101 /** 102 * Logs a message with <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 /** 115 * Logs a message with <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 /** 127 * Logs a message with <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 /** 140 * Logs a message with <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 /** 152 * Logs a message with <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 /** 165 * Logs a message with <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 /** 177 * Logs a message with <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 /** 190 * Logs a message with <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 /** 202 * Is logging to <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 <code>org.apache.avalon.framework.logger.Logger.error</code> enabled? 211 * @see org.apache.commons.logging.Log#isErrorEnabled() 212 */ 213 public boolean isErrorEnabled() { 214 return getLogger().isErrorEnabled(); 215 } 216 217 /** 218 * Is logging to <code>org.apache.avalon.framework.logger.Logger.fatalError</code> enabled? 219 * @see org.apache.commons.logging.Log#isFatalEnabled() 220 */ 221 public boolean isFatalEnabled() { 222 return getLogger().isFatalErrorEnabled(); 223 } 224 225 /** 226 * Is logging to <code>org.apache.avalon.framework.logger.Logger.info</code> enabled? 227 * @see org.apache.commons.logging.Log#isInfoEnabled() 228 */ 229 public boolean isInfoEnabled() { 230 return getLogger().isInfoEnabled(); 231 } 232 233 /** 234 * Is logging to <code>org.apache.avalon.framework.logger.Logger.debug</code> enabled? 235 * @see org.apache.commons.logging.Log#isTraceEnabled() 236 */ 237 public boolean isTraceEnabled() { 238 return getLogger().isDebugEnabled(); 239 } 240 241 /** 242 * Is logging to <code>org.apache.avalon.framework.logger.Logger.warn</code> enabled? 243 * @see org.apache.commons.logging.Log#isWarnEnabled() 244 */ 245 public boolean isWarnEnabled() { 246 return getLogger().isWarnEnabled(); 247 } 248 249 /** 250 * Logs a message with <code>org.apache.avalon.framework.logger.Logger.debug</code>. 251 * 252 * @param message to log. 253 * @param t log this cause. 254 * @see org.apache.commons.logging.Log#trace(Object, Throwable) 255 */ 256 public void trace(Object message, Throwable t) { 257 if (getLogger().isDebugEnabled()) { 258 getLogger().debug(String.valueOf(message), t); 259 } 260 } 261 262 /** 263 * Logs a message with <code>org.apache.avalon.framework.logger.Logger.debug</code>. 264 * 265 * @param message to log 266 * @see org.apache.commons.logging.Log#trace(Object) 267 */ 268 public void trace(Object message) { 269 if (getLogger().isDebugEnabled()) { 270 getLogger().debug(String.valueOf(message)); 271 } 272 } 273 274 /** 275 * Logs a message with <code>org.apache.avalon.framework.logger.Logger.warn</code>. 276 * 277 * @param message to log 278 * @param t log this cause 279 * @see org.apache.commons.logging.Log#warn(Object, Throwable) 280 */ 281 public void warn(Object message, Throwable t) { 282 if (getLogger().isWarnEnabled()) { 283 getLogger().warn(String.valueOf(message), t); 284 } 285 } 286 287 /** 288 * Logs a message with <code>org.apache.avalon.framework.logger.Logger.warn</code>. 289 * 290 * @param message to log 291 * @see org.apache.commons.logging.Log#warn(Object) 292 */ 293 public void warn(Object message) { 294 if (getLogger().isWarnEnabled()) { 295 getLogger().warn(String.valueOf(message)); 296 } 297 } 298}