diff --git a/pom.client.xml b/pom.client.xml index 653733fc6be04..d72795f21ee65 100644 --- a/pom.client.xml +++ b/pom.client.xml @@ -954,6 +954,7 @@ --add-opens com.azure.core/com.azure.core=ALL-UNNAMED + --add-opens com.azure.core/com.azure.core.util.logging=ALL-UNNAMED --add-opens com.azure.core/com.azure.core.util.polling=ALL-UNNAMED --add-opens com.azure.core/com.azure.core.util.serializer=ALL-UNNAMED --add-opens com.azure.core/com.azure.core.http=ALL-UNNAMED diff --git a/sdk/core/azure-core-amqp/pom.xml b/sdk/core/azure-core-amqp/pom.xml index 3080b15bfa70f..6efa0f74b82ca 100644 --- a/sdk/core/azure-core-amqp/pom.xml +++ b/sdk/core/azure-core-amqp/pom.xml @@ -95,12 +95,6 @@ 5.4.2 test - - org.slf4j - slf4j-simple - 1.7.25 - test - org.mockito mockito-core diff --git a/sdk/core/azure-core-management/pom.xml b/sdk/core/azure-core-management/pom.xml index 7281c77bfb0ed..5afbe44062eb9 100644 --- a/sdk/core/azure-core-management/pom.xml +++ b/sdk/core/azure-core-management/pom.xml @@ -88,13 +88,6 @@ test - - org.slf4j - slf4j-simple - 1.7.25 - test - - org.mockito mockito-core diff --git a/sdk/core/azure-core-test/pom.xml b/sdk/core/azure-core-test/pom.xml index 1af9eb784e272..f00a6f9305175 100644 --- a/sdk/core/azure-core-test/pom.xml +++ b/sdk/core/azure-core-test/pom.xml @@ -65,11 +65,6 @@ 5.4.2 test - - org.slf4j - slf4j-api - 1.7.28 - com.github.tomakehurst wiremock-standalone diff --git a/sdk/core/azure-core/src/main/java/com/azure/core/util/logging/ClientLogger.java b/sdk/core/azure-core/src/main/java/com/azure/core/util/logging/ClientLogger.java index f07249162cb73..556b1b745adb9 100644 --- a/sdk/core/azure-core/src/main/java/com/azure/core/util/logging/ClientLogger.java +++ b/sdk/core/azure-core/src/main/java/com/azure/core/util/logging/ClientLogger.java @@ -6,12 +6,13 @@ import com.azure.core.implementation.logging.DefaultLogger; import com.azure.core.util.Configuration; import com.azure.core.util.CoreUtils; -import java.util.Arrays; -import java.util.Objects; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.slf4j.helpers.NOPLogger; +import java.util.Arrays; +import java.util.Objects; + /** * This is a fluent logger helper class that wraps a pluggable {@link Logger}. * @@ -134,23 +135,46 @@ public void error(String format, Object... args) { /** * Logs the {@link RuntimeException} at the warning level and returns it to be thrown. + *

+ * This API covers the cases where a runtime exception type needs to be thrown and logged. If a {@link Throwable} is + * being logged use {@link #logThowableAsWarning(Throwable)} instead. * * @param runtimeException RuntimeException to be logged and returned. - * @return The passed {@code RuntimeException}. + * @return The passed {@link RuntimeException}. * @throws NullPointerException If {@code runtimeException} is {@code null}. */ public RuntimeException logExceptionAsWarning(RuntimeException runtimeException) { Objects.requireNonNull(runtimeException, "'runtimeException' cannot be null."); + + return logThowableAsWarning(runtimeException); + } + + /** + * Logs the {@link Throwable} at the warning level and returns it to be thrown. + *

+ * This API covers the cases where a checked exception type needs to be thrown and logged. If a {@link + * RuntimeException} is being logged use {@link #logExceptionAsWarning(RuntimeException)} instead. + * + * @param throwable Throwable to be logged and returned. + * @param Type of the Throwable being logged. + * @return The passed {@link Throwable}. + * @throws NullPointerException If {@code throwable} is {@code null}. + */ + public T logThowableAsWarning(T throwable) { + Objects.requireNonNull(throwable, "'throwable' cannot be null."); if (!logger.isWarnEnabled()) { - return runtimeException; + return throwable; } - performLogging(LogLevel.WARNING, true, runtimeException.getMessage(), runtimeException); - return runtimeException; + performLogging(LogLevel.WARNING, true, throwable.getMessage(), throwable); + return throwable; } /** * Logs the {@link RuntimeException} at the error level and returns it to be thrown. + *

+ * This API covers the cases where a runtime exception type needs to be thrown and logged. If a {@link Throwable} is + * being logged use {@link #logThrowableAsError(Throwable)} instead. * * @param runtimeException RuntimeException to be logged and returned. * @return The passed {@code RuntimeException}. @@ -158,13 +182,29 @@ public RuntimeException logExceptionAsWarning(RuntimeException runtimeException) */ public RuntimeException logExceptionAsError(RuntimeException runtimeException) { Objects.requireNonNull(runtimeException, "'runtimeException' cannot be null."); + + return logThrowableAsError(runtimeException); + } + + /** + * Logs the {@link Throwable} at the error level and returns it to be thrown. + *

+ * This API covers the cases where a checked exception type needs to be thrown and logged. If a {@link + * RuntimeException} is being logged use {@link #logExceptionAsError(RuntimeException)} instead. + * + * @param throwable Throwable to be logged and returned. + * @param Type of the Throwable being logged. + * @return The passed {@link Throwable}. + * @throws NullPointerException If {@code throwable} is {@code null}. + */ + public T logThrowableAsError(T throwable) { + Objects.requireNonNull(throwable, "'throwable' cannot be null."); if (!logger.isErrorEnabled()) { - return runtimeException; + return throwable; } - performLogging(LogLevel.VERBOSE, true, runtimeException.getMessage(), runtimeException); - - return runtimeException; + performLogging(LogLevel.ERROR, true, throwable.getMessage(), throwable); + return throwable; } /* diff --git a/sdk/core/azure-core/src/main/java/com/azure/core/util/logging/LogLevel.java b/sdk/core/azure-core/src/main/java/com/azure/core/util/logging/LogLevel.java index 83b603d17b1e8..7f3d5e1716572 100644 --- a/sdk/core/azure-core/src/main/java/com/azure/core/util/logging/LogLevel.java +++ b/sdk/core/azure-core/src/main/java/com/azure/core/util/logging/LogLevel.java @@ -7,7 +7,7 @@ import java.util.Locale; /** - * Enum which represent logging levels used in Azure SDKs. + * Enum which represent logging levels used in Azure SDKs. */ public enum LogLevel { /** @@ -33,7 +33,7 @@ public enum LogLevel { /** * Indicates that no log level is set. */ - NOT_SET(5); + NOT_SET(5, "5"); private final int numericValue; private final String[] allowedLogLevelVariables; @@ -65,7 +65,7 @@ public int getLogLevel() { * Converts the passed log level string to the corresponding {@link LogLevel}. * * @param logLevelVal The log level value which needs to convert - * @return The LogLevel Enum if pass in the valid string. + * @return The LogLevel Enum if pass in the valid string. * The valid strings for {@link LogLevel} are: *