1 package org.apache.commons.jcs3.log; 2 3 import java.util.function.Supplier; 4 import java.util.logging.Level; 5 import java.util.logging.Logger; 6 7 /* 8 * Licensed to the Apache Software Foundation (ASF) under one or more 9 * contributor license agreements. See the NOTICE file distributed with 10 * this work for additional information regarding copyright ownership. 11 * The ASF licenses this file to You under the Apache license, Version 2.0 12 * (the "License"); you may not use this file except in compliance with 13 * the License. You may obtain a copy of the License at 14 * 15 * http://www.apache.org/licenses/LICENSE-2.0 16 * 17 * Unless required by applicable law or agreed to in writing, software 18 * distributed under the License is distributed on an "AS IS" BASIS, 19 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 20 * See the license for the specific language governing permissions and 21 * limitations under the license. 22 */ 23 24 /** 25 * This is a wrapper around the <code>java.util.logging.Logger</code> implementing our own 26 * <code>Log</code> interface. 27 * <p> 28 * This is the mapping of the log levels 29 * </p> 30 * <pre> 31 * Java Level Log Level 32 * SEVERE FATAL 33 * SEVERE ERROR 34 * WARNING WARN 35 * INFO INFO 36 * FINE DEBUG 37 * FINER TRACE 38 * </pre> 39 */ 40 public class JulLogAdapter implements Log 41 { 42 private final Logger logger; 43 44 /** 45 * Construct a JUL Logger wrapper 46 * 47 * @param logger the JUL Logger 48 */ 49 public JulLogAdapter(final Logger logger) 50 { 51 this.logger = logger; 52 } 53 54 private void log(final Level level, final String message) 55 { 56 if (logger.isLoggable(level)) 57 { 58 logger.logp(level, logger.getName(), "", message); 59 } 60 } 61 62 private void log(final Level level, final Object message) 63 { 64 if (logger.isLoggable(level)) 65 { 66 if (message instanceof Throwable) 67 { 68 logger.logp(level, logger.getName(), "", "Exception:", (Throwable) message); 69 } 70 else 71 { 72 logger.logp(level, logger.getName(), "", 73 message == null ? null : message.toString()); 74 } 75 } 76 } 77 78 private void log(final Level level, final String message, final Throwable t) 79 { 80 if (logger.isLoggable(level)) 81 { 82 logger.logp(level, logger.getName(), "", message, t); 83 } 84 } 85 86 private void log(final Level level, final String message, final Object... params) 87 { 88 if (logger.isLoggable(level)) 89 { 90 final MessageFormatter formatter = new MessageFormatter(message, params); 91 if (formatter.hasThrowable()) 92 { 93 logger.logp(level, logger.getName(), "", 94 formatter.getFormattedMessage(), formatter.getThrowable()); 95 } 96 else 97 { 98 logger.logp(level, logger.getName(), "", 99 formatter.getFormattedMessage()); 100 } 101 } 102 } 103 104 private void log(final Level level, final String message, final Supplier<?>... paramSuppliers) 105 { 106 if (logger.isLoggable(level)) 107 { 108 final MessageFormatter formatter = new MessageFormatter(message, paramSuppliers); 109 if (formatter.hasThrowable()) 110 { 111 logger.logp(level, logger.getName(), "", 112 formatter.getFormattedMessage(), formatter.getThrowable()); 113 } 114 else 115 { 116 logger.logp(level, logger.getName(), "", 117 formatter.getFormattedMessage()); 118 } 119 } 120 } 121 122 /** 123 * Logs a message object with the DEBUG level. 124 * 125 * @param message the message string to log. 126 */ 127 @Override 128 public void debug(final String message) 129 { 130 log(Level.FINE, message); 131 } 132 133 /** 134 * Logs a message object with the DEBUG level. 135 * 136 * @param message the message object to log. 137 */ 138 @Override 139 public void debug(final Object message) 140 { 141 log(Level.FINE, message); 142 } 143 144 /** 145 * Logs a message with parameters at the DEBUG level. 146 * 147 * @param message the message to log; the format depends on the message factory. 148 * @param params parameters to the message. 149 */ 150 @Override 151 public void debug(final String message, final Object... params) 152 { 153 log(Level.FINE, message, params); 154 } 155 156 /** 157 * Logs a message with parameters which are only to be constructed if the 158 * logging level is the DEBUG level. 159 * 160 * @param message the message to log; the format depends on the message factory. 161 * @param paramSuppliers An array of functions, which when called, 162 * produce the desired log message parameters. 163 */ 164 @Override 165 public void debug(final String message, final Supplier<?>... paramSuppliers) 166 { 167 log(Level.FINE, message, paramSuppliers); 168 } 169 170 /** 171 * Logs a message at the DEBUG level including the stack trace of the {@link Throwable} 172 * <code>t</code> passed as parameter. 173 * 174 * @param message the message to log. 175 * @param t the exception to log, including its stack trace. 176 */ 177 @Override 178 public void debug(final String message, final Throwable t) 179 { 180 log(Level.FINE, message, t); 181 } 182 183 /** 184 * Logs a message object with the ERROR level. 185 * 186 * @param message the message string to log. 187 */ 188 @Override 189 public void error(final String message) 190 { 191 log(Level.SEVERE, message); 192 } 193 194 /** 195 * Logs a message object with the ERROR level. 196 * 197 * @param message the message object to log. 198 */ 199 @Override 200 public void error(final Object message) 201 { 202 log(Level.SEVERE, message); 203 } 204 205 /** 206 * Logs a message with parameters at the ERROR level. 207 * 208 * @param message the message to log; the format depends on the message factory. 209 * @param params parameters to the message. 210 */ 211 @Override 212 public void error(final String message, final Object... params) 213 { 214 log(Level.SEVERE, message, params); 215 } 216 217 /** 218 * Logs a message with parameters which are only to be constructed if the 219 * logging level is the ERROR level. 220 * 221 * @param message the message to log; the format depends on the message factory. 222 * @param paramSuppliers An array of functions, which when called, produce 223 * the desired log message parameters. 224 * @since 2.4 225 */ 226 @Override 227 public void error(final String message, final Supplier<?>... paramSuppliers) 228 { 229 log(Level.SEVERE, message, paramSuppliers); 230 } 231 232 /** 233 * Logs a message at the ERROR level including the stack trace of the {@link Throwable} 234 * <code>t</code> passed as parameter. 235 * 236 * @param message the message object to log. 237 * @param t the exception to log, including its stack trace. 238 */ 239 @Override 240 public void error(final String message, final Throwable t) 241 { 242 log(Level.SEVERE, message, t); 243 } 244 245 /** 246 * Logs a message object with the FATAL level. 247 * 248 * @param message the message string to log. 249 */ 250 @Override 251 public void fatal(final String message) 252 { 253 log(Level.SEVERE, message); 254 } 255 256 /** 257 * Logs a message object with the FATAL level. 258 * 259 * @param message the message object to log. 260 */ 261 @Override 262 public void fatal(final Object message) 263 { 264 log(Level.SEVERE, message); 265 } 266 267 /** 268 * Logs a message with parameters at the FATAL level. 269 * 270 * @param message the message to log; the format depends on the message factory. 271 * @param params parameters to the message. 272 */ 273 @Override 274 public void fatal(final String message, final Object... params) 275 { 276 log(Level.SEVERE, message, params); 277 } 278 279 /** 280 * Logs a message with parameters which are only to be constructed if the 281 * logging level is the FATAL level. 282 * 283 * @param message the message to log; the format depends on the message factory. 284 * @param paramSuppliers An array of functions, which when called, produce the 285 * desired log message parameters. 286 */ 287 @Override 288 public void fatal(final String message, final Supplier<?>... paramSuppliers) 289 { 290 log(Level.SEVERE, message, paramSuppliers); 291 } 292 293 /** 294 * Logs a message at the FATAL level including the stack trace of the {@link Throwable} 295 * <code>t</code> passed as parameter. 296 * 297 * @param message the message object to log. 298 * @param t the exception to log, including its stack trace. 299 */ 300 @Override 301 public void fatal(final String message, final Throwable t) 302 { 303 log(Level.SEVERE, message, t); 304 } 305 306 /** 307 * Gets the logger name. 308 * 309 * @return the logger name. 310 */ 311 @Override 312 public String getName() 313 { 314 return logger.getName(); 315 } 316 317 /** 318 * Logs a message object with the INFO level. 319 * 320 * @param message the message string to log. 321 */ 322 @Override 323 public void info(final String message) 324 { 325 log(Level.INFO, message); 326 } 327 328 /** 329 * Logs a message object with the INFO level. 330 * 331 * @param message the message object to log. 332 */ 333 @Override 334 public void info(final Object message) 335 { 336 log(Level.INFO, message); 337 } 338 339 /** 340 * Logs a message with parameters at the INFO level. 341 * 342 * @param message the message to log; the format depends on the message factory. 343 * @param params parameters to the message. 344 */ 345 @Override 346 public void info(final String message, final Object... params) 347 { 348 log(Level.INFO, message, params); 349 } 350 351 /** 352 * Logs a message with parameters which are only to be constructed if the 353 * logging level is the INFO level. 354 * 355 * @param message the message to log; the format depends on the message factory. 356 * @param paramSuppliers An array of functions, which when called, produce 357 * the desired log message parameters. 358 */ 359 @Override 360 public void info(final String message, final Supplier<?>... paramSuppliers) 361 { 362 log(Level.INFO, message, paramSuppliers); 363 } 364 365 /** 366 * Logs a message at the INFO level including the stack trace of the {@link Throwable} 367 * <code>t</code> passed as parameter. 368 * 369 * @param message the message object to log. 370 * @param t the exception to log, including its stack trace. 371 */ 372 @Override 373 public void info(final String message, final Throwable t) 374 { 375 log(Level.INFO, message, t); 376 } 377 378 /** 379 * Checks whether this Logger is enabled for the DEBUG Level. 380 * 381 * @return boolean - {@code true} if this Logger is enabled for level DEBUG, {@code false} 382 * otherwise. 383 */ 384 @Override 385 public boolean isDebugEnabled() 386 { 387 return logger.isLoggable(Level.FINE); 388 } 389 390 /** 391 * Checks whether this Logger is enabled for the ERROR Level. 392 * 393 * @return boolean - {@code true} if this Logger is enabled for level ERROR, {@code false} 394 * otherwise. 395 */ 396 @Override 397 public boolean isErrorEnabled() 398 { 399 return logger.isLoggable(Level.SEVERE); 400 } 401 402 /** 403 * Checks whether this Logger is enabled for the FATAL Level. 404 * 405 * @return boolean - {@code true} if this Logger is enabled for level FATAL, {@code false} 406 * otherwise. 407 */ 408 @Override 409 public boolean isFatalEnabled() 410 { 411 return logger.isLoggable(Level.SEVERE); 412 } 413 414 /** 415 * Checks whether this Logger is enabled for the INFO Level. 416 * 417 * @return boolean - {@code true} if this Logger is enabled for level INFO, {@code false} 418 * otherwise. 419 */ 420 @Override 421 public boolean isInfoEnabled() 422 { 423 return logger.isLoggable(Level.INFO); 424 } 425 426 /** 427 * Checks whether this Logger is enabled for the TRACE level. 428 * 429 * @return boolean - {@code true} if this Logger is enabled for level TRACE, {@code false} 430 * otherwise. 431 */ 432 @Override 433 public boolean isTraceEnabled() 434 { 435 return logger.isLoggable(Level.FINER); 436 } 437 438 /** 439 * Checks whether this Logger is enabled for the WARN Level. 440 * 441 * @return boolean - {@code true} if this Logger is enabled for level WARN, {@code false} 442 * otherwise. 443 */ 444 @Override 445 public boolean isWarnEnabled() 446 { 447 return logger.isLoggable(Level.WARNING); 448 } 449 450 /** 451 * Logs a message object with the TRACE level. 452 * 453 * @param message the message string to log. 454 */ 455 @Override 456 public void trace(final String message) 457 { 458 log(Level.FINER, message); 459 } 460 461 /** 462 * Logs a message object with the TRACE level. 463 * 464 * @param message the message object to log. 465 */ 466 @Override 467 public void trace(final Object message) 468 { 469 log(Level.FINER, message); 470 } 471 472 /** 473 * Logs a message with parameters at the TRACE level. 474 * 475 * @param message the message to log; the format depends on the message factory. 476 * @param params parameters to the message. 477 */ 478 @Override 479 public void trace(final String message, final Object... params) 480 { 481 log(Level.FINER, message, params); 482 } 483 484 /** 485 * Logs a message with parameters which are only to be constructed if the 486 * logging level is the TRACE level. 487 * 488 * @param message the message to log; the format depends on the message factory. 489 * @param paramSuppliers An array of functions, which when called, produce 490 * the desired log message parameters. 491 */ 492 @Override 493 public void trace(final String message, final Supplier<?>... paramSuppliers) 494 { 495 log(Level.FINER, message, paramSuppliers); 496 } 497 498 /** 499 * Logs a message at the TRACE level including the stack trace of the {@link Throwable} 500 * <code>t</code> passed as parameter. 501 * 502 * @param message the message object to log. 503 * @param t the exception to log, including its stack trace. 504 * @see #debug(String) 505 */ 506 @Override 507 public void trace(final String message, final Throwable t) 508 { 509 log(Level.FINER, message, t); 510 } 511 512 /** 513 * Logs a message object with the WARN level. 514 * 515 * @param message the message string to log. 516 */ 517 @Override 518 public void warn(final String message) 519 { 520 log(Level.WARNING, message); 521 } 522 523 /** 524 * Logs a message object with the WARN level. 525 * 526 * @param message the message object to log. 527 */ 528 @Override 529 public void warn(final Object message) 530 { 531 log(Level.WARNING, message); 532 } 533 534 /** 535 * Logs a message with parameters at the WARN level. 536 * 537 * @param message the message to log; the format depends on the message factory. 538 * @param params parameters to the message. 539 */ 540 @Override 541 public void warn(final String message, final Object... params) 542 { 543 log(Level.WARNING, message, params); 544 } 545 546 /** 547 * Logs a message with parameters which are only to be constructed if the 548 * logging level is the WARN level. 549 * 550 * @param message the message to log; the format depends on the message factory. 551 * @param paramSuppliers An array of functions, which when called, produce 552 * the desired log message parameters. 553 */ 554 @Override 555 public void warn(final String message, final Supplier<?>... paramSuppliers) 556 { 557 log(Level.WARNING, message, paramSuppliers); 558 } 559 560 /** 561 * Logs a message at the WARN level including the stack trace of the {@link Throwable} 562 * <code>t</code> passed as parameter. 563 * 564 * @param message the message object to log. 565 * @param t the exception to log, including its stack trace. 566 */ 567 @Override 568 public void warn(final String message, final Throwable t) 569 { 570 log(Level.WARNING, message, t); 571 } 572 }