325N/A/*
325N/A * Copyright (c) 1997, 2010, Oracle and/or its affiliates. All rights reserved.
325N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
325N/A *
325N/A * This code is free software; you can redistribute it and/or modify it
325N/A * under the terms of the GNU General Public License version 2 only, as
325N/A * published by the Free Software Foundation. Oracle designates this
325N/A * particular file as subject to the "Classpath" exception as provided
325N/A * by Oracle in the LICENSE file that accompanied this code.
325N/A *
325N/A * This code is distributed in the hope that it will be useful, but WITHOUT
325N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
325N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
325N/A * version 2 for more details (a copy is included in the LICENSE file that
325N/A * accompanied this code).
325N/A *
325N/A * You should have received a copy of the GNU General Public License version
325N/A * 2 along with this work; if not, write to the Free Software Foundation,
325N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
325N/A *
325N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
325N/A * or visit www.oracle.com if you need additional information or have any
325N/A * questions.
325N/A */
325N/A
325N/Apackage com.sun.istack.internal.logging;
325N/A
325N/Aimport com.sun.istack.internal.NotNull;
325N/A
325N/Aimport java.util.StringTokenizer;
325N/Aimport java.util.logging.Level;
325N/A
325N/A/**
325N/A * This is a helper class that provides some conveniece methods wrapped around the
325N/A * standard {@link java.util.logging.Logger} interface.
325N/A *
325N/A * The class also makes sure that logger names of each Metro subsystem are consistent
325N/A * with each other.
325N/A *
325N/A * @author Marek Potociar <marek.potociar at sun.com>
325N/A * @author Fabian Ritzmann
325N/A */
325N/Apublic class Logger {
325N/A
325N/A private static final String WS_LOGGING_SUBSYSTEM_NAME_ROOT = "com.sun.metro";
325N/A private static final String ROOT_WS_PACKAGE = "com.sun.xml.internal.ws.";
325N/A //
325N/A private static final Level METHOD_CALL_LEVEL_VALUE = Level.FINEST;
325N/A //
325N/A private final String componentClassName;
325N/A private final java.util.logging.Logger logger;
325N/A
325N/A /**
325N/A * Prevents creation of a new instance of this Logger unless used by a subclass.
325N/A */
325N/A protected Logger(final String systemLoggerName, final String componentName) {
325N/A this.componentClassName = "[" + componentName + "] ";
325N/A this.logger = java.util.logging.Logger.getLogger(systemLoggerName);
325N/A }
325N/A
325N/A /**
325N/A * <p>
325N/A * The factory method returns preconfigured Logger wrapper for the class. Method calls
325N/A * {@link #getSystemLoggerName(java.lang.Class)} to generate default logger name.
325N/A * </p>
325N/A * <p>
325N/A * Since there is no caching implemented, it is advised that the method is called only once
325N/A * per a class in order to initialize a final static logger variable, which is then used
325N/A * through the class to perform actual logging tasks.
325N/A * </p>
325N/A *
325N/A * @param componentClass class of the component that will use the logger instance. Must not be {@code null}.
325N/A * @return logger instance preconfigured for use with the component
325N/A * @throws NullPointerException if the componentClass parameter is {@code null}.
325N/A */
325N/A public static @NotNull Logger getLogger(final @NotNull Class<?> componentClass) {
325N/A return new Logger(getSystemLoggerName(componentClass), componentClass.getName());
325N/A }
325N/A
325N/A /**
325N/A * The factory method returns preconfigured Logger wrapper for the class. Since there is no caching implemented,
325N/A * it is advised that the method is called only once per a class in order to initialize a final static logger variable,
325N/A * which is then used through the class to perform actual logging tasks.
325N/A *
325N/A * This method should be only used in a special cases when overriding of a default logger name derived from the
325N/A * package of the component class is needed. For all common use cases please use {@link #getLogger(java.lang.Class)}
325N/A * method.
325N/A *
325N/A * @param customLoggerName custom name of the logger.
325N/A * @param componentClass class of the component that will use the logger instance. Must not be {@code null}.
325N/A * @return logger instance preconfigured for use with the component
325N/A * @throws NullPointerException if the componentClass parameter is {@code null}.
325N/A *
325N/A * @see #getLogger(java.lang.Class)
325N/A */
325N/A public static @NotNull Logger getLogger(final @NotNull String customLoggerName, final @NotNull Class<?> componentClass) {
325N/A return new Logger(customLoggerName, componentClass.getName());
325N/A }
325N/A
325N/A /**
325N/A * Calculates the subsystem suffix based on the package of the component class
325N/A * @param componentClass class of the component that will use the logger instance. Must not be {@code null}.
325N/A * @return system logger name for the given {@code componentClass} instance
325N/A */
325N/A static final String getSystemLoggerName(@NotNull Class<?> componentClass) {
325N/A StringBuilder sb = new StringBuilder(componentClass.getPackage().getName());
325N/A final int lastIndexOfWsPackage = sb.lastIndexOf(ROOT_WS_PACKAGE);
325N/A if (lastIndexOfWsPackage > -1) {
325N/A sb.replace(0, lastIndexOfWsPackage + ROOT_WS_PACKAGE.length(), "");
325N/A
325N/A StringTokenizer st = new StringTokenizer(sb.toString(), ".");
325N/A sb = new StringBuilder(WS_LOGGING_SUBSYSTEM_NAME_ROOT).append(".");
325N/A if (st.hasMoreTokens()) {
325N/A String token = st.nextToken();
325N/A if ("api".equals(token)) {
325N/A token = st.nextToken();
325N/A }
325N/A sb.append(token);
325N/A }
325N/A }
325N/A
325N/A return sb.toString();
325N/A }
325N/A
325N/A public void log(final Level level, final String message) {
325N/A if (!this.logger.isLoggable(level)) {
325N/A return;
325N/A }
325N/A logger.logp(level, componentClassName, getCallerMethodName(), message);
325N/A }
325N/A
325N/A public void log(final Level level, final String message, final Throwable thrown) {
325N/A if (!this.logger.isLoggable(level)) {
325N/A return;
325N/A }
325N/A logger.logp(level, componentClassName, getCallerMethodName(), message, thrown);
325N/A }
325N/A
325N/A public void finest(final String message) {
325N/A if (!this.logger.isLoggable(Level.FINEST)) {
325N/A return;
325N/A }
325N/A logger.logp(Level.FINEST, componentClassName, getCallerMethodName(), message);
325N/A }
325N/A
325N/A public void finest(final String message, final Throwable thrown) {
325N/A if (!this.logger.isLoggable(Level.FINEST)) {
325N/A return;
325N/A }
325N/A logger.logp(Level.FINEST, componentClassName, getCallerMethodName(), message, thrown);
325N/A }
325N/A
325N/A public void finer(final String message) {
325N/A if (!this.logger.isLoggable(Level.FINER)) {
325N/A return;
325N/A }
325N/A logger.logp(Level.FINER, componentClassName, getCallerMethodName(), message);
325N/A }
325N/A
325N/A public void finer(final String message, final Throwable thrown) {
325N/A if (!this.logger.isLoggable(Level.FINER)) {
325N/A return;
325N/A }
325N/A logger.logp(Level.FINER, componentClassName, getCallerMethodName(), message, thrown);
325N/A }
325N/A
325N/A public void fine(final String message) {
325N/A if (!this.logger.isLoggable(Level.FINE)) {
325N/A return;
325N/A }
325N/A logger.logp(Level.FINE, componentClassName, getCallerMethodName(), message);
325N/A }
325N/A
325N/A public void fine(final String message, final Throwable thrown) {
325N/A if (!this.logger.isLoggable(Level.FINE)) {
325N/A return;
325N/A }
325N/A logger.logp(Level.FINE, componentClassName, getCallerMethodName(), message, thrown);
325N/A }
325N/A
325N/A public void info(final String message) {
325N/A if (!this.logger.isLoggable(Level.INFO)) {
325N/A return;
325N/A }
325N/A logger.logp(Level.INFO, componentClassName, getCallerMethodName(), message);
325N/A }
325N/A
325N/A public void info(final String message, final Throwable thrown) {
325N/A if (!this.logger.isLoggable(Level.INFO)) {
325N/A return;
325N/A }
325N/A logger.logp(Level.INFO, componentClassName, getCallerMethodName(), message, thrown);
325N/A }
325N/A
325N/A public void config(final String message) {
325N/A if (!this.logger.isLoggable(Level.CONFIG)) {
325N/A return;
325N/A }
325N/A logger.logp(Level.CONFIG, componentClassName, getCallerMethodName(), message);
325N/A }
325N/A
325N/A public void config(final String message, final Throwable thrown) {
325N/A if (!this.logger.isLoggable(Level.CONFIG)) {
325N/A return;
325N/A }
325N/A logger.logp(Level.CONFIG, componentClassName, getCallerMethodName(), message, thrown);
325N/A }
325N/A
325N/A public void warning(final String message) {
325N/A if (!this.logger.isLoggable(Level.WARNING)) {
325N/A return;
325N/A }
325N/A logger.logp(Level.WARNING, componentClassName, getCallerMethodName(), message);
325N/A }
325N/A
325N/A public void warning(final String message, final Throwable thrown) {
325N/A if (!this.logger.isLoggable(Level.WARNING)) {
325N/A return;
325N/A }
325N/A logger.logp(Level.WARNING, componentClassName, getCallerMethodName(), message, thrown);
325N/A }
325N/A
325N/A public void severe(final String message) {
325N/A if (!this.logger.isLoggable(Level.SEVERE)) {
325N/A return;
325N/A }
325N/A logger.logp(Level.SEVERE, componentClassName, getCallerMethodName(), message);
325N/A }
325N/A
325N/A public void severe(final String message, final Throwable thrown) {
325N/A if (!this.logger.isLoggable(Level.SEVERE)) {
325N/A return;
325N/A }
325N/A logger.logp(Level.SEVERE, componentClassName, getCallerMethodName(), message, thrown);
325N/A }
325N/A
325N/A public boolean isMethodCallLoggable() {
325N/A return this.logger.isLoggable(METHOD_CALL_LEVEL_VALUE);
325N/A }
325N/A
325N/A public boolean isLoggable(final Level level) {
325N/A return this.logger.isLoggable(level);
325N/A }
325N/A
325N/A public void setLevel(final Level level) {
325N/A this.logger.setLevel(level);
325N/A }
325N/A
325N/A public void entering() {
325N/A if (!this.logger.isLoggable(METHOD_CALL_LEVEL_VALUE)) {
325N/A return;
325N/A }
325N/A
325N/A logger.entering(componentClassName, getCallerMethodName());
325N/A }
325N/A
325N/A public void entering(final Object... parameters) {
325N/A if (!this.logger.isLoggable(METHOD_CALL_LEVEL_VALUE)) {
325N/A return;
325N/A }
325N/A
325N/A logger.entering(componentClassName, getCallerMethodName(), parameters);
325N/A }
325N/A
325N/A public void exiting() {
325N/A if (!this.logger.isLoggable(METHOD_CALL_LEVEL_VALUE)) {
325N/A return;
325N/A }
325N/A logger.exiting(componentClassName, getCallerMethodName());
325N/A }
325N/A
325N/A public void exiting(final Object result) {
325N/A if (!this.logger.isLoggable(METHOD_CALL_LEVEL_VALUE)) {
325N/A return;
325N/A }
325N/A logger.exiting(componentClassName, getCallerMethodName(), result);
325N/A }
325N/A
325N/A /**
325N/A * Method logs {@code exception}'s message as a {@code SEVERE} logging level
325N/A * message.
325N/A * <p/>
325N/A * If {@code cause} parameter is not {@code null}, it is logged as well and
325N/A * {@code exception} original cause is initialized with instance referenced
325N/A * by {@code cause} parameter.
325N/A *
325N/A * @param exception exception whose message should be logged. Must not be
325N/A * {@code null}.
325N/A * @param cause initial cause of the exception that should be logged as well
325N/A * and set as {@code exception}'s original cause. May be {@code null}.
325N/A * @return the same exception instance that was passed in as the {@code exception}
325N/A * parameter.
325N/A */
325N/A public <T extends Throwable> T logSevereException(final T exception, final Throwable cause) {
325N/A if (this.logger.isLoggable(Level.SEVERE)) {
325N/A if (cause == null) {
325N/A logger.logp(Level.SEVERE, componentClassName, getCallerMethodName(), exception.getMessage());
325N/A } else {
325N/A exception.initCause(cause);
325N/A logger.logp(Level.SEVERE, componentClassName, getCallerMethodName(), exception.getMessage(), cause);
325N/A }
325N/A }
325N/A
325N/A return exception;
325N/A }
325N/A
325N/A /**
325N/A * Method logs {@code exception}'s message as a {@code SEVERE} logging level
325N/A * message.
325N/A * <p/>
325N/A * If {@code logCause} parameter is {@code true}, {@code exception}'s original
325N/A * cause is logged as well (if exists). This may be used in cases when
325N/A * {@code exception}'s class provides constructor to initialize the original
325N/A * cause. In such case you do not need to use
325N/A * {@link #logSevereException(Throwable, Throwable)}
325N/A * method version but you might still want to log the original cause as well.
325N/A *
325N/A * @param exception exception whose message should be logged. Must not be
325N/A * {@code null}.
325N/A * @param logCause deterimnes whether initial cause of the exception should
325N/A * be logged as well
325N/A * @return the same exception instance that was passed in as the {@code exception}
325N/A * parameter.
325N/A */
325N/A public <T extends Throwable> T logSevereException(final T exception, final boolean logCause) {
325N/A if (this.logger.isLoggable(Level.SEVERE)) {
325N/A if (logCause && exception.getCause() != null) {
325N/A logger.logp(Level.SEVERE, componentClassName, getCallerMethodName(), exception.getMessage(), exception.getCause());
325N/A } else {
325N/A logger.logp(Level.SEVERE, componentClassName, getCallerMethodName(), exception.getMessage());
325N/A }
325N/A }
325N/A
325N/A return exception;
325N/A }
325N/A
325N/A /**
325N/A * Same as {@link #logSevereException(Throwable, boolean) logSevereException(exception, true)}.
325N/A */
325N/A public <T extends Throwable> T logSevereException(final T exception) {
325N/A if (this.logger.isLoggable(Level.SEVERE)) {
325N/A if (exception.getCause() == null) {
325N/A logger.logp(Level.SEVERE, componentClassName, getCallerMethodName(), exception.getMessage());
325N/A } else {
325N/A logger.logp(Level.SEVERE, componentClassName, getCallerMethodName(), exception.getMessage(), exception.getCause());
325N/A }
325N/A }
325N/A
325N/A return exception;
325N/A }
325N/A
325N/A /**
325N/A * Method logs {@code exception}'s message at the logging level specified by the
325N/A * {@code level} argument.
325N/A * <p/>
325N/A * If {@code cause} parameter is not {@code null}, it is logged as well and
325N/A * {@code exception} original cause is initialized with instance referenced
325N/A * by {@code cause} parameter.
325N/A *
325N/A * @param exception exception whose message should be logged. Must not be
325N/A * {@code null}.
325N/A * @param cause initial cause of the exception that should be logged as well
325N/A * and set as {@code exception}'s original cause. May be {@code null}.
325N/A * @param level loging level which should be used for logging
325N/A * @return the same exception instance that was passed in as the {@code exception}
325N/A * parameter.
325N/A */
325N/A public <T extends Throwable> T logException(final T exception, final Throwable cause, final Level level) {
325N/A if (this.logger.isLoggable(level)) {
325N/A if (cause == null) {
325N/A logger.logp(level, componentClassName, getCallerMethodName(), exception.getMessage());
325N/A } else {
325N/A exception.initCause(cause);
325N/A logger.logp(level, componentClassName, getCallerMethodName(), exception.getMessage(), cause);
325N/A }
325N/A }
325N/A
325N/A return exception;
325N/A }
325N/A
325N/A /**
325N/A * Method logs {@code exception}'s message at the logging level specified by the
325N/A * {@code level} argument.
325N/A * <p/>
325N/A * If {@code logCause} parameter is {@code true}, {@code exception}'s original
325N/A * cause is logged as well (if exists). This may be used in cases when
325N/A * {@code exception}'s class provides constructor to initialize the original
325N/A * cause. In such case you do not need to use
325N/A * {@link #logException(Throwable, Throwable, Level) logException(exception, cause, level)}
325N/A * method version but you might still want to log the original cause as well.
325N/A *
325N/A * @param exception exception whose message should be logged. Must not be
325N/A * {@code null}.
325N/A * @param logCause deterimnes whether initial cause of the exception should
325N/A * be logged as well
325N/A * @param level loging level which should be used for logging
325N/A * @return the same exception instance that was passed in as the {@code exception}
325N/A * parameter.
325N/A */
325N/A public <T extends Throwable> T logException(final T exception, final boolean logCause, final Level level) {
325N/A if (this.logger.isLoggable(level)) {
325N/A if (logCause && exception.getCause() != null) {
325N/A logger.logp(level, componentClassName, getCallerMethodName(), exception.getMessage(), exception.getCause());
325N/A } else {
325N/A logger.logp(level, componentClassName, getCallerMethodName(), exception.getMessage());
325N/A }
325N/A }
325N/A
325N/A return exception;
325N/A }
325N/A
325N/A /**
325N/A * Same as {@link #logException(Throwable, Throwable, Level)
325N/A * logException(exception, true, level)}.
325N/A */
325N/A public <T extends Throwable> T logException(final T exception, final Level level) {
325N/A if (this.logger.isLoggable(level)) {
325N/A if (exception.getCause() == null) {
325N/A logger.logp(level, componentClassName, getCallerMethodName(), exception.getMessage());
325N/A } else {
325N/A logger.logp(level, componentClassName, getCallerMethodName(), exception.getMessage(), exception.getCause());
325N/A }
325N/A }
325N/A
325N/A return exception;
325N/A }
325N/A
325N/A /**
325N/A * Function returns the name of the caller method for the method executing this
325N/A * function.
325N/A *
325N/A * @return caller method name from the call stack of the current {@link Thread}.
325N/A */
325N/A private static String getCallerMethodName() {
325N/A return getStackMethodName(5);
325N/A }
325N/A
325N/A /**
325N/A * Method returns the name of the method that is on the {@code methodIndexInStack}
325N/A * position in the call stack of the current {@link Thread}.
325N/A *
325N/A * @param methodIndexInStack index to the call stack to get the method name for.
325N/A * @return the name of the method that is on the {@code methodIndexInStack}
325N/A * position in the call stack of the current {@link Thread}.
325N/A */
325N/A private static String getStackMethodName(final int methodIndexInStack) {
325N/A final String methodName;
325N/A
325N/A final StackTraceElement[] stack = Thread.currentThread().getStackTrace();
325N/A if (stack.length > methodIndexInStack + 1) {
325N/A methodName = stack[methodIndexInStack].getMethodName();
325N/A } else {
325N/A methodName = "UNKNOWN METHOD";
325N/A }
325N/A
325N/A return methodName;
325N/A }
325N/A
325N/A}