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