Tue, 09 Apr 2013 14:51:13 +0100
8010393: Update JAX-WS RI to 2.2.9-b12941
Reviewed-by: alanb, erikj
Contributed-by: miroslav.kos@oracle.com, martin.grebac@oracle.com
ohair@286 | 1 | /* |
alanb@368 | 2 | * Copyright (c) 1997, 2012, 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 | /** |
alanb@368 | 34 | * This is a helper class that provides some convenience 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 | |
alanb@368 | 132 | public void log(final Level level, final String message, Object param1) { |
alanb@368 | 133 | if (!this.logger.isLoggable(level)) { |
alanb@368 | 134 | return; |
alanb@368 | 135 | } |
alanb@368 | 136 | logger.logp(level, componentClassName, getCallerMethodName(), message, param1); |
alanb@368 | 137 | } |
alanb@368 | 138 | |
alanb@368 | 139 | public void log(final Level level, final String message, Object[] params) { |
alanb@368 | 140 | if (!this.logger.isLoggable(level)) { |
alanb@368 | 141 | return; |
alanb@368 | 142 | } |
alanb@368 | 143 | logger.logp(level, componentClassName, getCallerMethodName(), message, params); |
alanb@368 | 144 | } |
alanb@368 | 145 | |
ohair@286 | 146 | public void log(final Level level, final String message, final Throwable thrown) { |
ohair@286 | 147 | if (!this.logger.isLoggable(level)) { |
ohair@286 | 148 | return; |
ohair@286 | 149 | } |
ohair@286 | 150 | logger.logp(level, componentClassName, getCallerMethodName(), message, thrown); |
ohair@286 | 151 | } |
ohair@286 | 152 | |
ohair@286 | 153 | public void finest(final String message) { |
ohair@286 | 154 | if (!this.logger.isLoggable(Level.FINEST)) { |
ohair@286 | 155 | return; |
ohair@286 | 156 | } |
ohair@286 | 157 | logger.logp(Level.FINEST, componentClassName, getCallerMethodName(), message); |
ohair@286 | 158 | } |
ohair@286 | 159 | |
alanb@368 | 160 | public void finest(final String message, Object[] params) { |
alanb@368 | 161 | if (!this.logger.isLoggable(Level.FINEST)) { |
alanb@368 | 162 | return; |
alanb@368 | 163 | } |
alanb@368 | 164 | logger.logp(Level.FINEST, componentClassName, getCallerMethodName(), message, params); |
alanb@368 | 165 | } |
alanb@368 | 166 | |
ohair@286 | 167 | public void finest(final String message, final Throwable thrown) { |
ohair@286 | 168 | if (!this.logger.isLoggable(Level.FINEST)) { |
ohair@286 | 169 | return; |
ohair@286 | 170 | } |
ohair@286 | 171 | logger.logp(Level.FINEST, componentClassName, getCallerMethodName(), message, thrown); |
ohair@286 | 172 | } |
ohair@286 | 173 | |
ohair@286 | 174 | public void finer(final String message) { |
ohair@286 | 175 | if (!this.logger.isLoggable(Level.FINER)) { |
ohair@286 | 176 | return; |
ohair@286 | 177 | } |
ohair@286 | 178 | logger.logp(Level.FINER, componentClassName, getCallerMethodName(), message); |
ohair@286 | 179 | } |
ohair@286 | 180 | |
alanb@368 | 181 | public void finer(final String message, Object[] params) { |
alanb@368 | 182 | if (!this.logger.isLoggable(Level.FINER)) { |
alanb@368 | 183 | return; |
alanb@368 | 184 | } |
alanb@368 | 185 | logger.logp(Level.FINER, componentClassName, getCallerMethodName(), message, params); |
alanb@368 | 186 | } |
alanb@368 | 187 | |
ohair@286 | 188 | public void finer(final String message, final Throwable thrown) { |
ohair@286 | 189 | if (!this.logger.isLoggable(Level.FINER)) { |
ohair@286 | 190 | return; |
ohair@286 | 191 | } |
ohair@286 | 192 | logger.logp(Level.FINER, componentClassName, getCallerMethodName(), message, thrown); |
ohair@286 | 193 | } |
ohair@286 | 194 | |
ohair@286 | 195 | public void fine(final String message) { |
ohair@286 | 196 | if (!this.logger.isLoggable(Level.FINE)) { |
ohair@286 | 197 | return; |
ohair@286 | 198 | } |
ohair@286 | 199 | logger.logp(Level.FINE, componentClassName, getCallerMethodName(), message); |
ohair@286 | 200 | } |
ohair@286 | 201 | |
ohair@286 | 202 | public void fine(final String message, final Throwable thrown) { |
ohair@286 | 203 | if (!this.logger.isLoggable(Level.FINE)) { |
ohair@286 | 204 | return; |
ohair@286 | 205 | } |
ohair@286 | 206 | logger.logp(Level.FINE, componentClassName, getCallerMethodName(), message, thrown); |
ohair@286 | 207 | } |
ohair@286 | 208 | |
ohair@286 | 209 | public void info(final String message) { |
ohair@286 | 210 | if (!this.logger.isLoggable(Level.INFO)) { |
ohair@286 | 211 | return; |
ohair@286 | 212 | } |
ohair@286 | 213 | logger.logp(Level.INFO, componentClassName, getCallerMethodName(), message); |
ohair@286 | 214 | } |
ohair@286 | 215 | |
alanb@368 | 216 | public void info(final String message, Object[] params) { |
alanb@368 | 217 | if (!this.logger.isLoggable(Level.INFO)) { |
alanb@368 | 218 | return; |
alanb@368 | 219 | } |
alanb@368 | 220 | logger.logp(Level.INFO, componentClassName, getCallerMethodName(), message, params); |
alanb@368 | 221 | } |
alanb@368 | 222 | |
ohair@286 | 223 | public void info(final String message, final Throwable thrown) { |
ohair@286 | 224 | if (!this.logger.isLoggable(Level.INFO)) { |
ohair@286 | 225 | return; |
ohair@286 | 226 | } |
ohair@286 | 227 | logger.logp(Level.INFO, componentClassName, getCallerMethodName(), message, thrown); |
ohair@286 | 228 | } |
ohair@286 | 229 | |
ohair@286 | 230 | public void config(final String message) { |
ohair@286 | 231 | if (!this.logger.isLoggable(Level.CONFIG)) { |
ohair@286 | 232 | return; |
ohair@286 | 233 | } |
ohair@286 | 234 | logger.logp(Level.CONFIG, componentClassName, getCallerMethodName(), message); |
ohair@286 | 235 | } |
ohair@286 | 236 | |
alanb@368 | 237 | public void config(final String message, Object[] params) { |
alanb@368 | 238 | if (!this.logger.isLoggable(Level.CONFIG)) { |
alanb@368 | 239 | return; |
alanb@368 | 240 | } |
alanb@368 | 241 | logger.logp(Level.CONFIG, componentClassName, getCallerMethodName(), message, params); |
alanb@368 | 242 | } |
alanb@368 | 243 | |
ohair@286 | 244 | public void config(final String message, final Throwable thrown) { |
ohair@286 | 245 | if (!this.logger.isLoggable(Level.CONFIG)) { |
ohair@286 | 246 | return; |
ohair@286 | 247 | } |
ohair@286 | 248 | logger.logp(Level.CONFIG, componentClassName, getCallerMethodName(), message, thrown); |
ohair@286 | 249 | } |
ohair@286 | 250 | |
ohair@286 | 251 | public void warning(final String message) { |
ohair@286 | 252 | if (!this.logger.isLoggable(Level.WARNING)) { |
ohair@286 | 253 | return; |
ohair@286 | 254 | } |
ohair@286 | 255 | logger.logp(Level.WARNING, componentClassName, getCallerMethodName(), message); |
ohair@286 | 256 | } |
ohair@286 | 257 | |
alanb@368 | 258 | public void warning(final String message, Object[] params) { |
alanb@368 | 259 | if (!this.logger.isLoggable(Level.WARNING)) { |
alanb@368 | 260 | return; |
alanb@368 | 261 | } |
alanb@368 | 262 | logger.logp(Level.WARNING, componentClassName, getCallerMethodName(), message, params); |
alanb@368 | 263 | } |
alanb@368 | 264 | |
ohair@286 | 265 | public void warning(final String message, final Throwable thrown) { |
ohair@286 | 266 | if (!this.logger.isLoggable(Level.WARNING)) { |
ohair@286 | 267 | return; |
ohair@286 | 268 | } |
ohair@286 | 269 | logger.logp(Level.WARNING, componentClassName, getCallerMethodName(), message, thrown); |
ohair@286 | 270 | } |
ohair@286 | 271 | |
ohair@286 | 272 | public void severe(final String message) { |
ohair@286 | 273 | if (!this.logger.isLoggable(Level.SEVERE)) { |
ohair@286 | 274 | return; |
ohair@286 | 275 | } |
ohair@286 | 276 | logger.logp(Level.SEVERE, componentClassName, getCallerMethodName(), message); |
ohair@286 | 277 | } |
ohair@286 | 278 | |
alanb@368 | 279 | public void severe(final String message, Object[] params) { |
alanb@368 | 280 | if (!this.logger.isLoggable(Level.SEVERE)) { |
alanb@368 | 281 | return; |
alanb@368 | 282 | } |
alanb@368 | 283 | logger.logp(Level.SEVERE, componentClassName, getCallerMethodName(), message, params); |
alanb@368 | 284 | } |
alanb@368 | 285 | |
ohair@286 | 286 | public void severe(final String message, final Throwable thrown) { |
ohair@286 | 287 | if (!this.logger.isLoggable(Level.SEVERE)) { |
ohair@286 | 288 | return; |
ohair@286 | 289 | } |
ohair@286 | 290 | logger.logp(Level.SEVERE, componentClassName, getCallerMethodName(), message, thrown); |
ohair@286 | 291 | } |
ohair@286 | 292 | |
ohair@286 | 293 | public boolean isMethodCallLoggable() { |
ohair@286 | 294 | return this.logger.isLoggable(METHOD_CALL_LEVEL_VALUE); |
ohair@286 | 295 | } |
ohair@286 | 296 | |
ohair@286 | 297 | public boolean isLoggable(final Level level) { |
ohair@286 | 298 | return this.logger.isLoggable(level); |
ohair@286 | 299 | } |
ohair@286 | 300 | |
ohair@286 | 301 | public void setLevel(final Level level) { |
ohair@286 | 302 | this.logger.setLevel(level); |
ohair@286 | 303 | } |
ohair@286 | 304 | |
ohair@286 | 305 | public void entering() { |
ohair@286 | 306 | if (!this.logger.isLoggable(METHOD_CALL_LEVEL_VALUE)) { |
ohair@286 | 307 | return; |
ohair@286 | 308 | } |
ohair@286 | 309 | |
ohair@286 | 310 | logger.entering(componentClassName, getCallerMethodName()); |
ohair@286 | 311 | } |
ohair@286 | 312 | |
ohair@286 | 313 | public void entering(final Object... parameters) { |
ohair@286 | 314 | if (!this.logger.isLoggable(METHOD_CALL_LEVEL_VALUE)) { |
ohair@286 | 315 | return; |
ohair@286 | 316 | } |
ohair@286 | 317 | |
ohair@286 | 318 | logger.entering(componentClassName, getCallerMethodName(), parameters); |
ohair@286 | 319 | } |
ohair@286 | 320 | |
ohair@286 | 321 | public void exiting() { |
ohair@286 | 322 | if (!this.logger.isLoggable(METHOD_CALL_LEVEL_VALUE)) { |
ohair@286 | 323 | return; |
ohair@286 | 324 | } |
ohair@286 | 325 | logger.exiting(componentClassName, getCallerMethodName()); |
ohair@286 | 326 | } |
ohair@286 | 327 | |
ohair@286 | 328 | public void exiting(final Object result) { |
ohair@286 | 329 | if (!this.logger.isLoggable(METHOD_CALL_LEVEL_VALUE)) { |
ohair@286 | 330 | return; |
ohair@286 | 331 | } |
ohair@286 | 332 | logger.exiting(componentClassName, getCallerMethodName(), result); |
ohair@286 | 333 | } |
ohair@286 | 334 | |
ohair@286 | 335 | /** |
ohair@286 | 336 | * Method logs {@code exception}'s message as a {@code SEVERE} logging level |
ohair@286 | 337 | * message. |
ohair@286 | 338 | * <p/> |
ohair@286 | 339 | * If {@code cause} parameter is not {@code null}, it is logged as well and |
ohair@286 | 340 | * {@code exception} original cause is initialized with instance referenced |
ohair@286 | 341 | * by {@code cause} parameter. |
ohair@286 | 342 | * |
ohair@286 | 343 | * @param exception exception whose message should be logged. Must not be |
ohair@286 | 344 | * {@code null}. |
ohair@286 | 345 | * @param cause initial cause of the exception that should be logged as well |
ohair@286 | 346 | * and set as {@code exception}'s original cause. May be {@code null}. |
ohair@286 | 347 | * @return the same exception instance that was passed in as the {@code exception} |
ohair@286 | 348 | * parameter. |
ohair@286 | 349 | */ |
ohair@286 | 350 | public <T extends Throwable> T logSevereException(final T exception, final Throwable cause) { |
ohair@286 | 351 | if (this.logger.isLoggable(Level.SEVERE)) { |
ohair@286 | 352 | if (cause == null) { |
ohair@286 | 353 | logger.logp(Level.SEVERE, componentClassName, getCallerMethodName(), exception.getMessage()); |
ohair@286 | 354 | } else { |
ohair@286 | 355 | exception.initCause(cause); |
ohair@286 | 356 | logger.logp(Level.SEVERE, componentClassName, getCallerMethodName(), exception.getMessage(), cause); |
ohair@286 | 357 | } |
ohair@286 | 358 | } |
ohair@286 | 359 | |
ohair@286 | 360 | return exception; |
ohair@286 | 361 | } |
ohair@286 | 362 | |
ohair@286 | 363 | /** |
ohair@286 | 364 | * Method logs {@code exception}'s message as a {@code SEVERE} logging level |
ohair@286 | 365 | * message. |
ohair@286 | 366 | * <p/> |
ohair@286 | 367 | * If {@code logCause} parameter is {@code true}, {@code exception}'s original |
ohair@286 | 368 | * cause is logged as well (if exists). This may be used in cases when |
ohair@286 | 369 | * {@code exception}'s class provides constructor to initialize the original |
ohair@286 | 370 | * cause. In such case you do not need to use |
ohair@286 | 371 | * {@link #logSevereException(Throwable, Throwable)} |
ohair@286 | 372 | * method version but you might still want to log the original cause as well. |
ohair@286 | 373 | * |
ohair@286 | 374 | * @param exception exception whose message should be logged. Must not be |
ohair@286 | 375 | * {@code null}. |
ohair@286 | 376 | * @param logCause deterimnes whether initial cause of the exception should |
ohair@286 | 377 | * be logged as well |
ohair@286 | 378 | * @return the same exception instance that was passed in as the {@code exception} |
ohair@286 | 379 | * parameter. |
ohair@286 | 380 | */ |
ohair@286 | 381 | public <T extends Throwable> T logSevereException(final T exception, final boolean logCause) { |
ohair@286 | 382 | if (this.logger.isLoggable(Level.SEVERE)) { |
ohair@286 | 383 | if (logCause && exception.getCause() != null) { |
ohair@286 | 384 | logger.logp(Level.SEVERE, componentClassName, getCallerMethodName(), exception.getMessage(), exception.getCause()); |
ohair@286 | 385 | } else { |
ohair@286 | 386 | logger.logp(Level.SEVERE, componentClassName, getCallerMethodName(), exception.getMessage()); |
ohair@286 | 387 | } |
ohair@286 | 388 | } |
ohair@286 | 389 | |
ohair@286 | 390 | return exception; |
ohair@286 | 391 | } |
ohair@286 | 392 | |
ohair@286 | 393 | /** |
ohair@286 | 394 | * Same as {@link #logSevereException(Throwable, boolean) logSevereException(exception, true)}. |
ohair@286 | 395 | */ |
ohair@286 | 396 | public <T extends Throwable> T logSevereException(final T exception) { |
ohair@286 | 397 | if (this.logger.isLoggable(Level.SEVERE)) { |
ohair@286 | 398 | if (exception.getCause() == null) { |
ohair@286 | 399 | logger.logp(Level.SEVERE, componentClassName, getCallerMethodName(), exception.getMessage()); |
ohair@286 | 400 | } else { |
ohair@286 | 401 | logger.logp(Level.SEVERE, componentClassName, getCallerMethodName(), exception.getMessage(), exception.getCause()); |
ohair@286 | 402 | } |
ohair@286 | 403 | } |
ohair@286 | 404 | |
ohair@286 | 405 | return exception; |
ohair@286 | 406 | } |
ohair@286 | 407 | |
ohair@286 | 408 | /** |
ohair@286 | 409 | * Method logs {@code exception}'s message at the logging level specified by the |
ohair@286 | 410 | * {@code level} argument. |
ohair@286 | 411 | * <p/> |
ohair@286 | 412 | * If {@code cause} parameter is not {@code null}, it is logged as well and |
ohair@286 | 413 | * {@code exception} original cause is initialized with instance referenced |
ohair@286 | 414 | * by {@code cause} parameter. |
ohair@286 | 415 | * |
ohair@286 | 416 | * @param exception exception whose message should be logged. Must not be |
ohair@286 | 417 | * {@code null}. |
ohair@286 | 418 | * @param cause initial cause of the exception that should be logged as well |
ohair@286 | 419 | * and set as {@code exception}'s original cause. May be {@code null}. |
ohair@286 | 420 | * @param level loging level which should be used for logging |
ohair@286 | 421 | * @return the same exception instance that was passed in as the {@code exception} |
ohair@286 | 422 | * parameter. |
ohair@286 | 423 | */ |
ohair@286 | 424 | public <T extends Throwable> T logException(final T exception, final Throwable cause, final Level level) { |
ohair@286 | 425 | if (this.logger.isLoggable(level)) { |
ohair@286 | 426 | if (cause == null) { |
ohair@286 | 427 | logger.logp(level, componentClassName, getCallerMethodName(), exception.getMessage()); |
ohair@286 | 428 | } else { |
ohair@286 | 429 | exception.initCause(cause); |
ohair@286 | 430 | logger.logp(level, componentClassName, getCallerMethodName(), exception.getMessage(), cause); |
ohair@286 | 431 | } |
ohair@286 | 432 | } |
ohair@286 | 433 | |
ohair@286 | 434 | return exception; |
ohair@286 | 435 | } |
ohair@286 | 436 | |
ohair@286 | 437 | /** |
ohair@286 | 438 | * Method logs {@code exception}'s message at the logging level specified by the |
ohair@286 | 439 | * {@code level} argument. |
ohair@286 | 440 | * <p/> |
ohair@286 | 441 | * If {@code logCause} parameter is {@code true}, {@code exception}'s original |
ohair@286 | 442 | * cause is logged as well (if exists). This may be used in cases when |
ohair@286 | 443 | * {@code exception}'s class provides constructor to initialize the original |
ohair@286 | 444 | * cause. In such case you do not need to use |
ohair@286 | 445 | * {@link #logException(Throwable, Throwable, Level) logException(exception, cause, level)} |
ohair@286 | 446 | * method version but you might still want to log the original cause as well. |
ohair@286 | 447 | * |
ohair@286 | 448 | * @param exception exception whose message should be logged. Must not be |
ohair@286 | 449 | * {@code null}. |
ohair@286 | 450 | * @param logCause deterimnes whether initial cause of the exception should |
ohair@286 | 451 | * be logged as well |
ohair@286 | 452 | * @param level loging level which should be used for logging |
ohair@286 | 453 | * @return the same exception instance that was passed in as the {@code exception} |
ohair@286 | 454 | * parameter. |
ohair@286 | 455 | */ |
ohair@286 | 456 | public <T extends Throwable> T logException(final T exception, final boolean logCause, final Level level) { |
ohair@286 | 457 | if (this.logger.isLoggable(level)) { |
ohair@286 | 458 | if (logCause && exception.getCause() != null) { |
ohair@286 | 459 | logger.logp(level, componentClassName, getCallerMethodName(), exception.getMessage(), exception.getCause()); |
ohair@286 | 460 | } else { |
ohair@286 | 461 | logger.logp(level, componentClassName, getCallerMethodName(), exception.getMessage()); |
ohair@286 | 462 | } |
ohair@286 | 463 | } |
ohair@286 | 464 | |
ohair@286 | 465 | return exception; |
ohair@286 | 466 | } |
ohair@286 | 467 | |
ohair@286 | 468 | /** |
ohair@286 | 469 | * Same as {@link #logException(Throwable, Throwable, Level) |
ohair@286 | 470 | * logException(exception, true, level)}. |
ohair@286 | 471 | */ |
ohair@286 | 472 | public <T extends Throwable> T logException(final T exception, final Level level) { |
ohair@286 | 473 | if (this.logger.isLoggable(level)) { |
ohair@286 | 474 | if (exception.getCause() == null) { |
ohair@286 | 475 | logger.logp(level, componentClassName, getCallerMethodName(), exception.getMessage()); |
ohair@286 | 476 | } else { |
ohair@286 | 477 | logger.logp(level, componentClassName, getCallerMethodName(), exception.getMessage(), exception.getCause()); |
ohair@286 | 478 | } |
ohair@286 | 479 | } |
ohair@286 | 480 | |
ohair@286 | 481 | return exception; |
ohair@286 | 482 | } |
ohair@286 | 483 | |
ohair@286 | 484 | /** |
ohair@286 | 485 | * Function returns the name of the caller method for the method executing this |
ohair@286 | 486 | * function. |
ohair@286 | 487 | * |
ohair@286 | 488 | * @return caller method name from the call stack of the current {@link Thread}. |
ohair@286 | 489 | */ |
ohair@286 | 490 | private static String getCallerMethodName() { |
ohair@286 | 491 | return getStackMethodName(5); |
ohair@286 | 492 | } |
ohair@286 | 493 | |
ohair@286 | 494 | /** |
ohair@286 | 495 | * Method returns the name of the method that is on the {@code methodIndexInStack} |
ohair@286 | 496 | * position in the call stack of the current {@link Thread}. |
ohair@286 | 497 | * |
ohair@286 | 498 | * @param methodIndexInStack index to the call stack to get the method name for. |
ohair@286 | 499 | * @return the name of the method that is on the {@code methodIndexInStack} |
ohair@286 | 500 | * position in the call stack of the current {@link Thread}. |
ohair@286 | 501 | */ |
ohair@286 | 502 | private static String getStackMethodName(final int methodIndexInStack) { |
ohair@286 | 503 | final String methodName; |
ohair@286 | 504 | |
ohair@286 | 505 | final StackTraceElement[] stack = Thread.currentThread().getStackTrace(); |
ohair@286 | 506 | if (stack.length > methodIndexInStack + 1) { |
ohair@286 | 507 | methodName = stack[methodIndexInStack].getMethodName(); |
ohair@286 | 508 | } else { |
ohair@286 | 509 | methodName = "UNKNOWN METHOD"; |
ohair@286 | 510 | } |
ohair@286 | 511 | |
ohair@286 | 512 | return methodName; |
ohair@286 | 513 | } |
ohair@286 | 514 | |
ohair@286 | 515 | } |