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