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 19 package org.apache.commons.logging.impl; 20 21 import java.io.Serializable; 22 import org.apache.commons.logging.Log; 23 import org.apache.log4j.Logger; 24 import org.apache.log4j.Priority; 25 import org.apache.log4j.Level; 26 27 /** 28 * Implementation of {@link Log} that maps directly to a 29 * <strong>Logger</strong> for 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 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 * Log4J1.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 * @author <a href="mailto:sanders@apache.org">Scott Sanders</a> 45 * @author Rod Waldhoff 46 * @author Robert Burrell Donkin 47 * @version $Id: Log4JLogger.java 479747 2006-11-27 20:15:01Z dennisl $ 48 */ 49 50 public class Log4JLogger implements Log, Serializable { 51 52 // ------------------------------------------------------------- Attributes 53 54 /** The fully qualified name of the Log4JLogger class. */ 55 private static final String FQCN = Log4JLogger.class.getName(); 56 57 /** Log to this logger */ 58 private transient Logger logger = null; 59 60 /** Logger name */ 61 private String name = null; 62 63 private static Priority traceLevel; 64 65 // ------------------------------------------------------------ 66 // Static Initializer. 67 // 68 // Note that this must come after the static variable declarations 69 // otherwise initialiser expressions associated with those variables 70 // will override any settings done here. 71 // 72 // Verify that log4j is available, and that it is version 1.2. 73 // If an ExceptionInInitializerError is generated, then LogFactoryImpl 74 // will treat that as meaning that the appropriate underlying logging 75 // library is just not present - if discovery is in progress then 76 // discovery will continue. 77 // ------------------------------------------------------------ 78 79 static { 80 if (!Priority.class.isAssignableFrom(Level.class)) { 81 // nope, this is log4j 1.3, so force an ExceptionInInitializerError 82 throw new InstantiationError("Log4J 1.2 not available"); 83 } 84 85 // Releases of log4j1.2 >= 1.2.12 have Priority.TRACE available, earlier 86 // versions do not. If TRACE is not available, then we have to map 87 // calls to Log.trace(...) onto the DEBUG level. 88 89 try { 90 traceLevel = (Priority) Level.class.getDeclaredField("TRACE").get(null); 91 } catch(Exception ex) { 92 // ok, trace not available 93 traceLevel = Priority.DEBUG; 94 } 95 } 96 97 98 // ------------------------------------------------------------ Constructor 99 100 public Log4JLogger() { 101 } 102 103 104 /** 105 * Base constructor. 106 */ 107 public Log4JLogger(String name) { 108 this.name = name; 109 this.logger = getLogger(); 110 } 111 112 /** 113 * For use with a log4j factory. 114 */ 115 public Log4JLogger(Logger logger ) { 116 if (logger == null) { 117 throw new IllegalArgumentException( 118 "Warning - null logger in constructor; possible log4j misconfiguration."); 119 } 120 this.name = logger.getName(); 121 this.logger=logger; 122 } 123 124 125 // --------------------------------------------------------- 126 // Implementation 127 // 128 // Note that in the methods below the Priority class is used to define 129 // levels even though the Level class is supported in 1.2. This is done 130 // so that at compile time the call definitely resolves to a call to 131 // a method that takes a Priority rather than one that takes a Level. 132 // 133 // The Category class (and hence its subclass Logger) in version 1.2 only 134 // has methods that take Priority objects. The Category class (and hence 135 // Logger class) in version 1.3 has methods that take both Priority and 136 // Level objects. This means that if we use Level here, and compile 137 // against log4j 1.3 then calls would be bound to the versions of 138 // methods taking Level objects and then would fail to run against 139 // version 1.2 of log4j. 140 // --------------------------------------------------------- 141 142 143 /** 144 * Logs a message with <code>org.apache.log4j.Priority.TRACE</code>. 145 * When using a log4j version that does not support the <code>TRACE</code> 146 * level, the message will be logged at the <code>DEBUG</code> level. 147 * 148 * @param message to log 149 * @see org.apache.commons.logging.Log#trace(Object) 150 */ 151 public void trace(Object message) { 152 getLogger().log(FQCN, traceLevel, message, null ); 153 } 154 155 156 /** 157 * Logs a message with <code>org.apache.log4j.Priority.TRACE</code>. 158 * When using a log4j version that does not support the <code>TRACE</code> 159 * level, the message will be logged at the <code>DEBUG</code> level. 160 * 161 * @param message to log 162 * @param t log this cause 163 * @see org.apache.commons.logging.Log#trace(Object, Throwable) 164 */ 165 public void trace(Object message, Throwable t) { 166 getLogger().log(FQCN, traceLevel, message, t ); 167 } 168 169 170 /** 171 * Logs a message with <code>org.apache.log4j.Priority.DEBUG</code>. 172 * 173 * @param message to log 174 * @see org.apache.commons.logging.Log#debug(Object) 175 */ 176 public void debug(Object message) { 177 getLogger().log(FQCN, Priority.DEBUG, message, null ); 178 } 179 180 /** 181 * Logs a message with <code>org.apache.log4j.Priority.DEBUG</code>. 182 * 183 * @param message to log 184 * @param t log this cause 185 * @see org.apache.commons.logging.Log#debug(Object, Throwable) 186 */ 187 public void debug(Object message, Throwable t) { 188 getLogger().log(FQCN, Priority.DEBUG, message, t ); 189 } 190 191 192 /** 193 * Logs a message with <code>org.apache.log4j.Priority.INFO</code>. 194 * 195 * @param message to log 196 * @see org.apache.commons.logging.Log#info(Object) 197 */ 198 public void info(Object message) { 199 getLogger().log(FQCN, Priority.INFO, message, null ); 200 } 201 202 203 /** 204 * Logs a message with <code>org.apache.log4j.Priority.INFO</code>. 205 * 206 * @param message to log 207 * @param t log this cause 208 * @see org.apache.commons.logging.Log#info(Object, Throwable) 209 */ 210 public void info(Object message, Throwable t) { 211 getLogger().log(FQCN, Priority.INFO, message, t ); 212 } 213 214 215 /** 216 * Logs a message with <code>org.apache.log4j.Priority.WARN</code>. 217 * 218 * @param message to log 219 * @see org.apache.commons.logging.Log#warn(Object) 220 */ 221 public void warn(Object message) { 222 getLogger().log(FQCN, Priority.WARN, message, null ); 223 } 224 225 226 /** 227 * Logs a message with <code>org.apache.log4j.Priority.WARN</code>. 228 * 229 * @param message to log 230 * @param t log this cause 231 * @see org.apache.commons.logging.Log#warn(Object, Throwable) 232 */ 233 public void warn(Object message, Throwable t) { 234 getLogger().log(FQCN, Priority.WARN, message, t ); 235 } 236 237 238 /** 239 * Logs a message with <code>org.apache.log4j.Priority.ERROR</code>. 240 * 241 * @param message to log 242 * @see org.apache.commons.logging.Log#error(Object) 243 */ 244 public void error(Object message) { 245 getLogger().log(FQCN, Priority.ERROR, message, null ); 246 } 247 248 249 /** 250 * Logs a message with <code>org.apache.log4j.Priority.ERROR</code>. 251 * 252 * @param message to log 253 * @param t log this cause 254 * @see org.apache.commons.logging.Log#error(Object, Throwable) 255 */ 256 public void error(Object message, Throwable t) { 257 getLogger().log(FQCN, Priority.ERROR, message, t ); 258 } 259 260 261 /** 262 * Logs a message with <code>org.apache.log4j.Priority.FATAL</code>. 263 * 264 * @param message to log 265 * @see org.apache.commons.logging.Log#fatal(Object) 266 */ 267 public void fatal(Object message) { 268 getLogger().log(FQCN, Priority.FATAL, message, null ); 269 } 270 271 272 /** 273 * Logs a message with <code>org.apache.log4j.Priority.FATAL</code>. 274 * 275 * @param message to log 276 * @param t log this cause 277 * @see org.apache.commons.logging.Log#fatal(Object, Throwable) 278 */ 279 public void fatal(Object message, Throwable t) { 280 getLogger().log(FQCN, Priority.FATAL, message, t ); 281 } 282 283 284 /** 285 * Return the native Logger instance we are using. 286 */ 287 public Logger getLogger() { 288 if (logger == null) { 289 logger = Logger.getLogger(name); 290 } 291 return (this.logger); 292 } 293 294 295 /** 296 * Check whether the Log4j Logger used is enabled for <code>DEBUG</code> priority. 297 */ 298 public boolean isDebugEnabled() { 299 return getLogger().isDebugEnabled(); 300 } 301 302 303 /** 304 * Check whether the Log4j Logger used is enabled for <code>ERROR</code> priority. 305 */ 306 public boolean isErrorEnabled() { 307 return getLogger().isEnabledFor(Priority.ERROR); 308 } 309 310 311 /** 312 * Check whether the Log4j Logger used is enabled for <code>FATAL</code> priority. 313 */ 314 public boolean isFatalEnabled() { 315 return getLogger().isEnabledFor(Priority.FATAL); 316 } 317 318 319 /** 320 * Check whether the Log4j Logger used is enabled for <code>INFO</code> priority. 321 */ 322 public boolean isInfoEnabled() { 323 return getLogger().isInfoEnabled(); 324 } 325 326 327 /** 328 * Check whether the Log4j Logger used is enabled for <code>TRACE</code> priority. 329 * When using a log4j version that does not support the TRACE level, this call 330 * will report whether <code>DEBUG</code> is enabled or not. 331 */ 332 public boolean isTraceEnabled() { 333 return getLogger().isEnabledFor(traceLevel); 334 } 335 336 /** 337 * Check whether the Log4j Logger used is enabled for <code>WARN</code> priority. 338 */ 339 public boolean isWarnEnabled() { 340 return getLogger().isEnabledFor(Priority.WARN); 341 } 342 }