src/share/jaxws_classes/com/sun/istack/internal/logging/Logger.java

Thu, 31 Aug 2017 15:18:52 +0800

author
aoqi
date
Thu, 31 Aug 2017 15:18:52 +0800
changeset 637
9c07ef4934dd
parent 368
0989ad8c0860
parent 0
373ffda63c9a
permissions
-rw-r--r--

merge

aoqi@0 1 /*
aoqi@0 2 * Copyright (c) 1997, 2012, Oracle and/or its affiliates. All rights reserved.
aoqi@0 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
aoqi@0 4 *
aoqi@0 5 * This code is free software; you can redistribute it and/or modify it
aoqi@0 6 * under the terms of the GNU General Public License version 2 only, as
aoqi@0 7 * published by the Free Software Foundation. Oracle designates this
aoqi@0 8 * particular file as subject to the "Classpath" exception as provided
aoqi@0 9 * by Oracle in the LICENSE file that accompanied this code.
aoqi@0 10 *
aoqi@0 11 * This code is distributed in the hope that it will be useful, but WITHOUT
aoqi@0 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
aoqi@0 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
aoqi@0 14 * version 2 for more details (a copy is included in the LICENSE file that
aoqi@0 15 * accompanied this code).
aoqi@0 16 *
aoqi@0 17 * You should have received a copy of the GNU General Public License version
aoqi@0 18 * 2 along with this work; if not, write to the Free Software Foundation,
aoqi@0 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
aoqi@0 20 *
aoqi@0 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
aoqi@0 22 * or visit www.oracle.com if you need additional information or have any
aoqi@0 23 * questions.
aoqi@0 24 */
aoqi@0 25
aoqi@0 26 package com.sun.istack.internal.logging;
aoqi@0 27
aoqi@0 28 import com.sun.istack.internal.NotNull;
aoqi@0 29
aoqi@0 30 import java.util.StringTokenizer;
aoqi@0 31 import java.util.logging.Level;
aoqi@0 32
aoqi@0 33 /**
aoqi@0 34 * This is a helper class that provides some convenience methods wrapped around the
aoqi@0 35 * standard {@link java.util.logging.Logger} interface.
aoqi@0 36 *
aoqi@0 37 * The class also makes sure that logger names of each Metro subsystem are consistent
aoqi@0 38 * with each other.
aoqi@0 39 *
aoqi@0 40 * @author Marek Potociar <marek.potociar at sun.com>
aoqi@0 41 * @author Fabian Ritzmann
aoqi@0 42 */
aoqi@0 43 public class Logger {
aoqi@0 44
aoqi@0 45 private static final String WS_LOGGING_SUBSYSTEM_NAME_ROOT = "com.sun.metro";
aoqi@0 46 private static final String ROOT_WS_PACKAGE = "com.sun.xml.internal.ws.";
aoqi@0 47 //
aoqi@0 48 private static final Level METHOD_CALL_LEVEL_VALUE = Level.FINEST;
aoqi@0 49 //
aoqi@0 50 private final String componentClassName;
aoqi@0 51 private final java.util.logging.Logger logger;
aoqi@0 52
aoqi@0 53 /**
aoqi@0 54 * Prevents creation of a new instance of this Logger unless used by a subclass.
aoqi@0 55 */
aoqi@0 56 protected Logger(final String systemLoggerName, final String componentName) {
aoqi@0 57 this.componentClassName = "[" + componentName + "] ";
aoqi@0 58 this.logger = java.util.logging.Logger.getLogger(systemLoggerName);
aoqi@0 59 }
aoqi@0 60
aoqi@0 61 /**
aoqi@0 62 * <p>
aoqi@0 63 * The factory method returns preconfigured Logger wrapper for the class. Method calls
aoqi@0 64 * {@link #getSystemLoggerName(java.lang.Class)} to generate default logger name.
aoqi@0 65 * </p>
aoqi@0 66 * <p>
aoqi@0 67 * Since there is no caching implemented, it is advised that the method is called only once
aoqi@0 68 * per a class in order to initialize a final static logger variable, which is then used
aoqi@0 69 * through the class to perform actual logging tasks.
aoqi@0 70 * </p>
aoqi@0 71 *
aoqi@0 72 * @param componentClass class of the component that will use the logger instance. Must not be {@code null}.
aoqi@0 73 * @return logger instance preconfigured for use with the component
aoqi@0 74 * @throws NullPointerException if the componentClass parameter is {@code null}.
aoqi@0 75 */
aoqi@0 76 public static @NotNull Logger getLogger(final @NotNull Class<?> componentClass) {
aoqi@0 77 return new Logger(getSystemLoggerName(componentClass), componentClass.getName());
aoqi@0 78 }
aoqi@0 79
aoqi@0 80 /**
aoqi@0 81 * The factory method returns preconfigured Logger wrapper for the class. Since there is no caching implemented,
aoqi@0 82 * it is advised that the method is called only once per a class in order to initialize a final static logger variable,
aoqi@0 83 * which is then used through the class to perform actual logging tasks.
aoqi@0 84 *
aoqi@0 85 * This method should be only used in a special cases when overriding of a default logger name derived from the
aoqi@0 86 * package of the component class is needed. For all common use cases please use {@link #getLogger(java.lang.Class)}
aoqi@0 87 * method.
aoqi@0 88 *
aoqi@0 89 * @param customLoggerName custom name of the logger.
aoqi@0 90 * @param componentClass class of the component that will use the logger instance. Must not be {@code null}.
aoqi@0 91 * @return logger instance preconfigured for use with the component
aoqi@0 92 * @throws NullPointerException if the componentClass parameter is {@code null}.
aoqi@0 93 *
aoqi@0 94 * @see #getLogger(java.lang.Class)
aoqi@0 95 */
aoqi@0 96 public static @NotNull Logger getLogger(final @NotNull String customLoggerName, final @NotNull Class<?> componentClass) {
aoqi@0 97 return new Logger(customLoggerName, componentClass.getName());
aoqi@0 98 }
aoqi@0 99
aoqi@0 100 /**
aoqi@0 101 * Calculates the subsystem suffix based on the package of the component class
aoqi@0 102 * @param componentClass class of the component that will use the logger instance. Must not be {@code null}.
aoqi@0 103 * @return system logger name for the given {@code componentClass} instance
aoqi@0 104 */
aoqi@0 105 static final String getSystemLoggerName(@NotNull Class<?> componentClass) {
aoqi@0 106 StringBuilder sb = new StringBuilder(componentClass.getPackage().getName());
aoqi@0 107 final int lastIndexOfWsPackage = sb.lastIndexOf(ROOT_WS_PACKAGE);
aoqi@0 108 if (lastIndexOfWsPackage > -1) {
aoqi@0 109 sb.replace(0, lastIndexOfWsPackage + ROOT_WS_PACKAGE.length(), "");
aoqi@0 110
aoqi@0 111 StringTokenizer st = new StringTokenizer(sb.toString(), ".");
aoqi@0 112 sb = new StringBuilder(WS_LOGGING_SUBSYSTEM_NAME_ROOT).append(".");
aoqi@0 113 if (st.hasMoreTokens()) {
aoqi@0 114 String token = st.nextToken();
aoqi@0 115 if ("api".equals(token)) {
aoqi@0 116 token = st.nextToken();
aoqi@0 117 }
aoqi@0 118 sb.append(token);
aoqi@0 119 }
aoqi@0 120 }
aoqi@0 121
aoqi@0 122 return sb.toString();
aoqi@0 123 }
aoqi@0 124
aoqi@0 125 public void log(final Level level, final String message) {
aoqi@0 126 if (!this.logger.isLoggable(level)) {
aoqi@0 127 return;
aoqi@0 128 }
aoqi@0 129 logger.logp(level, componentClassName, getCallerMethodName(), message);
aoqi@0 130 }
aoqi@0 131
aoqi@0 132 public void log(final Level level, final String message, Object param1) {
aoqi@0 133 if (!this.logger.isLoggable(level)) {
aoqi@0 134 return;
aoqi@0 135 }
aoqi@0 136 logger.logp(level, componentClassName, getCallerMethodName(), message, param1);
aoqi@0 137 }
aoqi@0 138
aoqi@0 139 public void log(final Level level, final String message, Object[] params) {
aoqi@0 140 if (!this.logger.isLoggable(level)) {
aoqi@0 141 return;
aoqi@0 142 }
aoqi@0 143 logger.logp(level, componentClassName, getCallerMethodName(), message, params);
aoqi@0 144 }
aoqi@0 145
aoqi@0 146 public void log(final Level level, final String message, final Throwable thrown) {
aoqi@0 147 if (!this.logger.isLoggable(level)) {
aoqi@0 148 return;
aoqi@0 149 }
aoqi@0 150 logger.logp(level, componentClassName, getCallerMethodName(), message, thrown);
aoqi@0 151 }
aoqi@0 152
aoqi@0 153 public void finest(final String message) {
aoqi@0 154 if (!this.logger.isLoggable(Level.FINEST)) {
aoqi@0 155 return;
aoqi@0 156 }
aoqi@0 157 logger.logp(Level.FINEST, componentClassName, getCallerMethodName(), message);
aoqi@0 158 }
aoqi@0 159
aoqi@0 160 public void finest(final String message, Object[] params) {
aoqi@0 161 if (!this.logger.isLoggable(Level.FINEST)) {
aoqi@0 162 return;
aoqi@0 163 }
aoqi@0 164 logger.logp(Level.FINEST, componentClassName, getCallerMethodName(), message, params);
aoqi@0 165 }
aoqi@0 166
aoqi@0 167 public void finest(final String message, final Throwable thrown) {
aoqi@0 168 if (!this.logger.isLoggable(Level.FINEST)) {
aoqi@0 169 return;
aoqi@0 170 }
aoqi@0 171 logger.logp(Level.FINEST, componentClassName, getCallerMethodName(), message, thrown);
aoqi@0 172 }
aoqi@0 173
aoqi@0 174 public void finer(final String message) {
aoqi@0 175 if (!this.logger.isLoggable(Level.FINER)) {
aoqi@0 176 return;
aoqi@0 177 }
aoqi@0 178 logger.logp(Level.FINER, componentClassName, getCallerMethodName(), message);
aoqi@0 179 }
aoqi@0 180
aoqi@0 181 public void finer(final String message, Object[] params) {
aoqi@0 182 if (!this.logger.isLoggable(Level.FINER)) {
aoqi@0 183 return;
aoqi@0 184 }
aoqi@0 185 logger.logp(Level.FINER, componentClassName, getCallerMethodName(), message, params);
aoqi@0 186 }
aoqi@0 187
aoqi@0 188 public void finer(final String message, final Throwable thrown) {
aoqi@0 189 if (!this.logger.isLoggable(Level.FINER)) {
aoqi@0 190 return;
aoqi@0 191 }
aoqi@0 192 logger.logp(Level.FINER, componentClassName, getCallerMethodName(), message, thrown);
aoqi@0 193 }
aoqi@0 194
aoqi@0 195 public void fine(final String message) {
aoqi@0 196 if (!this.logger.isLoggable(Level.FINE)) {
aoqi@0 197 return;
aoqi@0 198 }
aoqi@0 199 logger.logp(Level.FINE, componentClassName, getCallerMethodName(), message);
aoqi@0 200 }
aoqi@0 201
aoqi@0 202 public void fine(final String message, final Throwable thrown) {
aoqi@0 203 if (!this.logger.isLoggable(Level.FINE)) {
aoqi@0 204 return;
aoqi@0 205 }
aoqi@0 206 logger.logp(Level.FINE, componentClassName, getCallerMethodName(), message, thrown);
aoqi@0 207 }
aoqi@0 208
aoqi@0 209 public void info(final String message) {
aoqi@0 210 if (!this.logger.isLoggable(Level.INFO)) {
aoqi@0 211 return;
aoqi@0 212 }
aoqi@0 213 logger.logp(Level.INFO, componentClassName, getCallerMethodName(), message);
aoqi@0 214 }
aoqi@0 215
aoqi@0 216 public void info(final String message, Object[] params) {
aoqi@0 217 if (!this.logger.isLoggable(Level.INFO)) {
aoqi@0 218 return;
aoqi@0 219 }
aoqi@0 220 logger.logp(Level.INFO, componentClassName, getCallerMethodName(), message, params);
aoqi@0 221 }
aoqi@0 222
aoqi@0 223 public void info(final String message, final Throwable thrown) {
aoqi@0 224 if (!this.logger.isLoggable(Level.INFO)) {
aoqi@0 225 return;
aoqi@0 226 }
aoqi@0 227 logger.logp(Level.INFO, componentClassName, getCallerMethodName(), message, thrown);
aoqi@0 228 }
aoqi@0 229
aoqi@0 230 public void config(final String message) {
aoqi@0 231 if (!this.logger.isLoggable(Level.CONFIG)) {
aoqi@0 232 return;
aoqi@0 233 }
aoqi@0 234 logger.logp(Level.CONFIG, componentClassName, getCallerMethodName(), message);
aoqi@0 235 }
aoqi@0 236
aoqi@0 237 public void config(final String message, Object[] params) {
aoqi@0 238 if (!this.logger.isLoggable(Level.CONFIG)) {
aoqi@0 239 return;
aoqi@0 240 }
aoqi@0 241 logger.logp(Level.CONFIG, componentClassName, getCallerMethodName(), message, params);
aoqi@0 242 }
aoqi@0 243
aoqi@0 244 public void config(final String message, final Throwable thrown) {
aoqi@0 245 if (!this.logger.isLoggable(Level.CONFIG)) {
aoqi@0 246 return;
aoqi@0 247 }
aoqi@0 248 logger.logp(Level.CONFIG, componentClassName, getCallerMethodName(), message, thrown);
aoqi@0 249 }
aoqi@0 250
aoqi@0 251 public void warning(final String message) {
aoqi@0 252 if (!this.logger.isLoggable(Level.WARNING)) {
aoqi@0 253 return;
aoqi@0 254 }
aoqi@0 255 logger.logp(Level.WARNING, componentClassName, getCallerMethodName(), message);
aoqi@0 256 }
aoqi@0 257
aoqi@0 258 public void warning(final String message, Object[] params) {
aoqi@0 259 if (!this.logger.isLoggable(Level.WARNING)) {
aoqi@0 260 return;
aoqi@0 261 }
aoqi@0 262 logger.logp(Level.WARNING, componentClassName, getCallerMethodName(), message, params);
aoqi@0 263 }
aoqi@0 264
aoqi@0 265 public void warning(final String message, final Throwable thrown) {
aoqi@0 266 if (!this.logger.isLoggable(Level.WARNING)) {
aoqi@0 267 return;
aoqi@0 268 }
aoqi@0 269 logger.logp(Level.WARNING, componentClassName, getCallerMethodName(), message, thrown);
aoqi@0 270 }
aoqi@0 271
aoqi@0 272 public void severe(final String message) {
aoqi@0 273 if (!this.logger.isLoggable(Level.SEVERE)) {
aoqi@0 274 return;
aoqi@0 275 }
aoqi@0 276 logger.logp(Level.SEVERE, componentClassName, getCallerMethodName(), message);
aoqi@0 277 }
aoqi@0 278
aoqi@0 279 public void severe(final String message, Object[] params) {
aoqi@0 280 if (!this.logger.isLoggable(Level.SEVERE)) {
aoqi@0 281 return;
aoqi@0 282 }
aoqi@0 283 logger.logp(Level.SEVERE, componentClassName, getCallerMethodName(), message, params);
aoqi@0 284 }
aoqi@0 285
aoqi@0 286 public void severe(final String message, final Throwable thrown) {
aoqi@0 287 if (!this.logger.isLoggable(Level.SEVERE)) {
aoqi@0 288 return;
aoqi@0 289 }
aoqi@0 290 logger.logp(Level.SEVERE, componentClassName, getCallerMethodName(), message, thrown);
aoqi@0 291 }
aoqi@0 292
aoqi@0 293 public boolean isMethodCallLoggable() {
aoqi@0 294 return this.logger.isLoggable(METHOD_CALL_LEVEL_VALUE);
aoqi@0 295 }
aoqi@0 296
aoqi@0 297 public boolean isLoggable(final Level level) {
aoqi@0 298 return this.logger.isLoggable(level);
aoqi@0 299 }
aoqi@0 300
aoqi@0 301 public void setLevel(final Level level) {
aoqi@0 302 this.logger.setLevel(level);
aoqi@0 303 }
aoqi@0 304
aoqi@0 305 public void entering() {
aoqi@0 306 if (!this.logger.isLoggable(METHOD_CALL_LEVEL_VALUE)) {
aoqi@0 307 return;
aoqi@0 308 }
aoqi@0 309
aoqi@0 310 logger.entering(componentClassName, getCallerMethodName());
aoqi@0 311 }
aoqi@0 312
aoqi@0 313 public void entering(final Object... parameters) {
aoqi@0 314 if (!this.logger.isLoggable(METHOD_CALL_LEVEL_VALUE)) {
aoqi@0 315 return;
aoqi@0 316 }
aoqi@0 317
aoqi@0 318 logger.entering(componentClassName, getCallerMethodName(), parameters);
aoqi@0 319 }
aoqi@0 320
aoqi@0 321 public void exiting() {
aoqi@0 322 if (!this.logger.isLoggable(METHOD_CALL_LEVEL_VALUE)) {
aoqi@0 323 return;
aoqi@0 324 }
aoqi@0 325 logger.exiting(componentClassName, getCallerMethodName());
aoqi@0 326 }
aoqi@0 327
aoqi@0 328 public void exiting(final Object result) {
aoqi@0 329 if (!this.logger.isLoggable(METHOD_CALL_LEVEL_VALUE)) {
aoqi@0 330 return;
aoqi@0 331 }
aoqi@0 332 logger.exiting(componentClassName, getCallerMethodName(), result);
aoqi@0 333 }
aoqi@0 334
aoqi@0 335 /**
aoqi@0 336 * Method logs {@code exception}'s message as a {@code SEVERE} logging level
aoqi@0 337 * message.
aoqi@0 338 * <p/>
aoqi@0 339 * If {@code cause} parameter is not {@code null}, it is logged as well and
aoqi@0 340 * {@code exception} original cause is initialized with instance referenced
aoqi@0 341 * by {@code cause} parameter.
aoqi@0 342 *
aoqi@0 343 * @param exception exception whose message should be logged. Must not be
aoqi@0 344 * {@code null}.
aoqi@0 345 * @param cause initial cause of the exception that should be logged as well
aoqi@0 346 * and set as {@code exception}'s original cause. May be {@code null}.
aoqi@0 347 * @return the same exception instance that was passed in as the {@code exception}
aoqi@0 348 * parameter.
aoqi@0 349 */
aoqi@0 350 public <T extends Throwable> T logSevereException(final T exception, final Throwable cause) {
aoqi@0 351 if (this.logger.isLoggable(Level.SEVERE)) {
aoqi@0 352 if (cause == null) {
aoqi@0 353 logger.logp(Level.SEVERE, componentClassName, getCallerMethodName(), exception.getMessage());
aoqi@0 354 } else {
aoqi@0 355 exception.initCause(cause);
aoqi@0 356 logger.logp(Level.SEVERE, componentClassName, getCallerMethodName(), exception.getMessage(), cause);
aoqi@0 357 }
aoqi@0 358 }
aoqi@0 359
aoqi@0 360 return exception;
aoqi@0 361 }
aoqi@0 362
aoqi@0 363 /**
aoqi@0 364 * Method logs {@code exception}'s message as a {@code SEVERE} logging level
aoqi@0 365 * message.
aoqi@0 366 * <p/>
aoqi@0 367 * If {@code logCause} parameter is {@code true}, {@code exception}'s original
aoqi@0 368 * cause is logged as well (if exists). This may be used in cases when
aoqi@0 369 * {@code exception}'s class provides constructor to initialize the original
aoqi@0 370 * cause. In such case you do not need to use
aoqi@0 371 * {@link #logSevereException(Throwable, Throwable)}
aoqi@0 372 * method version but you might still want to log the original cause as well.
aoqi@0 373 *
aoqi@0 374 * @param exception exception whose message should be logged. Must not be
aoqi@0 375 * {@code null}.
aoqi@0 376 * @param logCause deterimnes whether initial cause of the exception should
aoqi@0 377 * be logged as well
aoqi@0 378 * @return the same exception instance that was passed in as the {@code exception}
aoqi@0 379 * parameter.
aoqi@0 380 */
aoqi@0 381 public <T extends Throwable> T logSevereException(final T exception, final boolean logCause) {
aoqi@0 382 if (this.logger.isLoggable(Level.SEVERE)) {
aoqi@0 383 if (logCause && exception.getCause() != null) {
aoqi@0 384 logger.logp(Level.SEVERE, componentClassName, getCallerMethodName(), exception.getMessage(), exception.getCause());
aoqi@0 385 } else {
aoqi@0 386 logger.logp(Level.SEVERE, componentClassName, getCallerMethodName(), exception.getMessage());
aoqi@0 387 }
aoqi@0 388 }
aoqi@0 389
aoqi@0 390 return exception;
aoqi@0 391 }
aoqi@0 392
aoqi@0 393 /**
aoqi@0 394 * Same as {@link #logSevereException(Throwable, boolean) logSevereException(exception, true)}.
aoqi@0 395 */
aoqi@0 396 public <T extends Throwable> T logSevereException(final T exception) {
aoqi@0 397 if (this.logger.isLoggable(Level.SEVERE)) {
aoqi@0 398 if (exception.getCause() == null) {
aoqi@0 399 logger.logp(Level.SEVERE, componentClassName, getCallerMethodName(), exception.getMessage());
aoqi@0 400 } else {
aoqi@0 401 logger.logp(Level.SEVERE, componentClassName, getCallerMethodName(), exception.getMessage(), exception.getCause());
aoqi@0 402 }
aoqi@0 403 }
aoqi@0 404
aoqi@0 405 return exception;
aoqi@0 406 }
aoqi@0 407
aoqi@0 408 /**
aoqi@0 409 * Method logs {@code exception}'s message at the logging level specified by the
aoqi@0 410 * {@code level} argument.
aoqi@0 411 * <p/>
aoqi@0 412 * If {@code cause} parameter is not {@code null}, it is logged as well and
aoqi@0 413 * {@code exception} original cause is initialized with instance referenced
aoqi@0 414 * by {@code cause} parameter.
aoqi@0 415 *
aoqi@0 416 * @param exception exception whose message should be logged. Must not be
aoqi@0 417 * {@code null}.
aoqi@0 418 * @param cause initial cause of the exception that should be logged as well
aoqi@0 419 * and set as {@code exception}'s original cause. May be {@code null}.
aoqi@0 420 * @param level loging level which should be used for logging
aoqi@0 421 * @return the same exception instance that was passed in as the {@code exception}
aoqi@0 422 * parameter.
aoqi@0 423 */
aoqi@0 424 public <T extends Throwable> T logException(final T exception, final Throwable cause, final Level level) {
aoqi@0 425 if (this.logger.isLoggable(level)) {
aoqi@0 426 if (cause == null) {
aoqi@0 427 logger.logp(level, componentClassName, getCallerMethodName(), exception.getMessage());
aoqi@0 428 } else {
aoqi@0 429 exception.initCause(cause);
aoqi@0 430 logger.logp(level, componentClassName, getCallerMethodName(), exception.getMessage(), cause);
aoqi@0 431 }
aoqi@0 432 }
aoqi@0 433
aoqi@0 434 return exception;
aoqi@0 435 }
aoqi@0 436
aoqi@0 437 /**
aoqi@0 438 * Method logs {@code exception}'s message at the logging level specified by the
aoqi@0 439 * {@code level} argument.
aoqi@0 440 * <p/>
aoqi@0 441 * If {@code logCause} parameter is {@code true}, {@code exception}'s original
aoqi@0 442 * cause is logged as well (if exists). This may be used in cases when
aoqi@0 443 * {@code exception}'s class provides constructor to initialize the original
aoqi@0 444 * cause. In such case you do not need to use
aoqi@0 445 * {@link #logException(Throwable, Throwable, Level) logException(exception, cause, level)}
aoqi@0 446 * method version but you might still want to log the original cause as well.
aoqi@0 447 *
aoqi@0 448 * @param exception exception whose message should be logged. Must not be
aoqi@0 449 * {@code null}.
aoqi@0 450 * @param logCause deterimnes whether initial cause of the exception should
aoqi@0 451 * be logged as well
aoqi@0 452 * @param level loging level which should be used for logging
aoqi@0 453 * @return the same exception instance that was passed in as the {@code exception}
aoqi@0 454 * parameter.
aoqi@0 455 */
aoqi@0 456 public <T extends Throwable> T logException(final T exception, final boolean logCause, final Level level) {
aoqi@0 457 if (this.logger.isLoggable(level)) {
aoqi@0 458 if (logCause && exception.getCause() != null) {
aoqi@0 459 logger.logp(level, componentClassName, getCallerMethodName(), exception.getMessage(), exception.getCause());
aoqi@0 460 } else {
aoqi@0 461 logger.logp(level, componentClassName, getCallerMethodName(), exception.getMessage());
aoqi@0 462 }
aoqi@0 463 }
aoqi@0 464
aoqi@0 465 return exception;
aoqi@0 466 }
aoqi@0 467
aoqi@0 468 /**
aoqi@0 469 * Same as {@link #logException(Throwable, Throwable, Level)
aoqi@0 470 * logException(exception, true, level)}.
aoqi@0 471 */
aoqi@0 472 public <T extends Throwable> T logException(final T exception, final Level level) {
aoqi@0 473 if (this.logger.isLoggable(level)) {
aoqi@0 474 if (exception.getCause() == null) {
aoqi@0 475 logger.logp(level, componentClassName, getCallerMethodName(), exception.getMessage());
aoqi@0 476 } else {
aoqi@0 477 logger.logp(level, componentClassName, getCallerMethodName(), exception.getMessage(), exception.getCause());
aoqi@0 478 }
aoqi@0 479 }
aoqi@0 480
aoqi@0 481 return exception;
aoqi@0 482 }
aoqi@0 483
aoqi@0 484 /**
aoqi@0 485 * Function returns the name of the caller method for the method executing this
aoqi@0 486 * function.
aoqi@0 487 *
aoqi@0 488 * @return caller method name from the call stack of the current {@link Thread}.
aoqi@0 489 */
aoqi@0 490 private static String getCallerMethodName() {
aoqi@0 491 return getStackMethodName(5);
aoqi@0 492 }
aoqi@0 493
aoqi@0 494 /**
aoqi@0 495 * Method returns the name of the method that is on the {@code methodIndexInStack}
aoqi@0 496 * position in the call stack of the current {@link Thread}.
aoqi@0 497 *
aoqi@0 498 * @param methodIndexInStack index to the call stack to get the method name for.
aoqi@0 499 * @return the name of the method that is on the {@code methodIndexInStack}
aoqi@0 500 * position in the call stack of the current {@link Thread}.
aoqi@0 501 */
aoqi@0 502 private static String getStackMethodName(final int methodIndexInStack) {
aoqi@0 503 final String methodName;
aoqi@0 504
aoqi@0 505 final StackTraceElement[] stack = Thread.currentThread().getStackTrace();
aoqi@0 506 if (stack.length > methodIndexInStack + 1) {
aoqi@0 507 methodName = stack[methodIndexInStack].getMethodName();
aoqi@0 508 } else {
aoqi@0 509 methodName = "UNKNOWN METHOD";
aoqi@0 510 }
aoqi@0 511
aoqi@0 512 return methodName;
aoqi@0 513 }
aoqi@0 514
aoqi@0 515 }

mercurial