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 package org.apache.commons.configuration; 18 19 import org.apache.commons.logging.impl.Log4JLogger; 20 import org.apache.log4j.Priority; 21 import org.apache.log4j.Level; 22 import org.apache.log4j.Appender; 23 import org.apache.log4j.PatternLayout; 24 import org.apache.log4j.ConsoleAppender; 25 26 /** 27 * Configures logging for tests. 28 * 29 * When running with Maven do -Dmaven.surefire.debug="LogLevel=level" to set the 30 * Log Level to the desired value. 31 */ 32 public class Logging extends Log4JLogger 33 { 34 /** 35 * The fully qualified name of the Log4JLogger class. 36 */ 37 private static final String FQCN = Logging.class.getName(); 38 39 private static Priority traceLevel; 40 41 static 42 { 43 // Releases of log4j1.2 >= 1.2.12 have Priority.TRACE available, earlier 44 // versions do not. If TRACE is not available, then we have to map 45 // calls to Log.trace(...) onto the DEBUG level. 46 47 try 48 { 49 traceLevel = (Priority) Level.class.getDeclaredField("TRACE").get(null); 50 } 51 catch (Exception ex) 52 { 53 // ok, trace not available 54 traceLevel = Priority.DEBUG; 55 } 56 57 String level = System.getProperty("LogLevel"); 58 if (level != null) 59 { 60 org.apache.log4j.Logger log = org.apache.log4j.Logger.getRootLogger(); 61 log.setLevel(Level.toLevel(level)); 62 Appender appender = new ConsoleAppender(new PatternLayout("%p %l - %m%n"), ConsoleAppender.SYSTEM_OUT); 63 log.addAppender(appender); 64 } 65 } 66 67 public Logging() 68 { 69 super(); 70 } 71 72 73 /** 74 * Base constructor. 75 */ 76 public Logging(String name) 77 { 78 super(name); 79 } 80 81 /** 82 * For use with a log4j factory. 83 */ 84 public Logging(org.apache.log4j.Logger logger) 85 { 86 super(logger); 87 } 88 89 // --------------------------------------------------------- 90 // Implementation 91 // 92 // Note that in the methods below the Priority class is used to define 93 // levels even though the Level class is supported in 1.2. This is done 94 // so that at compile time the call definitely resolves to a call to 95 // a method that takes a Priority rather than one that takes a Level. 96 // 97 // The Category class (and hence its subclass Logging) in version 1.2 only 98 // has methods that take Priority objects. The Category class (and hence 99 // Logging class) in version 1.3 has methods that take both Priority and 100 // Level objects. This means that if we use Level here, and compile 101 // against log4j 1.3 then calls would be bound to the versions of 102 // methods taking Level objects and then would fail to run against 103 // version 1.2 of log4j. 104 // --------------------------------------------------------- 105 106 107 /** 108 * Logs a message with <code>org.apache.log4j.Priority.TRACE</code>. 109 * When using a log4j version that does not support the <code>TRACE</code> 110 * level, the message will be logged at the <code>DEBUG</code> level. 111 * 112 * @param message to log 113 * @see org.apache.commons.logging.Log#trace(Object) 114 */ 115 @Override 116 public void trace(Object message) 117 { 118 getLogger().log(FQCN, traceLevel, message, null); 119 } 120 121 122 /** 123 * Logs a message with <code>org.apache.log4j.Priority.TRACE</code>. 124 * When using a log4j version that does not support the <code>TRACE</code> 125 * level, the message will be logged at the <code>DEBUG</code> level. 126 * 127 * @param message to log 128 * @param t log this cause 129 * @see org.apache.commons.logging.Log#trace(Object, Throwable) 130 */ 131 @Override 132 public void trace(Object message, Throwable t) 133 { 134 getLogger().log(FQCN, traceLevel, message, t); 135 } 136 137 138 /** 139 * Logs a message with <code>org.apache.log4j.Priority.DEBUG</code>. 140 * 141 * @param message to log 142 * @see org.apache.commons.logging.Log#debug(Object) 143 */ 144 @Override 145 public void debug(Object message) 146 { 147 getLogger().log(FQCN, Priority.DEBUG, message, null); 148 } 149 150 /** 151 * Logs a message with <code>org.apache.log4j.Priority.DEBUG</code>. 152 * 153 * @param message to log 154 * @param t log this cause 155 * @see org.apache.commons.logging.Log#debug(Object, Throwable) 156 */ 157 @Override 158 public void debug(Object message, Throwable t) 159 { 160 getLogger().log(FQCN, Priority.DEBUG, message, t); 161 } 162 163 164 /** 165 * Logs a message with <code>org.apache.log4j.Priority.INFO</code>. 166 * 167 * @param message to log 168 * @see org.apache.commons.logging.Log#info(Object) 169 */ 170 @Override 171 public void info(Object message) 172 { 173 getLogger().log(FQCN, Priority.INFO, message, null); 174 } 175 176 177 /** 178 * Logs a message with <code>org.apache.log4j.Priority.INFO</code>. 179 * 180 * @param message to log 181 * @param t log this cause 182 * @see org.apache.commons.logging.Log#info(Object, Throwable) 183 */ 184 @Override 185 public void info(Object message, Throwable t) 186 { 187 getLogger().log(FQCN, Priority.INFO, message, t); 188 } 189 190 191 /** 192 * Logs a message with <code>org.apache.log4j.Priority.WARN</code>. 193 * 194 * @param message to log 195 * @see org.apache.commons.logging.Log#warn(Object) 196 */ 197 @Override 198 public void warn(Object message) 199 { 200 getLogger().log(FQCN, Priority.WARN, message, null); 201 } 202 203 204 /** 205 * Logs a message with <code>org.apache.log4j.Priority.WARN</code>. 206 * 207 * @param message to log 208 * @param t log this cause 209 * @see org.apache.commons.logging.Log#warn(Object, Throwable) 210 */ 211 @Override 212 public void warn(Object message, Throwable t) 213 { 214 getLogger().log(FQCN, Priority.WARN, message, t); 215 } 216 217 218 /** 219 * Logs a message with <code>org.apache.log4j.Priority.ERROR</code>. 220 * 221 * @param message to log 222 * @see org.apache.commons.logging.Log#error(Object) 223 */ 224 @Override 225 public void error(Object message) 226 { 227 getLogger().log(FQCN, Priority.ERROR, message, null); 228 } 229 230 231 /** 232 * Logs a message with <code>org.apache.log4j.Priority.ERROR</code>. 233 * 234 * @param message to log 235 * @param t log this cause 236 * @see org.apache.commons.logging.Log#error(Object, Throwable) 237 */ 238 @Override 239 public void error(Object message, Throwable t) 240 { 241 getLogger().log(FQCN, Priority.ERROR, message, t); 242 } 243 244 245 /** 246 * Logs a message with <code>org.apache.log4j.Priority.FATAL</code>. 247 * 248 * @param message to log 249 * @see org.apache.commons.logging.Log#fatal(Object) 250 */ 251 @Override 252 public void fatal(Object message) 253 { 254 getLogger().log(FQCN, Priority.FATAL, message, null); 255 } 256 257 258 /** 259 * Logs a message with <code>org.apache.log4j.Priority.FATAL</code>. 260 * 261 * @param message to log 262 * @param t log this cause 263 * @see org.apache.commons.logging.Log#fatal(Object, Throwable) 264 */ 265 @Override 266 public void fatal(Object message, Throwable t) 267 { 268 getLogger().log(FQCN, Priority.FATAL, message, t); 269 } 270 271 }