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 */ 017package org.apache.log4j; 018 019import org.apache.log4j.spi.LoggingEvent; 020 021/** 022 * This is a base class for LogMF and LogSF parameterized logging classes. 023 * 024 * 025 * @see org.apache.log4j.LogMF 026 * @see org.apache.log4j.LogSF 027 * @since 1.2.16 028 */ 029public abstract class LogXF { 030 /** 031 * Trace level. 032 */ 033 protected static final Level TRACE = new Level(5000, "TRACE", 7); 034 /** 035 * Fully Qualified Class Name of this class. 036 */ 037 private static final String FQCN = LogXF.class.getName(); 038 039 protected LogXF() { 040 } 041 042 /** 043 * Returns a Boolean instance representing the specified boolean. 044 * Boolean.valueOf was added in JDK 1.4. 045 * 046 * @param b a boolean value. 047 * @return a Boolean instance representing b. 048 */ 049 protected static Boolean valueOf(final boolean b) { 050 if (b) { 051 return Boolean.TRUE; 052 } 053 return Boolean.FALSE; 054 } 055 056 /** 057 * Returns a Character instance representing the specified char. 058 * Character.valueOf was added in JDK 1.5. 059 * 060 * @param c a character value. 061 * @return a Character instance representing c. 062 */ 063 protected static Character valueOf(final char c) { 064 return new Character(c); 065 } 066 067 /** 068 * Returns a Byte instance representing the specified byte. 069 * Byte.valueOf was added in JDK 1.5. 070 * 071 * @param b a byte value. 072 * @return a Byte instance representing b. 073 */ 074 protected static Byte valueOf(final byte b) { 075 return new Byte(b); 076 } 077 078 /** 079 * Returns a Short instance representing the specified short. 080 * Short.valueOf was added in JDK 1.5. 081 * 082 * @param b a short value. 083 * @return a Byte instance representing b. 084 */ 085 protected static Short valueOf(final short b) { 086 return new Short(b); 087 } 088 089 /** 090 * Returns an Integer instance representing the specified int. 091 * Integer.valueOf was added in JDK 1.5. 092 * 093 * @param b an int value. 094 * @return an Integer instance representing b. 095 */ 096 protected static Integer valueOf(final int b) { 097 return new Integer(b); 098 } 099 100 /** 101 * Returns a Long instance representing the specified long. 102 * Long.valueOf was added in JDK 1.5. 103 * 104 * @param b a long value. 105 * @return a Long instance representing b. 106 */ 107 protected static Long valueOf(final long b) { 108 return new Long(b); 109 } 110 111 /** 112 * Returns a Float instance representing the specified float. 113 * Float.valueOf was added in JDK 1.5. 114 * 115 * @param b a float value. 116 * @return a Float instance representing b. 117 */ 118 protected static Float valueOf(final float b) { 119 return new Float(b); 120 } 121 122 /** 123 * Returns a Double instance representing the specified double. 124 * Double.valueOf was added in JDK 1.5. 125 * 126 * @param b a double value. 127 * @return a Byte instance representing b. 128 */ 129 protected static Double valueOf(final double b) { 130 return new Double(b); 131 } 132 133 /** 134 * Create new array. 135 * 136 * @param param1 parameter 1. 137 * @return new array. 138 */ 139 protected static Object[] toArray(final Object param1) { 140 return new Object[]{ 141 param1 142 }; 143 } 144 145 /** 146 * Create new array. 147 * 148 * @param param1 parameter 1. 149 * @param param2 parameter 2. 150 * @return new array. 151 */ 152 protected static Object[] toArray(final Object param1, 153 final Object param2) { 154 return new Object[]{ 155 param1, param2 156 }; 157 } 158 159 /** 160 * Create new array. 161 * 162 * @param param1 parameter 1. 163 * @param param2 parameter 2. 164 * @param param3 parameter 3. 165 * @return new array. 166 */ 167 protected static Object[] toArray(final Object param1, 168 final Object param2, 169 final Object param3) { 170 return new Object[]{ 171 param1, param2, param3 172 }; 173 } 174 175 /** 176 * Create new array. 177 * 178 * @param param1 parameter 1. 179 * @param param2 parameter 2. 180 * @param param3 parameter 3. 181 * @param param4 parameter 4. 182 * @return new array. 183 */ 184 protected static Object[] toArray(final Object param1, 185 final Object param2, 186 final Object param3, 187 final Object param4) { 188 return new Object[]{ 189 param1, param2, param3, param4 190 }; 191 } 192 193 /** 194 * Log an entering message at DEBUG level. 195 * 196 * @param logger logger, may not be null. 197 * @param sourceClass source class, may be null. 198 * @param sourceMethod method, may be null. 199 */ 200 public static void entering(final Logger logger, 201 final String sourceClass, 202 final String sourceMethod) { 203 if (logger.isDebugEnabled()) { 204 logger.callAppenders(new LoggingEvent(FQCN, logger, Level.DEBUG, 205 sourceClass + "." + sourceMethod + " ENTRY", null)); 206 } 207 } 208 209 /** 210 * Log an entering message with a parameter at DEBUG level. 211 * 212 * @param logger logger, may not be null. 213 * @param sourceClass source class, may be null. 214 * @param sourceMethod method, may be null. 215 * @param param parameter, may be null. 216 */ 217 public static void entering(final Logger logger, 218 final String sourceClass, 219 final String sourceMethod, 220 final String param) { 221 if (logger.isDebugEnabled()) { 222 String msg = sourceClass + "." + sourceMethod + " ENTRY " + param; 223 logger.callAppenders(new LoggingEvent(FQCN, logger, Level.DEBUG, 224 msg, null)); 225 } 226 } 227 228 /** 229 * Log an entering message with a parameter at DEBUG level. 230 * 231 * @param logger logger, may not be null. 232 * @param sourceClass source class, may be null. 233 * @param sourceMethod method, may be null. 234 * @param param parameter, may be null. 235 */ 236 public static void entering(final Logger logger, 237 final String sourceClass, 238 final String sourceMethod, 239 final Object param) { 240 if (logger.isDebugEnabled()) { 241 String msg = sourceClass + "." + sourceMethod + " ENTRY "; 242 if (param == null) { 243 msg += "null"; 244 } else { 245 try { 246 msg += param; 247 } catch(Throwable ex) { 248 msg += "?"; 249 } 250 } 251 logger.callAppenders(new LoggingEvent(FQCN, logger, Level.DEBUG, 252 msg, null)); 253 } 254 } 255 256 /** 257 * Log an entering message with an array of parameters at DEBUG level. 258 * 259 * @param logger logger, may not be null. 260 * @param sourceClass source class, may be null. 261 * @param sourceMethod method, may be null. 262 * @param params parameters, may be null. 263 */ 264 public static void entering(final Logger logger, 265 final String sourceClass, 266 final String sourceMethod, 267 final Object[] params) { 268 if (logger.isDebugEnabled()) { 269 String msg = sourceClass + "." + sourceMethod + " ENTRY "; 270 if (params != null && params.length > 0) { 271 String delim = "{"; 272 for (int i = 0; i < params.length; i++) { 273 try { 274 msg += delim + params[i]; 275 } catch(Throwable ex) { 276 msg += delim + "?"; 277 } 278 delim = ","; 279 } 280 msg += "}"; 281 } else { 282 msg += "{}"; 283 } 284 logger.callAppenders(new LoggingEvent(FQCN, logger, Level.DEBUG, 285 msg, null)); 286 } 287 } 288 289 /** 290 * Log an exiting message at DEBUG level. 291 * 292 * @param logger logger, may not be null. 293 * @param sourceClass source class, may be null. 294 * @param sourceMethod method, may be null. 295 */ 296 public static void exiting(final Logger logger, 297 final String sourceClass, 298 final String sourceMethod) { 299 if (logger.isDebugEnabled()) { 300 logger.callAppenders(new LoggingEvent(FQCN, logger, Level.DEBUG, 301 sourceClass + "." + sourceMethod + " RETURN", null)); 302 } 303 } 304 305 /** 306 * Log an exiting message with result at DEBUG level. 307 * 308 * @param logger logger, may not be null. 309 * @param sourceClass source class, may be null. 310 * @param sourceMethod method, may be null. 311 * @param result result, may be null. 312 */ 313 public static void exiting( 314 final Logger logger, 315 final String sourceClass, 316 final String sourceMethod, 317 final String result) { 318 if (logger.isDebugEnabled()) { 319 logger.callAppenders(new LoggingEvent(FQCN, logger, Level.DEBUG, 320 sourceClass + "." + sourceMethod + " RETURN " + result, null)); 321 } 322 } 323 324 /** 325 * Log an exiting message with result at DEBUG level. 326 * 327 * @param logger logger, may not be null. 328 * @param sourceClass source class, may be null. 329 * @param sourceMethod method, may be null. 330 * @param result result, may be null. 331 */ 332 public static void exiting( 333 final Logger logger, 334 final String sourceClass, 335 final String sourceMethod, 336 final Object result) { 337 if (logger.isDebugEnabled()) { 338 String msg = sourceClass + "." + sourceMethod + " RETURN "; 339 if (result == null) { 340 msg += "null"; 341 } else { 342 try { 343 msg += result; 344 } catch(Throwable ex) { 345 msg += "?"; 346 } 347 } 348 logger.callAppenders(new LoggingEvent(FQCN, logger, Level.DEBUG, 349 msg, null)); 350 } 351 } 352 353 /** 354 * Logs a throwing message at DEBUG level. 355 * 356 * @param logger logger, may not be null. 357 * @param sourceClass source class, may be null. 358 * @param sourceMethod method, may be null. 359 * @param thrown throwable, may be null. 360 */ 361 public static void throwing( 362 final Logger logger, 363 final String sourceClass, 364 final String sourceMethod, 365 final Throwable thrown) { 366 if (logger.isDebugEnabled()) { 367 logger.callAppenders(new LoggingEvent(FQCN, logger, Level.DEBUG, 368 sourceClass + "." + sourceMethod + " THROW", thrown)); 369 } 370 } 371}