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 package org.apache.commons.configuration; 018 019 import org.apache.commons.logging.impl.Log4JLogger; 020 import org.apache.log4j.Priority; 021 import org.apache.log4j.Level; 022 import org.apache.log4j.Appender; 023 import org.apache.log4j.PatternLayout; 024 import org.apache.log4j.ConsoleAppender; 025 026 /** 027 * Configures logging for tests. 028 * 029 * When running with Maven do -Dmaven.surefire.debug="LogLevel=level" to set the 030 * Log Level to the desired value. 031 */ 032 public class Logging extends Log4JLogger 033 { 034 /** 035 * The fully qualified name of the Log4JLogger class. 036 */ 037 private static final String FQCN = Logging.class.getName(); 038 039 private static Priority traceLevel; 040 041 static 042 { 043 // Releases of log4j1.2 >= 1.2.12 have Priority.TRACE available, earlier 044 // versions do not. If TRACE is not available, then we have to map 045 // calls to Log.trace(...) onto the DEBUG level. 046 047 try 048 { 049 traceLevel = (Priority) Level.class.getDeclaredField("TRACE").get(null); 050 } 051 catch (Exception ex) 052 { 053 // ok, trace not available 054 traceLevel = Priority.DEBUG; 055 } 056 057 String level = System.getProperty("LogLevel"); 058 if (level != null) 059 { 060 org.apache.log4j.Logger log = org.apache.log4j.Logger.getRootLogger(); 061 log.setLevel(Level.toLevel(level)); 062 Appender appender = new ConsoleAppender(new PatternLayout("%p %l - %m%n"), ConsoleAppender.SYSTEM_OUT); 063 log.addAppender(appender); 064 } 065 } 066 067 public Logging() 068 { 069 super(); 070 } 071 072 073 /** 074 * Base constructor. 075 */ 076 public Logging(String name) 077 { 078 super(name); 079 } 080 081 /** 082 * For use with a log4j factory. 083 */ 084 public Logging(org.apache.log4j.Logger logger) 085 { 086 super(logger); 087 } 088 089 // --------------------------------------------------------- 090 // Implementation 091 // 092 // Note that in the methods below the Priority class is used to define 093 // levels even though the Level class is supported in 1.2. This is done 094 // so that at compile time the call definitely resolves to a call to 095 // a method that takes a Priority rather than one that takes a Level. 096 // 097 // The Category class (and hence its subclass Logging) in version 1.2 only 098 // has methods that take Priority objects. The Category class (and hence 099 // 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 }