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 java.util.Objects; 021 022import org.apache.avalon.framework.logger.Logger; 023import org.apache.commons.logging.Log; 024 025/** 026 * Implements Commons Logging's Log interface to delegate all 027 * logging calls to the Avalon logging abstraction: the Logger interface. 028 * <p> 029 * There are two ways in which this class can be used: 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 ancestral Avalon logger for this class. Any {@code AvalonLogger} 038 * instances created through the {@code LogFactory} mechanisms will output 039 * to child loggers of this {@code Logger}. 040 * </li> 041 * </ul> 042 * <p> 043 * <strong>Note:</strong> {@code AvalonLogger} 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 <em>was</em> 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 * 052 * @deprecated Scheduled for removal because the Apache Avalon Project has been discontinued. 053 */ 054@Deprecated 055public class AvalonLogger implements Log { 056 057 /** Ancestral Avalon logger. */ 058 private static volatile Logger defaultLogger; 059 060 /** 061 * Sets the ancestral Avalon logger from which the delegating loggers will descend. 062 * 063 * @param logger the default avalon logger, 064 * in case there is no logger instance supplied in constructor 065 */ 066 public static void setDefaultLogger(final Logger logger) { 067 defaultLogger = logger; 068 } 069 070 /** Avalon logger used to perform log. */ 071 private final transient Logger logger; 072 073 /** 074 * Constructs an {@code AvalonLogger} that outputs to the given 075 * {@code Logger} instance. 076 * 077 * @param logger the Avalon logger implementation to delegate to 078 */ 079 public AvalonLogger(final Logger logger) { 080 this.logger = logger; 081 } 082 083 /** 084 * Constructs an {@code AvalonLogger} that will log to a child 085 * of the {@code Logger} set by calling {@link #setDefaultLogger}. 086 * 087 * @param name the name of the avalon logger implementation to delegate to 088 */ 089 public AvalonLogger(final String name) { 090 Objects.requireNonNull(defaultLogger, "defaultLogger"); 091 this.logger = defaultLogger.getChildLogger(name); 092 } 093 094 /** 095 * Logs a message with {@code org.apache.avalon.framework.logger.Logger.debug}. 096 * 097 * @param message to log. 098 * @see org.apache.commons.logging.Log#debug(Object) 099 */ 100 @Override 101 public void debug(final Object message) { 102 if (getLogger().isDebugEnabled()) { 103 getLogger().debug(String.valueOf(message)); 104 } 105 } 106 107 /** 108 * Logs a message with {@code org.apache.avalon.framework.logger.Logger.debug}. 109 * 110 * @param message to log 111 * @param t log this cause 112 * @see org.apache.commons.logging.Log#debug(Object, Throwable) 113 */ 114 @Override 115 public void debug(final Object message, final Throwable t) { 116 if (getLogger().isDebugEnabled()) { 117 getLogger().debug(String.valueOf(message), t); 118 } 119 } 120 121 /** 122 * Logs a message with {@code org.apache.avalon.framework.logger.Logger.error}. 123 * 124 * @param message to log 125 * @see org.apache.commons.logging.Log#error(Object) 126 */ 127 @Override 128 public void error(final Object message) { 129 if (getLogger().isErrorEnabled()) { 130 getLogger().error(String.valueOf(message)); 131 } 132 } 133 134 /** 135 * Logs a message with {@code org.apache.avalon.framework.logger.Logger.error}. 136 * 137 * @param message to log 138 * @param t log this cause 139 * @see org.apache.commons.logging.Log#error(Object, Throwable) 140 */ 141 @Override 142 public void error(final Object message, final Throwable t) { 143 if (getLogger().isErrorEnabled()) { 144 getLogger().error(String.valueOf(message), t); 145 } 146 } 147 148 /** 149 * Logs a message with {@code org.apache.avalon.framework.logger.Logger.fatalError}. 150 * 151 * @param message to log 152 * @see org.apache.commons.logging.Log#fatal(Object) 153 */ 154 @Override 155 public void fatal(final Object message) { 156 if (getLogger().isFatalErrorEnabled()) { 157 getLogger().fatalError(String.valueOf(message)); 158 } 159 } 160 161 /** 162 * Logs a message with {@code org.apache.avalon.framework.logger.Logger.fatalError}. 163 * 164 * @param message to log. 165 * @param t log this cause. 166 * @see org.apache.commons.logging.Log#fatal(Object, Throwable) 167 */ 168 @Override 169 public void fatal(final Object message, final Throwable t) { 170 if (getLogger().isFatalErrorEnabled()) { 171 getLogger().fatalError(String.valueOf(message), t); 172 } 173 } 174 175 /** 176 * Gets the Avalon logger implementation used to perform logging. 177 * 178 * @return avalon logger implementation 179 */ 180 public Logger getLogger() { 181 return logger; 182 } 183 184 /** 185 * Logs a message with {@code org.apache.avalon.framework.logger.Logger.info}. 186 * 187 * @param message to log 188 * @see org.apache.commons.logging.Log#info(Object) 189 */ 190 @Override 191 public void info(final Object message) { 192 if (getLogger().isInfoEnabled()) { 193 getLogger().info(String.valueOf(message)); 194 } 195 } 196 197 /** 198 * Logs a message with {@code org.apache.avalon.framework.logger.Logger.info}. 199 * 200 * @param message to log 201 * @param t log this cause 202 * @see org.apache.commons.logging.Log#info(Object, Throwable) 203 */ 204 @Override 205 public void info(final Object message, final Throwable t) { 206 if (getLogger().isInfoEnabled()) { 207 getLogger().info(String.valueOf(message), t); 208 } 209 } 210 211 /** 212 * Is logging to {@code org.apache.avalon.framework.logger.Logger.debug} enabled? 213 * @see org.apache.commons.logging.Log#isDebugEnabled() 214 */ 215 @Override 216 public boolean isDebugEnabled() { 217 return getLogger().isDebugEnabled(); 218 } 219 220 /** 221 * Is logging to {@code org.apache.avalon.framework.logger.Logger.error} enabled? 222 * @see org.apache.commons.logging.Log#isErrorEnabled() 223 */ 224 @Override 225 public boolean isErrorEnabled() { 226 return getLogger().isErrorEnabled(); 227 } 228 229 /** 230 * Is logging to {@code org.apache.avalon.framework.logger.Logger.fatalError} enabled? 231 * @see org.apache.commons.logging.Log#isFatalEnabled() 232 */ 233 @Override 234 public boolean isFatalEnabled() { 235 return getLogger().isFatalErrorEnabled(); 236 } 237 238 /** 239 * Is logging to {@code org.apache.avalon.framework.logger.Logger.info} enabled? 240 * @see org.apache.commons.logging.Log#isInfoEnabled() 241 */ 242 @Override 243 public boolean isInfoEnabled() { 244 return getLogger().isInfoEnabled(); 245 } 246 247 /** 248 * Is logging to {@code org.apache.avalon.framework.logger.Logger.debug} enabled? 249 * @see org.apache.commons.logging.Log#isTraceEnabled() 250 */ 251 @Override 252 public boolean isTraceEnabled() { 253 return getLogger().isDebugEnabled(); 254 } 255 256 /** 257 * Is logging to {@code org.apache.avalon.framework.logger.Logger.warn} enabled? 258 * @see org.apache.commons.logging.Log#isWarnEnabled() 259 */ 260 @Override 261 public boolean isWarnEnabled() { 262 return getLogger().isWarnEnabled(); 263 } 264 265 /** 266 * Logs a message with {@code org.apache.avalon.framework.logger.Logger.debug}. 267 * 268 * @param message to log 269 * @see org.apache.commons.logging.Log#trace(Object) 270 */ 271 @Override 272 public void trace(final Object message) { 273 if (getLogger().isDebugEnabled()) { 274 getLogger().debug(String.valueOf(message)); 275 } 276 } 277 278 /** 279 * Logs a message with {@code org.apache.avalon.framework.logger.Logger.debug}. 280 * 281 * @param message to log. 282 * @param t log this cause. 283 * @see org.apache.commons.logging.Log#trace(Object, Throwable) 284 */ 285 @Override 286 public void trace(final Object message, final Throwable t) { 287 if (getLogger().isDebugEnabled()) { 288 getLogger().debug(String.valueOf(message), t); 289 } 290 } 291 292 /** 293 * Logs a message with {@code org.apache.avalon.framework.logger.Logger.warn}. 294 * 295 * @param message to log 296 * @see org.apache.commons.logging.Log#warn(Object) 297 */ 298 @Override 299 public void warn(final Object message) { 300 if (getLogger().isWarnEnabled()) { 301 getLogger().warn(String.valueOf(message)); 302 } 303 } 304 305 /** 306 * Logs a message with {@code org.apache.avalon.framework.logger.Logger.warn}. 307 * 308 * @param message to log 309 * @param t log this cause 310 * @see org.apache.commons.logging.Log#warn(Object, Throwable) 311 */ 312 @Override 313 public void warn(final Object message, final Throwable t) { 314 if (getLogger().isWarnEnabled()) { 315 getLogger().warn(String.valueOf(message), t); 316 } 317 } 318}