|
10 | 10 | import org.slf4j.LoggerFactory; |
11 | 11 | import org.slf4j.helpers.NOPLogger; |
12 | 12 |
|
13 | | -import java.util.Arrays; |
14 | 13 | import java.util.Objects; |
15 | 14 | import java.util.function.Supplier; |
16 | 15 |
|
17 | 16 | import static com.azure.core.implementation.logging.LoggingUtils.removeNewLinesFromLogMessage; |
18 | 17 |
|
| 18 | +import static com.azure.core.implementation.logging.LoggingUtils.doesArgsHaveThrowable; |
| 19 | +import static com.azure.core.implementation.logging.LoggingUtils.removeThrowable; |
| 20 | + |
19 | 21 | /** |
20 | 22 | * This is a fluent logger helper class that wraps a pluggable {@link Logger}. |
21 | 23 | * |
@@ -345,11 +347,10 @@ public <T extends Throwable> T logThowableAsWarning(T throwable) { |
345 | 347 | */ |
346 | 348 | public <T extends Throwable> T logThrowableAsWarning(T throwable) { |
347 | 349 | Objects.requireNonNull(throwable, "'throwable' cannot be null."); |
348 | | - if (!logger.isWarnEnabled()) { |
349 | | - return throwable; |
| 350 | + if (logger.isWarnEnabled()) { |
| 351 | + performLogging(LogLevel.WARNING, true, throwable.getMessage(), throwable); |
350 | 352 | } |
351 | 353 |
|
352 | | - performLogging(LogLevel.WARNING, true, throwable.getMessage(), throwable); |
353 | 354 | return throwable; |
354 | 355 | } |
355 | 356 |
|
@@ -453,9 +454,9 @@ private void performLogging(LogLevel logLevel, boolean isExceptionLogging, Strin |
453 | 454 | * @param args Arguments for the message, if an exception is being logged last argument is the throwable. |
454 | 455 | */ |
455 | 456 | private void performDeferredLogging(LogLevel logLevel, Supplier<String> messageSupplier, Throwable throwable) { |
| 457 | + String message = removeNewLinesFromLogMessage(messageSupplier.get()); |
456 | 458 | String throwableMessage = (throwable != null) ? throwable.getMessage() : ""; |
457 | | - String message = messageSupplier.get(); |
458 | | - message = removeNewLinesFromLogMessage(message); |
| 459 | + |
459 | 460 | switch (logLevel) { |
460 | 461 | case VERBOSE: |
461 | 462 | if (throwable != null) { |
@@ -489,7 +490,6 @@ private void performDeferredLogging(LogLevel logLevel, Supplier<String> messageS |
489 | 490 | * @param args The arguments passed to evaluate suppliers in args. |
490 | 491 | * @return Return the argument with evaluated supplier |
491 | 492 | */ |
492 | | - |
493 | 493 | Object[] evaluateSupplierArgument(Object[] args) { |
494 | 494 | if (isSupplierLogging(args)) { |
495 | 495 | args[0] = ((Supplier<?>) args[0]).get(); |
@@ -530,28 +530,89 @@ public boolean canLogAtLevel(LogLevel logLevel) { |
530 | 530 | } |
531 | 531 | } |
532 | 532 |
|
533 | | - /* |
534 | | - * Determines if the arguments contains a throwable that would be logged, SLF4J logs a throwable if it is the last |
535 | | - * element in the argument list. |
| 533 | + /** |
| 534 | + * Creates {@link LoggingEventBuilder} for {@code error} log level that can be |
| 535 | + * used to enrich log with additional context. |
| 536 | + * <p><strong>Code samples</strong></p> |
| 537 | + * |
| 538 | + * <p>Logging with context at error level.</p> |
536 | 539 | * |
537 | | - * @param args The arguments passed to format the log message. |
538 | | - * @return True if the last element is a throwable, false otherwise. |
| 540 | + * <!-- src_embed com.azure.core.util.logging.clientlogger.atverbose.addKeyValue#primitive --> |
| 541 | + * <pre> |
| 542 | + * logger.atVerbose() |
| 543 | + * .addKeyValue("key", 1L) |
| 544 | + * .log(() -> String.format("Param 1: %s, Param 2: %s, Param 3: %s", "param1", "param2", "param3")); |
| 545 | + * </pre> |
| 546 | + * <!-- end com.azure.core.util.logging.clientlogger.atverbose.addKeyValue#primitive --> |
| 547 | + * |
| 548 | + * @return instance of {@link LoggingEventBuilder} or no-op if error logging is disabled. |
539 | 549 | */ |
540 | | - private boolean doesArgsHaveThrowable(Object... args) { |
541 | | - if (args.length == 0) { |
542 | | - return false; |
543 | | - } |
| 550 | + public LoggingEventBuilder atError() { |
| 551 | + return LoggingEventBuilder.create(logger, LogLevel.ERROR, canLogAtLevel(LogLevel.ERROR)); |
| 552 | + } |
| 553 | + |
| 554 | + /** |
| 555 | + * Creates {@link LoggingEventBuilder} for {@code warning} log level that can be |
| 556 | + * used to enrich log with additional context. |
| 557 | +
|
| 558 | + * <p><strong>Code samples</strong></p> |
| 559 | + * |
| 560 | + * <p>Logging with context at warning level.</p> |
| 561 | + * |
| 562 | + * <!-- src_embed com.azure.core.util.logging.clientlogger.atWarning --> |
| 563 | + * <pre> |
| 564 | + * logger.atWarning() |
| 565 | + * .addKeyValue("key", "value") |
| 566 | + * .log("A formattable message. Hello, {}", name, exception); |
| 567 | + * </pre> |
| 568 | + * <!-- end com.azure.core.util.logging.clientlogger.atWarning --> |
| 569 | + * |
| 570 | + * @return instance of {@link LoggingEventBuilder} or no-op if warn logging is disabled. |
| 571 | + */ |
| 572 | + public LoggingEventBuilder atWarning() { |
| 573 | + return LoggingEventBuilder.create(logger, LogLevel.WARNING, canLogAtLevel(LogLevel.WARNING)); |
| 574 | + } |
544 | 575 |
|
545 | | - return args[args.length - 1] instanceof Throwable; |
| 576 | + /** |
| 577 | + * Creates {@link LoggingEventBuilder} for {@code info} log level that can be |
| 578 | + * used to enrich log with additional context. |
| 579 | + * |
| 580 | + * <p><strong>Code samples</strong></p> |
| 581 | + * |
| 582 | + * <p>Logging with context at info level.</p> |
| 583 | + * |
| 584 | + * <!-- src_embed com.azure.core.util.logging.clientlogger.atInfo --> |
| 585 | + * <pre> |
| 586 | + * logger.atInfo() |
| 587 | + * .addKeyValue("key", "value") |
| 588 | + * .log("A formattable message. Hello, {}", name); |
| 589 | + * </pre> |
| 590 | + * <!-- end com.azure.core.util.logging.clientlogger.atInfo --> |
| 591 | + * |
| 592 | + * @return instance of {@link LoggingEventBuilder} or no-op if info logging is disabled. |
| 593 | + */ |
| 594 | + public LoggingEventBuilder atInfo() { |
| 595 | + return LoggingEventBuilder.create(logger, LogLevel.INFORMATIONAL, canLogAtLevel(LogLevel.INFORMATIONAL)); |
546 | 596 | } |
547 | 597 |
|
548 | | - /* |
549 | | - * Removes the last element from the arguments as it is a throwable. |
| 598 | + /** |
| 599 | + * Creates {@link LoggingEventBuilder} for {@code verbose} log level that can be |
| 600 | + * used to enrich log with additional context. |
| 601 | + * <p><strong>Code samples</strong></p> |
| 602 | + * |
| 603 | + * <p>Logging with context at verbose level.</p> |
| 604 | + * |
| 605 | + * <!-- src_embed com.azure.core.util.logging.clientlogger.atverbose.addKeyValue#primitive --> |
| 606 | + * <pre> |
| 607 | + * logger.atVerbose() |
| 608 | + * .addKeyValue("key", 1L) |
| 609 | + * .log(() -> String.format("Param 1: %s, Param 2: %s, Param 3: %s", "param1", "param2", "param3")); |
| 610 | + * </pre> |
| 611 | + * <!-- end com.azure.core.util.logging.clientlogger.atverbose.addKeyValue#primitive --> |
550 | 612 | * |
551 | | - * @param args The arguments passed to format the log message. |
552 | | - * @return The arguments with the last element removed. |
| 613 | + * @return instance of {@link LoggingEventBuilder} or no-op if verbose logging is disabled. |
553 | 614 | */ |
554 | | - private Object[] removeThrowable(Object... args) { |
555 | | - return Arrays.copyOf(args, args.length - 1); |
| 615 | + public LoggingEventBuilder atVerbose() { |
| 616 | + return LoggingEventBuilder.create(logger, LogLevel.VERBOSE, canLogAtLevel(LogLevel.VERBOSE)); |
556 | 617 | } |
557 | 618 | } |
0 commit comments