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