1 /* 2 * Copyright (c) 1997, 2013, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. Oracle designates this 8 * particular file as subject to the "Classpath" exception as provided 9 * by Oracle in the LICENSE file that accompanied this code. 10 * 11 * This code is distributed in the hope that it will be useful, but WITHOUT 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 14 * version 2 for more details (a copy is included in the LICENSE file that 15 * accompanied this code). 16 * 17 * You should have received a copy of the GNU General Public License version 18 * 2 along with this work; if not, write to the Free Software Foundation, 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 20 * 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 22 * or visit www.oracle.com if you need additional information or have any 23 * questions. 24 */ 25 26 package com.sun.istack.internal.logging; 27 28 import com.sun.istack.internal.NotNull; 29 30 import java.util.StringTokenizer; 31 import java.util.logging.Level; 32 33 /** 34 * This is a helper class that provides some convenience methods wrapped around the 35 * standard {@link java.util.logging.Logger} interface. 36 * 37 * The class also makes sure that logger names of each Metro subsystem are consistent 38 * with each other. 39 * 40 * @author Marek Potociar <marek.potociar at sun.com> 41 * @author Fabian Ritzmann 42 */ 43 public class Logger { 44 45 private static final String WS_LOGGING_SUBSYSTEM_NAME_ROOT = "com.sun.metro"; 46 private static final String ROOT_WS_PACKAGE = "com.sun.xml.internal.ws."; 47 // 48 private static final Level METHOD_CALL_LEVEL_VALUE = Level.FINEST; 49 // 50 private final String componentClassName; 51 private final java.util.logging.Logger logger; 52 53 /** 54 * Prevents creation of a new instance of this Logger unless used by a subclass. 55 */ 56 protected Logger(final String systemLoggerName, final String componentName) { 57 this.componentClassName = "[" + componentName + "] "; 58 this.logger = java.util.logging.Logger.getLogger(systemLoggerName); 59 } 60 318 logger.entering(componentClassName, getCallerMethodName(), parameters); 319 } 320 321 public void exiting() { 322 if (!this.logger.isLoggable(METHOD_CALL_LEVEL_VALUE)) { 323 return; 324 } 325 logger.exiting(componentClassName, getCallerMethodName()); 326 } 327 328 public void exiting(final Object result) { 329 if (!this.logger.isLoggable(METHOD_CALL_LEVEL_VALUE)) { 330 return; 331 } 332 logger.exiting(componentClassName, getCallerMethodName(), result); 333 } 334 335 /** 336 * Method logs {@code exception}'s message as a {@code SEVERE} logging level 337 * message. 338 * <p/> 339 * If {@code cause} parameter is not {@code null}, it is logged as well and 340 * {@code exception} original cause is initialized with instance referenced 341 * by {@code cause} parameter. 342 * 343 * @param exception exception whose message should be logged. Must not be 344 * {@code null}. 345 * @param cause initial cause of the exception that should be logged as well 346 * and set as {@code exception}'s original cause. May be {@code null}. 347 * @return the same exception instance that was passed in as the {@code exception} 348 * parameter. 349 */ 350 public <T extends Throwable> T logSevereException(final T exception, final Throwable cause) { 351 if (this.logger.isLoggable(Level.SEVERE)) { 352 if (cause == null) { 353 logger.logp(Level.SEVERE, componentClassName, getCallerMethodName(), exception.getMessage()); 354 } else { 355 exception.initCause(cause); 356 logger.logp(Level.SEVERE, componentClassName, getCallerMethodName(), exception.getMessage(), cause); 357 } 358 } 359 360 return exception; 361 } 362 363 /** 364 * Method logs {@code exception}'s message as a {@code SEVERE} logging level 365 * message. 366 * <p/> 367 * If {@code logCause} parameter is {@code true}, {@code exception}'s original 368 * cause is logged as well (if exists). This may be used in cases when 369 * {@code exception}'s class provides constructor to initialize the original 370 * cause. In such case you do not need to use 371 * {@link #logSevereException(Throwable, Throwable)} 372 * method version but you might still want to log the original cause as well. 373 * 374 * @param exception exception whose message should be logged. Must not be 375 * {@code null}. 376 * @param logCause deterimnes whether initial cause of the exception should 377 * be logged as well 378 * @return the same exception instance that was passed in as the {@code exception} 379 * parameter. 380 */ 381 public <T extends Throwable> T logSevereException(final T exception, final boolean logCause) { 382 if (this.logger.isLoggable(Level.SEVERE)) { 383 if (logCause && exception.getCause() != null) { 384 logger.logp(Level.SEVERE, componentClassName, getCallerMethodName(), exception.getMessage(), exception.getCause()); 385 } else { 386 logger.logp(Level.SEVERE, componentClassName, getCallerMethodName(), exception.getMessage()); 391 } 392 393 /** 394 * Same as {@link #logSevereException(Throwable, boolean) logSevereException(exception, true)}. 395 */ 396 public <T extends Throwable> T logSevereException(final T exception) { 397 if (this.logger.isLoggable(Level.SEVERE)) { 398 if (exception.getCause() == null) { 399 logger.logp(Level.SEVERE, componentClassName, getCallerMethodName(), exception.getMessage()); 400 } else { 401 logger.logp(Level.SEVERE, componentClassName, getCallerMethodName(), exception.getMessage(), exception.getCause()); 402 } 403 } 404 405 return exception; 406 } 407 408 /** 409 * Method logs {@code exception}'s message at the logging level specified by the 410 * {@code level} argument. 411 * <p/> 412 * If {@code cause} parameter is not {@code null}, it is logged as well and 413 * {@code exception} original cause is initialized with instance referenced 414 * by {@code cause} parameter. 415 * 416 * @param exception exception whose message should be logged. Must not be 417 * {@code null}. 418 * @param cause initial cause of the exception that should be logged as well 419 * and set as {@code exception}'s original cause. May be {@code null}. 420 * @param level loging level which should be used for logging 421 * @return the same exception instance that was passed in as the {@code exception} 422 * parameter. 423 */ 424 public <T extends Throwable> T logException(final T exception, final Throwable cause, final Level level) { 425 if (this.logger.isLoggable(level)) { 426 if (cause == null) { 427 logger.logp(level, componentClassName, getCallerMethodName(), exception.getMessage()); 428 } else { 429 exception.initCause(cause); 430 logger.logp(level, componentClassName, getCallerMethodName(), exception.getMessage(), cause); 431 } 432 } 433 434 return exception; 435 } 436 437 /** 438 * Method logs {@code exception}'s message at the logging level specified by the 439 * {@code level} argument. 440 * <p/> 441 * If {@code logCause} parameter is {@code true}, {@code exception}'s original 442 * cause is logged as well (if exists). This may be used in cases when 443 * {@code exception}'s class provides constructor to initialize the original 444 * cause. In such case you do not need to use 445 * {@link #logException(Throwable, Throwable, Level) logException(exception, cause, level)} 446 * method version but you might still want to log the original cause as well. 447 * 448 * @param exception exception whose message should be logged. Must not be 449 * {@code null}. 450 * @param logCause deterimnes whether initial cause of the exception should 451 * be logged as well 452 * @param level loging level which should be used for logging 453 * @return the same exception instance that was passed in as the {@code exception} 454 * parameter. 455 */ 456 public <T extends Throwable> T logException(final T exception, final boolean logCause, final Level level) { 457 if (this.logger.isLoggable(level)) { 458 if (logCause && exception.getCause() != null) { 459 logger.logp(level, componentClassName, getCallerMethodName(), exception.getMessage(), exception.getCause()); 460 } else { | 1 /* 2 * Copyright (c) 1997, 2015, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. Oracle designates this 8 * particular file as subject to the "Classpath" exception as provided 9 * by Oracle in the LICENSE file that accompanied this code. 10 * 11 * This code is distributed in the hope that it will be useful, but WITHOUT 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 14 * version 2 for more details (a copy is included in the LICENSE file that 15 * accompanied this code). 16 * 17 * You should have received a copy of the GNU General Public License version 18 * 2 along with this work; if not, write to the Free Software Foundation, 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 20 * 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 22 * or visit www.oracle.com if you need additional information or have any 23 * questions. 24 */ 25 26 package com.sun.istack.internal.logging; 27 28 import com.sun.istack.internal.NotNull; 29 30 import java.util.StringTokenizer; 31 import java.util.logging.Level; 32 33 /** 34 * This is a helper class that provides some convenience methods wrapped around the 35 * standard {@link java.util.logging.Logger} interface. 36 * 37 * The class also makes sure that logger names of each Metro subsystem are consistent 38 * with each other. 39 * 40 * @author Marek Potociar 41 * @author Fabian Ritzmann 42 */ 43 public class Logger { 44 45 private static final String WS_LOGGING_SUBSYSTEM_NAME_ROOT = "com.sun.metro"; 46 private static final String ROOT_WS_PACKAGE = "com.sun.xml.internal.ws."; 47 // 48 private static final Level METHOD_CALL_LEVEL_VALUE = Level.FINEST; 49 // 50 private final String componentClassName; 51 private final java.util.logging.Logger logger; 52 53 /** 54 * Prevents creation of a new instance of this Logger unless used by a subclass. 55 */ 56 protected Logger(final String systemLoggerName, final String componentName) { 57 this.componentClassName = "[" + componentName + "] "; 58 this.logger = java.util.logging.Logger.getLogger(systemLoggerName); 59 } 60 318 logger.entering(componentClassName, getCallerMethodName(), parameters); 319 } 320 321 public void exiting() { 322 if (!this.logger.isLoggable(METHOD_CALL_LEVEL_VALUE)) { 323 return; 324 } 325 logger.exiting(componentClassName, getCallerMethodName()); 326 } 327 328 public void exiting(final Object result) { 329 if (!this.logger.isLoggable(METHOD_CALL_LEVEL_VALUE)) { 330 return; 331 } 332 logger.exiting(componentClassName, getCallerMethodName(), result); 333 } 334 335 /** 336 * Method logs {@code exception}'s message as a {@code SEVERE} logging level 337 * message. 338 * <p> 339 * If {@code cause} parameter is not {@code null}, it is logged as well and 340 * {@code exception} original cause is initialized with instance referenced 341 * by {@code cause} parameter. 342 * 343 * @param exception exception whose message should be logged. Must not be 344 * {@code null}. 345 * @param cause initial cause of the exception that should be logged as well 346 * and set as {@code exception}'s original cause. May be {@code null}. 347 * @return the same exception instance that was passed in as the {@code exception} 348 * parameter. 349 */ 350 public <T extends Throwable> T logSevereException(final T exception, final Throwable cause) { 351 if (this.logger.isLoggable(Level.SEVERE)) { 352 if (cause == null) { 353 logger.logp(Level.SEVERE, componentClassName, getCallerMethodName(), exception.getMessage()); 354 } else { 355 exception.initCause(cause); 356 logger.logp(Level.SEVERE, componentClassName, getCallerMethodName(), exception.getMessage(), cause); 357 } 358 } 359 360 return exception; 361 } 362 363 /** 364 * Method logs {@code exception}'s message as a {@code SEVERE} logging level 365 * message. 366 * <p> 367 * If {@code logCause} parameter is {@code true}, {@code exception}'s original 368 * cause is logged as well (if exists). This may be used in cases when 369 * {@code exception}'s class provides constructor to initialize the original 370 * cause. In such case you do not need to use 371 * {@link #logSevereException(Throwable, Throwable)} 372 * method version but you might still want to log the original cause as well. 373 * 374 * @param exception exception whose message should be logged. Must not be 375 * {@code null}. 376 * @param logCause deterimnes whether initial cause of the exception should 377 * be logged as well 378 * @return the same exception instance that was passed in as the {@code exception} 379 * parameter. 380 */ 381 public <T extends Throwable> T logSevereException(final T exception, final boolean logCause) { 382 if (this.logger.isLoggable(Level.SEVERE)) { 383 if (logCause && exception.getCause() != null) { 384 logger.logp(Level.SEVERE, componentClassName, getCallerMethodName(), exception.getMessage(), exception.getCause()); 385 } else { 386 logger.logp(Level.SEVERE, componentClassName, getCallerMethodName(), exception.getMessage()); 391 } 392 393 /** 394 * Same as {@link #logSevereException(Throwable, boolean) logSevereException(exception, true)}. 395 */ 396 public <T extends Throwable> T logSevereException(final T exception) { 397 if (this.logger.isLoggable(Level.SEVERE)) { 398 if (exception.getCause() == null) { 399 logger.logp(Level.SEVERE, componentClassName, getCallerMethodName(), exception.getMessage()); 400 } else { 401 logger.logp(Level.SEVERE, componentClassName, getCallerMethodName(), exception.getMessage(), exception.getCause()); 402 } 403 } 404 405 return exception; 406 } 407 408 /** 409 * Method logs {@code exception}'s message at the logging level specified by the 410 * {@code level} argument. 411 * <p> 412 * If {@code cause} parameter is not {@code null}, it is logged as well and 413 * {@code exception} original cause is initialized with instance referenced 414 * by {@code cause} parameter. 415 * 416 * @param exception exception whose message should be logged. Must not be 417 * {@code null}. 418 * @param cause initial cause of the exception that should be logged as well 419 * and set as {@code exception}'s original cause. May be {@code null}. 420 * @param level loging level which should be used for logging 421 * @return the same exception instance that was passed in as the {@code exception} 422 * parameter. 423 */ 424 public <T extends Throwable> T logException(final T exception, final Throwable cause, final Level level) { 425 if (this.logger.isLoggable(level)) { 426 if (cause == null) { 427 logger.logp(level, componentClassName, getCallerMethodName(), exception.getMessage()); 428 } else { 429 exception.initCause(cause); 430 logger.logp(level, componentClassName, getCallerMethodName(), exception.getMessage(), cause); 431 } 432 } 433 434 return exception; 435 } 436 437 /** 438 * Method logs {@code exception}'s message at the logging level specified by the 439 * {@code level} argument. 440 * <p> 441 * If {@code logCause} parameter is {@code true}, {@code exception}'s original 442 * cause is logged as well (if exists). This may be used in cases when 443 * {@code exception}'s class provides constructor to initialize the original 444 * cause. In such case you do not need to use 445 * {@link #logException(Throwable, Throwable, Level) logException(exception, cause, level)} 446 * method version but you might still want to log the original cause as well. 447 * 448 * @param exception exception whose message should be logged. Must not be 449 * {@code null}. 450 * @param logCause deterimnes whether initial cause of the exception should 451 * be logged as well 452 * @param level loging level which should be used for logging 453 * @return the same exception instance that was passed in as the {@code exception} 454 * parameter. 455 */ 456 public <T extends Throwable> T logException(final T exception, final boolean logCause, final Level level) { 457 if (this.logger.isLoggable(level)) { 458 if (logCause && exception.getCause() != null) { 459 logger.logp(level, componentClassName, getCallerMethodName(), exception.getMessage(), exception.getCause()); 460 } else { |