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 java.io.Serializable; 21 22 import org.apache.commons.logging.Log; 23 import org.apache.log4j.Level; 24 import org.apache.log4j.Logger; 25 import org.apache.log4j.Priority; 26 27 /** 28 * Implements {@link Log} to map directly to a 29 * <strong>Logger</strong> for Apache Log4J version 1.2. 30 * <p> 31 * Initial configuration of the corresponding Logger instances should be done 32 * in the usual manner, as outlined in the Log4J documentation. 33 * <p> 34 * The reason this logger is distinct from the 1.3.0 logger is that in version 1.2 35 * of Log4J: 36 * <ul> 37 * <li>class Logger takes Priority parameters not Level parameters. 38 * <li>class Level extends Priority 39 * </ul> 40 * Log4j 1.3 is expected to change Level so it no longer extends Priority, which is 41 * a non-binary-compatible change. The class generated by compiling this code against 42 * Log4j 1.2 will therefore not run against Log4j 1.3. 43 * 44 * @deprecated Scheduled for removal since version 1.x of Log4j has reached end-of-life. 45 */ 46 @Deprecated 47 public class Log4JLogger implements Log, Serializable { 48 49 /** Serializable version identifier. */ 50 private static final long serialVersionUID = 5160705895411730424L; 51 52 /** The fully qualified name of the Log4JLogger class. */ 53 private static final String FQCN = Log4JLogger.class.getName(); 54 55 private static final Priority traceLevel; 56 57 // 58 // Note that this must come after the static variable declarations 59 // otherwise initializer expressions associated with those variables 60 // will override any settings done here. 61 // 62 // Verify that Log4j is available, and that it is version 1.2. 63 // If an ExceptionInInitializerError is generated, then LogFactoryImpl 64 // will treat that as meaning that the appropriate underlying logging 65 // library is just not present - if discovery is in progress then 66 // discovery will continue. 67 static { 68 if (!Priority.class.isAssignableFrom(Level.class)) { 69 // nope, this is Log4j 1.3, so force an ExceptionInInitializerError 70 throw new InstantiationError("Log4J 1.2 not available"); 71 } 72 73 // Releases of Log4j 1.2 >= 1.2.12 have Priority.TRACE available, earlier 74 // versions do not. If TRACE is not available, then we have to map 75 // calls to Log.trace(...) onto the DEBUG level. 76 77 Priority _traceLevel; 78 try { 79 _traceLevel = (Priority) Level.class.getDeclaredField("TRACE").get(null); 80 } catch (final Exception ex) { 81 // ok, trace not available 82 _traceLevel = Level.DEBUG; 83 } 84 traceLevel = _traceLevel; 85 } 86 87 /** Log to this logger */ 88 private transient volatile Logger logger; 89 90 /** Logger name */ 91 private final String name; 92 93 /** 94 * Constructs a new instance. 95 */ 96 public Log4JLogger() { 97 name = null; 98 } 99 100 /** 101 * For use with a Log4j factory. 102 * 103 * @param logger Logger. 104 */ 105 public Log4JLogger(final Logger logger) { 106 if (logger == null) { 107 throw new IllegalArgumentException( 108 "Warning - null logger in constructor; possible Log4j misconfiguration."); 109 } 110 this.name = logger.getName(); 111 this.logger = logger; 112 } 113 114 /** 115 * Base constructor. 116 * 117 * @param name name. 118 */ 119 public Log4JLogger(final String name) { 120 this.name = name; 121 this.logger = getLogger(); 122 } 123 124 /** 125 * Logs a message with {@code org.apache.log4j.Priority.DEBUG}. 126 * 127 * @param message to log 128 * @see org.apache.commons.logging.Log#debug(Object) 129 */ 130 @Override 131 public void debug(final Object message) { 132 getLogger().log(FQCN, Level.DEBUG, message, null); 133 } 134 135 /** 136 * Logs a message with {@code org.apache.log4j.Priority.DEBUG}. 137 * 138 * @param message to log 139 * @param t log this cause 140 * @see org.apache.commons.logging.Log#debug(Object, Throwable) 141 */ 142 @Override 143 public void debug(final Object message, final Throwable t) { 144 getLogger().log(FQCN, Level.DEBUG, message, t); 145 } 146 147 /** 148 * Logs a message with {@code org.apache.log4j.Priority.ERROR}. 149 * 150 * @param message to log 151 * @see org.apache.commons.logging.Log#error(Object) 152 */ 153 @Override 154 public void error(final Object message) { 155 getLogger().log(FQCN, Level.ERROR, message, null); 156 } 157 158 /** 159 * Logs a message with {@code org.apache.log4j.Priority.ERROR}. 160 * 161 * @param message to log 162 * @param t log this cause 163 * @see org.apache.commons.logging.Log#error(Object, Throwable) 164 */ 165 @Override 166 public void error(final Object message, final Throwable t) { 167 getLogger().log(FQCN, Level.ERROR, message, t); 168 } 169 170 /** 171 * Logs a message with {@code org.apache.log4j.Priority.FATAL}. 172 * 173 * @param message to log 174 * @see org.apache.commons.logging.Log#fatal(Object) 175 */ 176 @Override 177 public void fatal(final Object message) { 178 getLogger().log(FQCN, Level.FATAL, message, null); 179 } 180 181 /** 182 * Logs a message with {@code org.apache.log4j.Priority.FATAL}. 183 * 184 * @param message to log 185 * @param t log this cause 186 * @see org.apache.commons.logging.Log#fatal(Object, Throwable) 187 */ 188 @Override 189 public void fatal(final Object message, final Throwable t) { 190 getLogger().log(FQCN, Level.FATAL, message, t); 191 } 192 193 /** 194 * Gets the native Logger instance we are using. 195 * 196 * @return the native Logger instance we are using. 197 */ 198 public Logger getLogger() { 199 Logger result = logger; 200 if (result == null) { 201 synchronized(this) { 202 result = logger; 203 if (result == null) { 204 logger = result = Logger.getLogger(name); 205 } 206 } 207 } 208 return result; 209 } 210 211 /** 212 * Logs a message with {@code org.apache.log4j.Priority.INFO}. 213 * 214 * @param message to log 215 * @see org.apache.commons.logging.Log#info(Object) 216 */ 217 @Override 218 public void info(final Object message) { 219 getLogger().log(FQCN, Level.INFO, message, null); 220 } 221 222 /** 223 * Logs a message with {@code org.apache.log4j.Priority.INFO}. 224 * 225 * @param message to log 226 * @param t log this cause 227 * @see org.apache.commons.logging.Log#info(Object, Throwable) 228 */ 229 @Override 230 public void info(final Object message, final Throwable t) { 231 getLogger().log(FQCN, Level.INFO, message, t); 232 } 233 234 /** 235 * Tests whether the Log4j Logger used is enabled for {@code DEBUG} priority. 236 */ 237 @Override 238 public boolean isDebugEnabled() { 239 return getLogger().isDebugEnabled(); 240 } 241 242 /** 243 * Tests whether the Log4j Logger used is enabled for {@code ERROR} priority. 244 */ 245 @Override 246 public boolean isErrorEnabled() { 247 return getLogger().isEnabledFor(Level.ERROR); 248 } 249 250 /** 251 * Tests whether the Log4j Logger used is enabled for {@code FATAL} priority. 252 */ 253 @Override 254 public boolean isFatalEnabled() { 255 return getLogger().isEnabledFor(Level.FATAL); 256 } 257 258 /** 259 * Tests whether the Log4j Logger used is enabled for {@code INFO} priority. 260 */ 261 @Override 262 public boolean isInfoEnabled() { 263 return getLogger().isInfoEnabled(); 264 } 265 266 /** 267 * Tests whether the Log4j Logger used is enabled for {@code TRACE} priority. 268 * When using a Log4j version that does not support the TRACE level, this call 269 * will report whether {@code DEBUG} is enabled or not. 270 */ 271 @Override 272 public boolean isTraceEnabled() { 273 return getLogger().isEnabledFor(traceLevel); 274 } 275 276 /** 277 * Tests whether the Log4j Logger used is enabled for {@code WARN} priority. 278 */ 279 @Override 280 public boolean isWarnEnabled() { 281 return getLogger().isEnabledFor(Level.WARN); 282 } 283 284 /** 285 * Logs a message with {@code org.apache.log4j.Priority.TRACE}. 286 * When using a Log4j version that does not support the {@code TRACE} 287 * level, the message will be logged at the {@code DEBUG} level. 288 * 289 * @param message to log 290 * @see org.apache.commons.logging.Log#trace(Object) 291 */ 292 @Override 293 public void trace(final Object message) { 294 getLogger().log(FQCN, traceLevel, message, null); 295 } 296 297 /** 298 * Logs a message with {@code org.apache.log4j.Priority.TRACE}. 299 * When using a Log4j version that does not support the {@code TRACE} 300 * level, the message will be logged at the {@code DEBUG} level. 301 * 302 * @param message to log 303 * @param t log this cause 304 * @see org.apache.commons.logging.Log#trace(Object, Throwable) 305 */ 306 @Override 307 public void trace(final Object message, final Throwable t) { 308 getLogger().log(FQCN, traceLevel, message, t); 309 } 310 311 /** 312 * Logs a message with {@code org.apache.log4j.Priority.WARN}. 313 * 314 * @param message to log 315 * @see org.apache.commons.logging.Log#warn(Object) 316 */ 317 @Override 318 public void warn(final Object message) { 319 getLogger().log(FQCN, Level.WARN, message, null); 320 } 321 322 /** 323 * Logs a message with {@code org.apache.log4j.Priority.WARN}. 324 * 325 * @param message to log 326 * @param t log this cause 327 * @see org.apache.commons.logging.Log#warn(Object, Throwable) 328 */ 329 @Override 330 public void warn(final Object message, final Throwable t) { 331 getLogger().log(FQCN, Level.WARN, message, t); 332 } 333 334 }