From 4e5cfa44ceb565b5c45062a849e1f5380d8a5e20 Mon Sep 17 00:00:00 2001 From: Sima Zhu Date: Mon, 16 Dec 2019 16:41:06 -0800 Subject: [PATCH 01/29] Added canLogAtLevel API in logging. --- .../com/azure/core/util/logging/ClientLogger.java | 11 +++++++++++ .../azure/core/util/logging/ClientLoggerTests.java | 13 +++++++++++++ 2 files changed, 24 insertions(+) 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 27868d089e66..824b2ec522c8 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 @@ -173,6 +173,17 @@ private RuntimeException logException(RuntimeException runtimeException, LogLeve return runtimeException; } + /* + * Determines if the environment and logger support logging at the given log level. + * + * @param logLevel Logging level for the log message. + * @return Flag indicating if logger are configured to support logging at the given log level. + */ + public boolean canLogAtLevel(LogLevel logLevel) { + LogLevel environmentLoggingLevel = getEnvironmentLoggingLevel(); + return canLogAtLevel(logLevel, environmentLoggingLevel); + } + /* * Performs the logging. * diff --git a/sdk/core/azure-core/src/test/java/com/azure/core/util/logging/ClientLoggerTests.java b/sdk/core/azure-core/src/test/java/com/azure/core/util/logging/ClientLoggerTests.java index be652413517d..54e8bd24662a 100644 --- a/sdk/core/azure-core/src/test/java/com/azure/core/util/logging/ClientLoggerTests.java +++ b/sdk/core/azure-core/src/test/java/com/azure/core/util/logging/ClientLoggerTests.java @@ -3,6 +3,7 @@ package com.azure.core.util.logging; +import com.azure.core.implementation.LogLevel; import com.azure.core.util.Configuration; import com.azure.core.util.CoreUtils; import org.junit.jupiter.api.AfterEach; @@ -276,6 +277,18 @@ public void logExceptionAsErrorStackTrace() { assertTrue(logValues.contains(runtimeException.getStackTrace()[0].toString())); } + @Test + public void canLogAtLevelTrue(){ + setupLogLevel(2); + assertTrue(new ClientLogger(ClientLoggerTests.class).canLogAtLevel(LogLevel.ERROR)); + } + + @Test + public void canLogAtLevelFalse(){ + setupLogLevel(2); + assertFalse(new ClientLogger(ClientLoggerTests.class).canLogAtLevel(LogLevel.VERBOSE)); + } + private String setupLogLevel(int logLevelToSet) { String originalLogLevel = Configuration.getGlobalConfiguration().get(Configuration.PROPERTY_AZURE_CLOUD); System.setProperty(Configuration.PROPERTY_AZURE_LOG_LEVEL, Integer.toString(logLevelToSet)); From e6ad3c683c548bd8b5bd30a46dc6caa9bd181ef8 Mon Sep 17 00:00:00 2001 From: Sima Zhu <48036328+sima-zhu@users.noreply.github.com> Date: Tue, 17 Dec 2019 11:32:48 -0800 Subject: [PATCH 02/29] Update sdk/core/azure-core/src/main/java/com/azure/core/util/logging/ClientLogger.java Co-Authored-By: Alan Zimmer <48699787+alzimmermsft@users.noreply.github.com> --- .../src/main/java/com/azure/core/util/logging/ClientLogger.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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 824b2ec522c8..41f36e7d5e9e 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 @@ -177,7 +177,7 @@ private RuntimeException logException(RuntimeException runtimeException, LogLeve * Determines if the environment and logger support logging at the given log level. * * @param logLevel Logging level for the log message. - * @return Flag indicating if logger are configured to support logging at the given log level. + * @return Flag indicating if the environment and logger support logging at the given log level. */ public boolean canLogAtLevel(LogLevel logLevel) { LogLevel environmentLoggingLevel = getEnvironmentLoggingLevel(); From 19c6422a56caa943ce781d3513d0d3f0a7cfb555 Mon Sep 17 00:00:00 2001 From: Sima Zhu <48036328+sima-zhu@users.noreply.github.com> Date: Tue, 17 Dec 2019 11:32:58 -0800 Subject: [PATCH 03/29] Update sdk/core/azure-core/src/main/java/com/azure/core/util/logging/ClientLogger.java Co-Authored-By: Alan Zimmer <48699787+alzimmermsft@users.noreply.github.com> --- .../src/main/java/com/azure/core/util/logging/ClientLogger.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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 41f36e7d5e9e..1397ae16265c 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 @@ -176,7 +176,7 @@ private RuntimeException logException(RuntimeException runtimeException, LogLeve /* * Determines if the environment and logger support logging at the given log level. * - * @param logLevel Logging level for the log message. + * @param logLevel The {@link LogLevel} being validated as supported. * @return Flag indicating if the environment and logger support logging at the given log level. */ public boolean canLogAtLevel(LogLevel logLevel) { From 9fc91ba433b41a9e10c34df7902a403f5bc4eec1 Mon Sep 17 00:00:00 2001 From: Sima Zhu Date: Tue, 17 Dec 2019 12:58:51 -0800 Subject: [PATCH 04/29] added parameterized tests for newly added API --- .../core/http/policy/HttpLoggingPolicy.java | 2 +- .../core/implementation/LoggingUtil.java | 1 + .../azure/core/util/logging/ClientLogger.java | 1 - .../logging}/LogLevel.java | 2 +- .../core/util/logging/ClientLoggerTests.java | 27 ++++++++++++------- 5 files changed, 21 insertions(+), 12 deletions(-) rename sdk/core/azure-core/src/main/java/com/azure/core/{implementation => util/logging}/LogLevel.java (96%) diff --git a/sdk/core/azure-core/src/main/java/com/azure/core/http/policy/HttpLoggingPolicy.java b/sdk/core/azure-core/src/main/java/com/azure/core/http/policy/HttpLoggingPolicy.java index 50f6a5f196ad..898f223ca073 100644 --- a/sdk/core/azure-core/src/main/java/com/azure/core/http/policy/HttpLoggingPolicy.java +++ b/sdk/core/azure-core/src/main/java/com/azure/core/http/policy/HttpLoggingPolicy.java @@ -10,11 +10,11 @@ import com.azure.core.http.HttpPipelineNextPolicy; import com.azure.core.http.HttpRequest; import com.azure.core.http.HttpResponse; -import com.azure.core.implementation.LogLevel; import com.azure.core.implementation.LoggingUtil; import com.azure.core.util.CoreUtils; import com.azure.core.util.UrlBuilder; import com.azure.core.util.logging.ClientLogger; +import com.azure.core.util.logging.LogLevel; import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.SerializationFeature; import reactor.core.publisher.Mono; diff --git a/sdk/core/azure-core/src/main/java/com/azure/core/implementation/LoggingUtil.java b/sdk/core/azure-core/src/main/java/com/azure/core/implementation/LoggingUtil.java index bfff5d531638..66637398bcd4 100644 --- a/sdk/core/azure-core/src/main/java/com/azure/core/implementation/LoggingUtil.java +++ b/sdk/core/azure-core/src/main/java/com/azure/core/implementation/LoggingUtil.java @@ -6,6 +6,7 @@ import com.azure.core.util.Configuration; import com.azure.core.util.CoreUtils; +import com.azure.core.util.logging.LogLevel; import java.util.Arrays; import java.util.Map; import java.util.stream.Collectors; 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 824b2ec522c8..e8831a503e29 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 @@ -3,7 +3,6 @@ package com.azure.core.util.logging; -import com.azure.core.implementation.LogLevel; import com.azure.core.util.Configuration; import com.azure.core.util.CoreUtils; import org.slf4j.Logger; diff --git a/sdk/core/azure-core/src/main/java/com/azure/core/implementation/LogLevel.java b/sdk/core/azure-core/src/main/java/com/azure/core/util/logging/LogLevel.java similarity index 96% rename from sdk/core/azure-core/src/main/java/com/azure/core/implementation/LogLevel.java rename to sdk/core/azure-core/src/main/java/com/azure/core/util/logging/LogLevel.java index 56c83b5c5909..684fb2285f75 100644 --- a/sdk/core/azure-core/src/main/java/com/azure/core/implementation/LogLevel.java +++ b/sdk/core/azure-core/src/main/java/com/azure/core/util/logging/LogLevel.java @@ -1,7 +1,7 @@ // Copyright (c) Microsoft Corporation. All rights reserved. // Licensed under the MIT License. -package com.azure.core.implementation; +package com.azure.core.util.logging; /** * Enum which represent logging levels used in Azure SDKs. diff --git a/sdk/core/azure-core/src/test/java/com/azure/core/util/logging/ClientLoggerTests.java b/sdk/core/azure-core/src/test/java/com/azure/core/util/logging/ClientLoggerTests.java index 54e8bd24662a..09144222f7de 100644 --- a/sdk/core/azure-core/src/test/java/com/azure/core/util/logging/ClientLoggerTests.java +++ b/sdk/core/azure-core/src/test/java/com/azure/core/util/logging/ClientLoggerTests.java @@ -3,19 +3,23 @@ package com.azure.core.util.logging; -import com.azure.core.implementation.LogLevel; import com.azure.core.util.Configuration; import com.azure.core.util.CoreUtils; import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.ArgumentsSources; +import org.junit.jupiter.params.provider.CsvFileSource; +import org.junit.jupiter.params.provider.CsvSource; +import org.junit.jupiter.params.provider.EnumSource; import org.junit.jupiter.params.provider.ValueSource; import java.io.ByteArrayOutputStream; import java.io.PrintStream; import java.nio.charset.StandardCharsets; +import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertFalse; import static org.junit.jupiter.api.Assertions.assertTrue; @@ -277,16 +281,21 @@ public void logExceptionAsErrorStackTrace() { assertTrue(logValues.contains(runtimeException.getStackTrace()[0].toString())); } - @Test - public void canLogAtLevelTrue(){ - setupLogLevel(2); - assertTrue(new ClientLogger(ClientLoggerTests.class).canLogAtLevel(LogLevel.ERROR)); + @ParameterizedTest(name = PARAMETERIZED_TEST_NAME_TEMPLATE) + @CsvSource({"1, 1, true", "1, 2, true", "1, 3, true", "1, 4, true", "2, 1, false", "2, 5, false"}) + public void canLogAtLevel(int logLevelToConfigure, int logLevelToValidate, boolean expected){ + setupLogLevel(logLevelToConfigure); + LogLevel logLevel = convertToLogLevel(logLevelToValidate); + assertEquals(new ClientLogger(ClientLoggerTests.class).canLogAtLevel(logLevel), expected); } - @Test - public void canLogAtLevelFalse(){ - setupLogLevel(2); - assertFalse(new ClientLogger(ClientLoggerTests.class).canLogAtLevel(LogLevel.VERBOSE)); + private LogLevel convertToLogLevel(int logLevelStr) { + for (LogLevel level: LogLevel.values()) { + if (logLevelStr == level.toNumeric()) { + return level; + } + } + return null; } private String setupLogLevel(int logLevelToSet) { From b8104d42163591c2ad25372b0e95785ba732907d Mon Sep 17 00:00:00 2001 From: Sima Zhu Date: Tue, 17 Dec 2019 13:01:55 -0800 Subject: [PATCH 05/29] Fixed linting issue --- .../java/com/azure/core/util/logging/ClientLoggerTests.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sdk/core/azure-core/src/test/java/com/azure/core/util/logging/ClientLoggerTests.java b/sdk/core/azure-core/src/test/java/com/azure/core/util/logging/ClientLoggerTests.java index 09144222f7de..8307467dda14 100644 --- a/sdk/core/azure-core/src/test/java/com/azure/core/util/logging/ClientLoggerTests.java +++ b/sdk/core/azure-core/src/test/java/com/azure/core/util/logging/ClientLoggerTests.java @@ -283,7 +283,7 @@ public void logExceptionAsErrorStackTrace() { @ParameterizedTest(name = PARAMETERIZED_TEST_NAME_TEMPLATE) @CsvSource({"1, 1, true", "1, 2, true", "1, 3, true", "1, 4, true", "2, 1, false", "2, 5, false"}) - public void canLogAtLevel(int logLevelToConfigure, int logLevelToValidate, boolean expected){ + public void canLogAtLevel(int logLevelToConfigure, int logLevelToValidate, boolean expected) { setupLogLevel(logLevelToConfigure); LogLevel logLevel = convertToLogLevel(logLevelToValidate); assertEquals(new ClientLogger(ClientLoggerTests.class).canLogAtLevel(logLevel), expected); From 210302cfa5f2f283e7dd6c86d57f615bf3638213 Mon Sep 17 00:00:00 2001 From: Sima Zhu Date: Tue, 17 Dec 2019 16:26:51 -0800 Subject: [PATCH 06/29] Added string alias for log level and remove disable level. --- .../core/implementation/LoggingUtil.java | 14 +---- .../azure/core/util/logging/ClientLogger.java | 5 ++ .../com/azure/core/util/logging/LogLevel.java | 51 +++++++++++++++---- .../core/util/logging/ClientLoggerTests.java | 17 ++++--- 4 files changed, 59 insertions(+), 28 deletions(-) diff --git a/sdk/core/azure-core/src/main/java/com/azure/core/implementation/LoggingUtil.java b/sdk/core/azure-core/src/main/java/com/azure/core/implementation/LoggingUtil.java index 66637398bcd4..f150c01dca82 100644 --- a/sdk/core/azure-core/src/main/java/com/azure/core/implementation/LoggingUtil.java +++ b/sdk/core/azure-core/src/main/java/com/azure/core/implementation/LoggingUtil.java @@ -4,34 +4,24 @@ package com.azure.core.implementation; import com.azure.core.util.Configuration; -import com.azure.core.util.CoreUtils; - import com.azure.core.util.logging.LogLevel; -import java.util.Arrays; -import java.util.Map; -import java.util.stream.Collectors; /** * This class contains utility methods useful for logging. */ public final class LoggingUtil { - private static final Map LOG_LEVEL_MAPPER = Arrays.stream(LogLevel.values()) - .collect(Collectors.toMap(LogLevel::toNumeric, logLevel -> logLevel)); - /** * Retrieve the environment logging level which is used to determine if and what we are allowed to log. * *

The value returned from this method should be used throughout a single logging event as it may change during * the logging operation, this will help prevent difficult to debug timing issues.

* - * @return Environment logging level if set, otherwise {@link LogLevel#DISABLED}. + * @return Environment logging level if set, otherwise null. */ public static LogLevel getEnvironmentLoggingLevel() { String environmentLogLevel = Configuration.getGlobalConfiguration().get(Configuration.PROPERTY_AZURE_LOG_LEVEL); - return CoreUtils.isNullOrEmpty(environmentLogLevel) - ? LogLevel.DISABLED - : LOG_LEVEL_MAPPER.getOrDefault(Integer.parseInt(environmentLogLevel), LogLevel.DISABLED); + return LogLevel.getLogLevel(environmentLogLevel); } // Private constructor 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 0d8b0f94e706..a6c108674b30 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 @@ -246,6 +246,11 @@ private void performLogging(LogLevel logLevel, LogLevel environmentLogLevel, boo * @return Flag indicating if the environment and logger are configured to support logging at the given log level. */ private boolean canLogAtLevel(LogLevel logLevel, LogLevel environmentLoggingLevel) { + // Do not log if logLevel is null or env variable is not set. + if (logLevel == null || environmentLoggingLevel == null) { + return false; + } + // Attempting to log at a level not supported by the environment. if (logLevel.toNumeric() < environmentLoggingLevel.toNumeric()) { return false; 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 684fb2285f75..722a873cc283 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 @@ -3,6 +3,11 @@ package com.azure.core.util.logging; +import java.util.Arrays; +import java.util.HashSet; +import java.util.Locale; +import java.util.Set; + /** * Enum which represent logging levels used in Azure SDKs. */ @@ -10,32 +15,29 @@ public enum LogLevel { /** * Indicates that log level is at verbose level. */ - VERBOSE(1), + VERBOSE(1, "verbose"), /** * Indicates that log level is at information level. */ - INFORMATIONAL(2), + INFORMATIONAL(2, "info", "information", "informational"), /** * Indicates that log level is at warning level. */ - WARNING(3), + WARNING(3, "warn", "warning"), /** * Indicates that log level is at error level. */ - ERROR(4), - - /** - * Indicates that logging is disabled. - */ - DISABLED(5); + ERROR(4, "err", "error"); private final int numericValue; + private final Set allowedLogLevelVariables; - LogLevel(int numericValue) { + LogLevel(int numericValue, String... allowedLogLevelVariables) { this.numericValue = numericValue; + this.allowedLogLevelVariables = new HashSet<>(Arrays.asList(allowedLogLevelVariables)); } /** @@ -46,4 +48,33 @@ public enum LogLevel { public int toNumeric() { return numericValue; } + + /** + * Converts the log level into a numeric representation used for comparisons. + * + * @return The numeric representation of the log level. + */ + public Set getAllowedLogLevels() { + return allowedLogLevelVariables; + } + + /** + * Convert log level value to LogLevel. + * + * @param logLevelVal The log level value which needs to convert + * @return The LogLevel Enum. + */ + public static LogLevel getLogLevel(String logLevelVal) { + return Arrays.stream(LogLevel.values()).filter(logLevel -> + isValueMatch(logLevel, logLevelVal)).findFirst().orElse(null); + } + + /** + * Check if the log level value matches the LogLevel enum. + */ + private static boolean isValueMatch(LogLevel logLevel, String logLevelVal) { + return String.valueOf(logLevel.toNumeric()).equals(logLevelVal) + || logLevel.getAllowedLogLevels().contains(logLevelVal.toLowerCase(Locale.ROOT)); + } + } diff --git a/sdk/core/azure-core/src/test/java/com/azure/core/util/logging/ClientLoggerTests.java b/sdk/core/azure-core/src/test/java/com/azure/core/util/logging/ClientLoggerTests.java index 8307467dda14..18254a47e30b 100644 --- a/sdk/core/azure-core/src/test/java/com/azure/core/util/logging/ClientLoggerTests.java +++ b/sdk/core/azure-core/src/test/java/com/azure/core/util/logging/ClientLoggerTests.java @@ -5,14 +5,12 @@ import com.azure.core.util.Configuration; import com.azure.core.util.CoreUtils; +import org.eclipse.jetty.util.log.Log; import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import org.junit.jupiter.params.ParameterizedTest; -import org.junit.jupiter.params.provider.ArgumentsSources; -import org.junit.jupiter.params.provider.CsvFileSource; import org.junit.jupiter.params.provider.CsvSource; -import org.junit.jupiter.params.provider.EnumSource; import org.junit.jupiter.params.provider.ValueSource; import java.io.ByteArrayOutputStream; @@ -33,6 +31,11 @@ public class ClientLoggerTests { private PrintStream originalSystemErr; private ByteArrayOutputStream logCaptureStream; + @Test + public void test() { + setupLogLevel(1); + } + @BeforeEach public void setupLoggingConfiguration() { /* @@ -282,10 +285,12 @@ public void logExceptionAsErrorStackTrace() { } @ParameterizedTest(name = PARAMETERIZED_TEST_NAME_TEMPLATE) - @CsvSource({"1, 1, true", "1, 2, true", "1, 3, true", "1, 4, true", "2, 1, false", "2, 5, false"}) - public void canLogAtLevel(int logLevelToConfigure, int logLevelToValidate, boolean expected) { + @CsvSource({"1, 1, true", "1, 2, true", "1, 3, true", "1, 4, true", "2, 1, false", "2, 5, false", + "1, VERBOSE, true", "1, info, true", "1, warning, true", "1, error, true", "2, verbose, false", "2, invalid, false"}) + public void canLogAtLevel(int logLevelToConfigure, String logLevelToValidate, boolean expected) { setupLogLevel(logLevelToConfigure); - LogLevel logLevel = convertToLogLevel(logLevelToValidate); + LogLevel logLevel = LogLevel.getLogLevel(logLevelToValidate); + System.out.println(logLevel); assertEquals(new ClientLogger(ClientLoggerTests.class).canLogAtLevel(logLevel), expected); } From 6211513ced4d17143d7e9f84ab84be4f81efa537 Mon Sep 17 00:00:00 2001 From: Sima Zhu Date: Tue, 17 Dec 2019 16:31:38 -0800 Subject: [PATCH 07/29] Remove unused imports --- .../test/java/com/azure/core/util/logging/ClientLoggerTests.java | 1 - 1 file changed, 1 deletion(-) diff --git a/sdk/core/azure-core/src/test/java/com/azure/core/util/logging/ClientLoggerTests.java b/sdk/core/azure-core/src/test/java/com/azure/core/util/logging/ClientLoggerTests.java index 18254a47e30b..aae5a62abb3b 100644 --- a/sdk/core/azure-core/src/test/java/com/azure/core/util/logging/ClientLoggerTests.java +++ b/sdk/core/azure-core/src/test/java/com/azure/core/util/logging/ClientLoggerTests.java @@ -5,7 +5,6 @@ import com.azure.core.util.Configuration; import com.azure.core.util.CoreUtils; -import org.eclipse.jetty.util.log.Log; import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; From d3b9fffe0abf6dd60166344667891a5212b2ea9d Mon Sep 17 00:00:00 2001 From: Sima Zhu Date: Tue, 17 Dec 2019 16:32:28 -0800 Subject: [PATCH 08/29] Remove extra helper methods --- .../com/azure/core/util/logging/ClientLoggerTests.java | 9 --------- 1 file changed, 9 deletions(-) diff --git a/sdk/core/azure-core/src/test/java/com/azure/core/util/logging/ClientLoggerTests.java b/sdk/core/azure-core/src/test/java/com/azure/core/util/logging/ClientLoggerTests.java index aae5a62abb3b..a9557f0d1620 100644 --- a/sdk/core/azure-core/src/test/java/com/azure/core/util/logging/ClientLoggerTests.java +++ b/sdk/core/azure-core/src/test/java/com/azure/core/util/logging/ClientLoggerTests.java @@ -293,15 +293,6 @@ public void canLogAtLevel(int logLevelToConfigure, String logLevelToValidate, bo assertEquals(new ClientLogger(ClientLoggerTests.class).canLogAtLevel(logLevel), expected); } - private LogLevel convertToLogLevel(int logLevelStr) { - for (LogLevel level: LogLevel.values()) { - if (logLevelStr == level.toNumeric()) { - return level; - } - } - return null; - } - private String setupLogLevel(int logLevelToSet) { String originalLogLevel = Configuration.getGlobalConfiguration().get(Configuration.PROPERTY_AZURE_CLOUD); System.setProperty(Configuration.PROPERTY_AZURE_LOG_LEVEL, Integer.toString(logLevelToSet)); From ec2c5b147fcabaede308da71eb556487f4d179ef Mon Sep 17 00:00:00 2001 From: Sima Zhu Date: Tue, 17 Dec 2019 16:32:52 -0800 Subject: [PATCH 09/29] Remove extra helper methods --- .../java/com/azure/core/util/logging/ClientLoggerTests.java | 5 ----- 1 file changed, 5 deletions(-) diff --git a/sdk/core/azure-core/src/test/java/com/azure/core/util/logging/ClientLoggerTests.java b/sdk/core/azure-core/src/test/java/com/azure/core/util/logging/ClientLoggerTests.java index a9557f0d1620..50c1415b1d07 100644 --- a/sdk/core/azure-core/src/test/java/com/azure/core/util/logging/ClientLoggerTests.java +++ b/sdk/core/azure-core/src/test/java/com/azure/core/util/logging/ClientLoggerTests.java @@ -30,11 +30,6 @@ public class ClientLoggerTests { private PrintStream originalSystemErr; private ByteArrayOutputStream logCaptureStream; - @Test - public void test() { - setupLogLevel(1); - } - @BeforeEach public void setupLoggingConfiguration() { /* From 139931968fa2332353e29a8c10de6794cc93959e Mon Sep 17 00:00:00 2001 From: Sima Zhu Date: Tue, 17 Dec 2019 16:37:48 -0800 Subject: [PATCH 10/29] Make changes to JavaDoc. --- .../main/java/com/azure/core/util/logging/ClientLogger.java | 2 +- .../src/main/java/com/azure/core/util/logging/LogLevel.java | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) 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 a6c108674b30..7ea9c75cf4c8 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 @@ -172,7 +172,7 @@ private RuntimeException logException(RuntimeException runtimeException, LogLeve return runtimeException; } - /* + /** * Determines if the environment and logger support logging at the given log level. * * @param logLevel The {@link LogLevel} being validated as supported. 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 722a873cc283..fc2956d9e766 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 @@ -50,9 +50,9 @@ public int toNumeric() { } /** - * Converts the log level into a numeric representation used for comparisons. + * Converts the log level into string representations used for comparisons. * - * @return The numeric representation of the log level. + * @return The string representations of the log level. */ public Set getAllowedLogLevels() { return allowedLogLevelVariables; From 15edb3df3179f1b8cce8bd6d7a5c89c9537019ee Mon Sep 17 00:00:00 2001 From: Sima Zhu <48036328+sima-zhu@users.noreply.github.com> Date: Tue, 17 Dec 2019 16:58:08 -0800 Subject: [PATCH 11/29] Update sdk/core/azure-core/src/main/java/com/azure/core/util/logging/LogLevel.java Co-Authored-By: Alan Zimmer <48699787+alzimmermsft@users.noreply.github.com> --- .../src/main/java/com/azure/core/util/logging/LogLevel.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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 fc2956d9e766..6b7eb0e14495 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 @@ -59,7 +59,7 @@ public Set getAllowedLogLevels() { } /** - * Convert log level value to LogLevel. + * 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. From ceb458a4a489b4443ebb87b65fc3c42b38556352 Mon Sep 17 00:00:00 2001 From: Sima Zhu Date: Tue, 17 Dec 2019 17:25:46 -0800 Subject: [PATCH 12/29] Change public methods private --- .../com/azure/core/http/policy/HttpLoggingPolicy.java | 8 ++++---- .../java/com/azure/core/implementation/LoggingUtil.java | 2 +- .../java/com/azure/core/util/logging/ClientLogger.java | 4 ++-- .../main/java/com/azure/core/util/logging/LogLevel.java | 8 ++++---- .../com/azure/core/util/logging/ClientLoggerTests.java | 2 +- 5 files changed, 12 insertions(+), 12 deletions(-) diff --git a/sdk/core/azure-core/src/main/java/com/azure/core/http/policy/HttpLoggingPolicy.java b/sdk/core/azure-core/src/main/java/com/azure/core/http/policy/HttpLoggingPolicy.java index 898f223ca073..2a586f8ffca2 100644 --- a/sdk/core/azure-core/src/main/java/com/azure/core/http/policy/HttpLoggingPolicy.java +++ b/sdk/core/azure-core/src/main/java/com/azure/core/http/policy/HttpLoggingPolicy.java @@ -101,7 +101,7 @@ public Mono process(HttpPipelineCallContext context, HttpPipelineN * @return A Mono which will emit the string to log. */ private Mono logRequest(final ClientLogger logger, final HttpRequest request) { - int numericLogLevel = LoggingUtil.getEnvironmentLoggingLevel().toNumeric(); + int numericLogLevel = LoggingUtil.getEnvironmentLoggingLevel().getNumericLogLevel(); if (shouldLoggingBeSkipped(numericLogLevel)) { return Mono.empty(); } @@ -181,7 +181,7 @@ private Mono logRequest(final ClientLogger logger, final HttpRequest reque * @return A Mono containing the HTTP response. */ private Mono logResponse(final ClientLogger logger, final HttpResponse response, long startNs) { - int numericLogLevel = LoggingUtil.getEnvironmentLoggingLevel().toNumeric(); + int numericLogLevel = LoggingUtil.getEnvironmentLoggingLevel().getNumericLogLevel(); if (shouldLoggingBeSkipped(numericLogLevel)) { return Mono.just(response); } @@ -260,7 +260,7 @@ private Mono logAndReturn(ClientLogger logger, StringBuilder logMessageBu * @return A flag indicating if logging should be skipped. */ private boolean shouldLoggingBeSkipped(int environmentLogLevel) { - return environmentLogLevel > LogLevel.INFORMATIONAL.toNumeric(); + return environmentLogLevel > LogLevel.INFORMATIONAL.getNumericLogLevel(); } /* @@ -318,7 +318,7 @@ private String getAllowedQueryString(String queryString) { */ private void addHeadersToLogMessage(HttpHeaders headers, StringBuilder sb, int logLevel) { // Either headers shouldn't be logged or the logging level isn't set to VERBOSE, don't add headers. - if (!httpLogDetailLevel.shouldLogHeaders() || logLevel > LogLevel.VERBOSE.toNumeric()) { + if (!httpLogDetailLevel.shouldLogHeaders() || logLevel > LogLevel.VERBOSE.getNumericLogLevel()) { return; } diff --git a/sdk/core/azure-core/src/main/java/com/azure/core/implementation/LoggingUtil.java b/sdk/core/azure-core/src/main/java/com/azure/core/implementation/LoggingUtil.java index f150c01dca82..6be2cd5618d5 100644 --- a/sdk/core/azure-core/src/main/java/com/azure/core/implementation/LoggingUtil.java +++ b/sdk/core/azure-core/src/main/java/com/azure/core/implementation/LoggingUtil.java @@ -21,7 +21,7 @@ public final class LoggingUtil { public static LogLevel getEnvironmentLoggingLevel() { String environmentLogLevel = Configuration.getGlobalConfiguration().get(Configuration.PROPERTY_AZURE_LOG_LEVEL); - return LogLevel.getLogLevel(environmentLogLevel); + return LogLevel.getLogLevelFromString(environmentLogLevel); } // Private constructor 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 7ea9c75cf4c8..a9ac306f4e13 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 @@ -208,7 +208,7 @@ private void performLogging(LogLevel logLevel, LogLevel environmentLogLevel, boo * Environment is logging at a level higher than verbose, strip out the throwable as it would log its * stack trace which is only expected when logging at a verbose level. */ - if (environmentLogLevel.toNumeric() > LogLevel.VERBOSE.toNumeric()) { + if (environmentLogLevel.getNumericLogLevel() > LogLevel.VERBOSE.getNumericLogLevel()) { args = removeThrowable(args); } } @@ -252,7 +252,7 @@ private boolean canLogAtLevel(LogLevel logLevel, LogLevel environmentLoggingLeve } // Attempting to log at a level not supported by the environment. - if (logLevel.toNumeric() < environmentLoggingLevel.toNumeric()) { + if (logLevel.getNumericLogLevel() < environmentLoggingLevel.getNumericLogLevel()) { return false; } 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 fc2956d9e766..d8bfafccbd6e 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 @@ -45,7 +45,7 @@ public enum LogLevel { * * @return The numeric representation of the log level. */ - public int toNumeric() { + public int getNumericLogLevel() { return numericValue; } @@ -54,7 +54,7 @@ public int toNumeric() { * * @return The string representations of the log level. */ - public Set getAllowedLogLevels() { + private Set getAllowedLogLevels() { return allowedLogLevelVariables; } @@ -64,7 +64,7 @@ public Set getAllowedLogLevels() { * @param logLevelVal The log level value which needs to convert * @return The LogLevel Enum. */ - public static LogLevel getLogLevel(String logLevelVal) { + public static LogLevel getLogLevelFromString(String logLevelVal) { return Arrays.stream(LogLevel.values()).filter(logLevel -> isValueMatch(logLevel, logLevelVal)).findFirst().orElse(null); } @@ -73,7 +73,7 @@ public static LogLevel getLogLevel(String logLevelVal) { * Check if the log level value matches the LogLevel enum. */ private static boolean isValueMatch(LogLevel logLevel, String logLevelVal) { - return String.valueOf(logLevel.toNumeric()).equals(logLevelVal) + return String.valueOf(logLevel.getNumericLogLevel()).equals(logLevelVal) || logLevel.getAllowedLogLevels().contains(logLevelVal.toLowerCase(Locale.ROOT)); } diff --git a/sdk/core/azure-core/src/test/java/com/azure/core/util/logging/ClientLoggerTests.java b/sdk/core/azure-core/src/test/java/com/azure/core/util/logging/ClientLoggerTests.java index 50c1415b1d07..c21a2b5a8006 100644 --- a/sdk/core/azure-core/src/test/java/com/azure/core/util/logging/ClientLoggerTests.java +++ b/sdk/core/azure-core/src/test/java/com/azure/core/util/logging/ClientLoggerTests.java @@ -283,7 +283,7 @@ public void logExceptionAsErrorStackTrace() { "1, VERBOSE, true", "1, info, true", "1, warning, true", "1, error, true", "2, verbose, false", "2, invalid, false"}) public void canLogAtLevel(int logLevelToConfigure, String logLevelToValidate, boolean expected) { setupLogLevel(logLevelToConfigure); - LogLevel logLevel = LogLevel.getLogLevel(logLevelToValidate); + LogLevel logLevel = LogLevel.getLogLevelFromString(logLevelToValidate); System.out.println(logLevel); assertEquals(new ClientLogger(ClientLoggerTests.class).canLogAtLevel(logLevel), expected); } From f6ed817dad03b0fd8815f0464b1de9673a5678aa Mon Sep 17 00:00:00 2001 From: Sima Zhu Date: Tue, 17 Dec 2019 20:46:32 -0800 Subject: [PATCH 13/29] Remove numeric, added map at initial --- .../com/azure/core/util/logging/LogLevel.java | 31 +++++++++++-------- 1 file changed, 18 insertions(+), 13 deletions(-) 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 272be168bf72..b8371c37a8ca 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 @@ -6,7 +6,11 @@ import java.util.Arrays; import java.util.HashSet; import java.util.Locale; +import java.util.Map; import java.util.Set; +import java.util.stream.Collectors; +import reactor.util.function.Tuple2; +import reactor.util.function.Tuples; /** * Enum which represent logging levels used in Azure SDKs. @@ -15,29 +19,27 @@ public enum LogLevel { /** * Indicates that log level is at verbose level. */ - VERBOSE(1, "verbose"), + VERBOSE("1", "verbose"), /** * Indicates that log level is at information level. */ - INFORMATIONAL(2, "info", "information", "informational"), + INFORMATIONAL("2", "info", "information", "informational"), /** * Indicates that log level is at warning level. */ - WARNING(3, "warn", "warning"), + WARNING("3", "warn", "warning"), /** * Indicates that log level is at error level. */ - ERROR(4, "err", "error"); + ERROR("4", "err", "error"); - private final int numericValue; - private final Set allowedLogLevelVariables; + private final String[] allowedLogLevelVariables; - LogLevel(int numericValue, String... allowedLogLevelVariables) { - this.numericValue = numericValue; - this.allowedLogLevelVariables = new HashSet<>(Arrays.asList(allowedLogLevelVariables)); + LogLevel(String... allowedLogLevelVariables) { + this.allowedLogLevelVariables = allowedLogLevelVariables; } /** @@ -46,7 +48,7 @@ public enum LogLevel { * @return The numeric representation of the log level. */ public int getNumericLogLevel() { - return numericValue; + return Integer.parseInt(allowedLogLevelVariables[0]); } /** @@ -54,10 +56,14 @@ public int getNumericLogLevel() { * * @return The string representations of the log level. */ - private Set getAllowedLogLevels() { + private String[] getAllowedLogLevels() { return allowedLogLevelVariables; } + private static final Map LOG_LEVEL_STRING_MAPPER = Arrays.stream(LogLevel.values()) + .flatMap(logLevel -> Arrays.stream(logLevel.getAllowedLogLevels()).map(v -> Tuples.of(v, logLevel))) + .collect(Collectors.toMap(Tuple2::getT1, Tuple2::getT2)); + /** * Converts the passed log level string to the corresponding {@link LogLevel}. * @@ -65,8 +71,7 @@ private Set getAllowedLogLevels() { * @return The LogLevel Enum. */ public static LogLevel getLogLevelFromString(String logLevelVal) { - return Arrays.stream(LogLevel.values()).filter(logLevel -> - isValueMatch(logLevel, logLevelVal)).findFirst().orElse(null); + return LOG_LEVEL_STRING_MAPPER.get(logLevelVal); } /** From 12d82d9ff1327314c68b607bf546cecf98d0b45d Mon Sep 17 00:00:00 2001 From: Sima Zhu Date: Tue, 17 Dec 2019 20:50:45 -0800 Subject: [PATCH 14/29] Make changes to name --- .../azure/core/http/policy/HttpLoggingPolicy.java | 8 ++++---- .../com/azure/core/implementation/LoggingUtil.java | 2 +- .../com/azure/core/util/logging/ClientLogger.java | 4 ++-- .../java/com/azure/core/util/logging/LogLevel.java | 13 ++----------- .../azure/core/util/logging/ClientLoggerTests.java | 2 +- 5 files changed, 10 insertions(+), 19 deletions(-) diff --git a/sdk/core/azure-core/src/main/java/com/azure/core/http/policy/HttpLoggingPolicy.java b/sdk/core/azure-core/src/main/java/com/azure/core/http/policy/HttpLoggingPolicy.java index 2a586f8ffca2..6a39e5d386a6 100644 --- a/sdk/core/azure-core/src/main/java/com/azure/core/http/policy/HttpLoggingPolicy.java +++ b/sdk/core/azure-core/src/main/java/com/azure/core/http/policy/HttpLoggingPolicy.java @@ -101,7 +101,7 @@ public Mono process(HttpPipelineCallContext context, HttpPipelineN * @return A Mono which will emit the string to log. */ private Mono logRequest(final ClientLogger logger, final HttpRequest request) { - int numericLogLevel = LoggingUtil.getEnvironmentLoggingLevel().getNumericLogLevel(); + int numericLogLevel = LoggingUtil.getEnvironmentLoggingLevel().getLogLevel(); if (shouldLoggingBeSkipped(numericLogLevel)) { return Mono.empty(); } @@ -181,7 +181,7 @@ private Mono logRequest(final ClientLogger logger, final HttpRequest reque * @return A Mono containing the HTTP response. */ private Mono logResponse(final ClientLogger logger, final HttpResponse response, long startNs) { - int numericLogLevel = LoggingUtil.getEnvironmentLoggingLevel().getNumericLogLevel(); + int numericLogLevel = LoggingUtil.getEnvironmentLoggingLevel().getLogLevel(); if (shouldLoggingBeSkipped(numericLogLevel)) { return Mono.just(response); } @@ -260,7 +260,7 @@ private Mono logAndReturn(ClientLogger logger, StringBuilder logMessageBu * @return A flag indicating if logging should be skipped. */ private boolean shouldLoggingBeSkipped(int environmentLogLevel) { - return environmentLogLevel > LogLevel.INFORMATIONAL.getNumericLogLevel(); + return environmentLogLevel > LogLevel.INFORMATIONAL.getLogLevel(); } /* @@ -318,7 +318,7 @@ private String getAllowedQueryString(String queryString) { */ private void addHeadersToLogMessage(HttpHeaders headers, StringBuilder sb, int logLevel) { // Either headers shouldn't be logged or the logging level isn't set to VERBOSE, don't add headers. - if (!httpLogDetailLevel.shouldLogHeaders() || logLevel > LogLevel.VERBOSE.getNumericLogLevel()) { + if (!httpLogDetailLevel.shouldLogHeaders() || logLevel > LogLevel.VERBOSE.getLogLevel()) { return; } diff --git a/sdk/core/azure-core/src/main/java/com/azure/core/implementation/LoggingUtil.java b/sdk/core/azure-core/src/main/java/com/azure/core/implementation/LoggingUtil.java index 6be2cd5618d5..1867be65bf54 100644 --- a/sdk/core/azure-core/src/main/java/com/azure/core/implementation/LoggingUtil.java +++ b/sdk/core/azure-core/src/main/java/com/azure/core/implementation/LoggingUtil.java @@ -21,7 +21,7 @@ public final class LoggingUtil { public static LogLevel getEnvironmentLoggingLevel() { String environmentLogLevel = Configuration.getGlobalConfiguration().get(Configuration.PROPERTY_AZURE_LOG_LEVEL); - return LogLevel.getLogLevelFromString(environmentLogLevel); + return LogLevel.fromString(environmentLogLevel); } // Private constructor 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 a9ac306f4e13..7b016a6f8b24 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 @@ -208,7 +208,7 @@ private void performLogging(LogLevel logLevel, LogLevel environmentLogLevel, boo * Environment is logging at a level higher than verbose, strip out the throwable as it would log its * stack trace which is only expected when logging at a verbose level. */ - if (environmentLogLevel.getNumericLogLevel() > LogLevel.VERBOSE.getNumericLogLevel()) { + if (environmentLogLevel.getLogLevel() > LogLevel.VERBOSE.getLogLevel()) { args = removeThrowable(args); } } @@ -252,7 +252,7 @@ private boolean canLogAtLevel(LogLevel logLevel, LogLevel environmentLoggingLeve } // Attempting to log at a level not supported by the environment. - if (logLevel.getNumericLogLevel() < environmentLoggingLevel.getNumericLogLevel()) { + if (logLevel.getLogLevel() < environmentLoggingLevel.getLogLevel()) { return false; } 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 b8371c37a8ca..c8fa5c078ee4 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 @@ -47,7 +47,7 @@ public enum LogLevel { * * @return The numeric representation of the log level. */ - public int getNumericLogLevel() { + public int getLogLevel() { return Integer.parseInt(allowedLogLevelVariables[0]); } @@ -70,16 +70,7 @@ private String[] getAllowedLogLevels() { * @param logLevelVal The log level value which needs to convert * @return The LogLevel Enum. */ - public static LogLevel getLogLevelFromString(String logLevelVal) { + public static LogLevel fromString(String logLevelVal) { return LOG_LEVEL_STRING_MAPPER.get(logLevelVal); } - - /** - * Check if the log level value matches the LogLevel enum. - */ - private static boolean isValueMatch(LogLevel logLevel, String logLevelVal) { - return String.valueOf(logLevel.getNumericLogLevel()).equals(logLevelVal) - || logLevel.getAllowedLogLevels().contains(logLevelVal.toLowerCase(Locale.ROOT)); - } - } diff --git a/sdk/core/azure-core/src/test/java/com/azure/core/util/logging/ClientLoggerTests.java b/sdk/core/azure-core/src/test/java/com/azure/core/util/logging/ClientLoggerTests.java index c21a2b5a8006..8ee87cd350ed 100644 --- a/sdk/core/azure-core/src/test/java/com/azure/core/util/logging/ClientLoggerTests.java +++ b/sdk/core/azure-core/src/test/java/com/azure/core/util/logging/ClientLoggerTests.java @@ -283,7 +283,7 @@ public void logExceptionAsErrorStackTrace() { "1, VERBOSE, true", "1, info, true", "1, warning, true", "1, error, true", "2, verbose, false", "2, invalid, false"}) public void canLogAtLevel(int logLevelToConfigure, String logLevelToValidate, boolean expected) { setupLogLevel(logLevelToConfigure); - LogLevel logLevel = LogLevel.getLogLevelFromString(logLevelToValidate); + LogLevel logLevel = LogLevel.fromString(logLevelToValidate); System.out.println(logLevel); assertEquals(new ClientLogger(ClientLoggerTests.class).canLogAtLevel(logLevel), expected); } From 9235b21d8e7186cfe44352f8a3900b582710bd7a Mon Sep 17 00:00:00 2001 From: Sima Zhu Date: Tue, 17 Dec 2019 21:03:52 -0800 Subject: [PATCH 15/29] Fixed linting issue --- .../src/main/java/com/azure/core/util/logging/LogLevel.java | 4 +--- .../java/com/azure/core/util/logging/ClientLoggerTests.java | 6 ++---- 2 files changed, 3 insertions(+), 7 deletions(-) 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 c8fa5c078ee4..35b1fc8dc0b4 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 @@ -4,10 +4,8 @@ package com.azure.core.util.logging; import java.util.Arrays; -import java.util.HashSet; import java.util.Locale; import java.util.Map; -import java.util.Set; import java.util.stream.Collectors; import reactor.util.function.Tuple2; import reactor.util.function.Tuples; @@ -71,6 +69,6 @@ private String[] getAllowedLogLevels() { * @return The LogLevel Enum. */ public static LogLevel fromString(String logLevelVal) { - return LOG_LEVEL_STRING_MAPPER.get(logLevelVal); + return LOG_LEVEL_STRING_MAPPER.get(logLevelVal.toLowerCase(Locale.ROOT)); } } diff --git a/sdk/core/azure-core/src/test/java/com/azure/core/util/logging/ClientLoggerTests.java b/sdk/core/azure-core/src/test/java/com/azure/core/util/logging/ClientLoggerTests.java index 8ee87cd350ed..cdf0602a033d 100644 --- a/sdk/core/azure-core/src/test/java/com/azure/core/util/logging/ClientLoggerTests.java +++ b/sdk/core/azure-core/src/test/java/com/azure/core/util/logging/ClientLoggerTests.java @@ -278,13 +278,11 @@ public void logExceptionAsErrorStackTrace() { assertTrue(logValues.contains(runtimeException.getStackTrace()[0].toString())); } - @ParameterizedTest(name = PARAMETERIZED_TEST_NAME_TEMPLATE) - @CsvSource({"1, 1, true", "1, 2, true", "1, 3, true", "1, 4, true", "2, 1, false", "2, 5, false", - "1, VERBOSE, true", "1, info, true", "1, warning, true", "1, error, true", "2, verbose, false", "2, invalid, false"}) + @ParameterizedTest(name = "{index} from logLevelToConfigure = {0}, logLevelToValidate={1}, expected={2}") + @CsvSource({"1, 1, true", "1, 2, true", "1, 3, true", "1, 4, true", "2, 1, false", "2, 5, false", "1, VERBOSE, true", "1, info, true", "1, warning, true", "1, error, true", "2, verbose, false", "2, invalid, false"}) public void canLogAtLevel(int logLevelToConfigure, String logLevelToValidate, boolean expected) { setupLogLevel(logLevelToConfigure); LogLevel logLevel = LogLevel.fromString(logLevelToValidate); - System.out.println(logLevel); assertEquals(new ClientLogger(ClientLoggerTests.class).canLogAtLevel(logLevel), expected); } From 96b9133de91dec7ef3fbe80e9c141bac2cf59e21 Mon Sep 17 00:00:00 2001 From: Sima Zhu Date: Wed, 18 Dec 2019 10:32:38 -0800 Subject: [PATCH 16/29] Fixed null value bugs --- .../src/main/java/com/azure/core/util/logging/LogLevel.java | 3 +++ 1 file changed, 3 insertions(+) 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 35b1fc8dc0b4..4a7ff0b8f60d 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 @@ -69,6 +69,9 @@ private String[] getAllowedLogLevels() { * @return The LogLevel Enum. */ public static LogLevel fromString(String logLevelVal) { + if (logLevelVal == null) { + return null; + } return LOG_LEVEL_STRING_MAPPER.get(logLevelVal.toLowerCase(Locale.ROOT)); } } From a2862b7cd0f35d125a90fc9187a786951da22db3 Mon Sep 17 00:00:00 2001 From: Sima Zhu Date: Wed, 18 Dec 2019 11:57:10 -0800 Subject: [PATCH 17/29] Added more null gating for logging --- .../azure/core/http/policy/HttpLoggingPolicy.java | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/sdk/core/azure-core/src/main/java/com/azure/core/http/policy/HttpLoggingPolicy.java b/sdk/core/azure-core/src/main/java/com/azure/core/http/policy/HttpLoggingPolicy.java index 6a39e5d386a6..c12be9103435 100644 --- a/sdk/core/azure-core/src/main/java/com/azure/core/http/policy/HttpLoggingPolicy.java +++ b/sdk/core/azure-core/src/main/java/com/azure/core/http/policy/HttpLoggingPolicy.java @@ -101,7 +101,11 @@ public Mono process(HttpPipelineCallContext context, HttpPipelineN * @return A Mono which will emit the string to log. */ private Mono logRequest(final ClientLogger logger, final HttpRequest request) { - int numericLogLevel = LoggingUtil.getEnvironmentLoggingLevel().getLogLevel(); + LogLevel logLevel = LoggingUtil.getEnvironmentLoggingLevel(); + if (logLevel == null) { + return Mono.empty(); + } + int numericLogLevel = logLevel.getLogLevel(); if (shouldLoggingBeSkipped(numericLogLevel)) { return Mono.empty(); } @@ -181,7 +185,12 @@ private Mono logRequest(final ClientLogger logger, final HttpRequest reque * @return A Mono containing the HTTP response. */ private Mono logResponse(final ClientLogger logger, final HttpResponse response, long startNs) { - int numericLogLevel = LoggingUtil.getEnvironmentLoggingLevel().getLogLevel(); + LogLevel logLevel = LoggingUtil.getEnvironmentLoggingLevel(); + if (logLevel == null) { + return Mono.just(response); + } + + int numericLogLevel = logLevel.getLogLevel(); if (shouldLoggingBeSkipped(numericLogLevel)) { return Mono.just(response); } From 5334113abafea2a102c2caaf4d3e6fb34a923cec Mon Sep 17 00:00:00 2001 From: Sima Zhu Date: Wed, 18 Dec 2019 14:42:01 -0800 Subject: [PATCH 18/29] Make changes to add NOT_SET, throw error for invalid log level --- .../core/http/policy/HttpLoggingPolicy.java | 14 ++--- .../com/azure/core/util/Configuration.java | 2 +- .../azure/core/util/logging/ClientLogger.java | 3 +- .../com/azure/core/util/logging/LogLevel.java | 53 ++++++++++--------- .../core/util/logging/ClientLoggerTests.java | 25 +++++---- 5 files changed, 47 insertions(+), 50 deletions(-) diff --git a/sdk/core/azure-core/src/main/java/com/azure/core/http/policy/HttpLoggingPolicy.java b/sdk/core/azure-core/src/main/java/com/azure/core/http/policy/HttpLoggingPolicy.java index c12be9103435..9e0b153593ba 100644 --- a/sdk/core/azure-core/src/main/java/com/azure/core/http/policy/HttpLoggingPolicy.java +++ b/sdk/core/azure-core/src/main/java/com/azure/core/http/policy/HttpLoggingPolicy.java @@ -101,11 +101,8 @@ public Mono process(HttpPipelineCallContext context, HttpPipelineN * @return A Mono which will emit the string to log. */ private Mono logRequest(final ClientLogger logger, final HttpRequest request) { - LogLevel logLevel = LoggingUtil.getEnvironmentLoggingLevel(); - if (logLevel == null) { - return Mono.empty(); - } - int numericLogLevel = logLevel.getLogLevel(); + int numericLogLevel = LoggingUtil.getEnvironmentLoggingLevel().getLogLevel(); + if (shouldLoggingBeSkipped(numericLogLevel)) { return Mono.empty(); } @@ -185,12 +182,7 @@ private Mono logRequest(final ClientLogger logger, final HttpRequest reque * @return A Mono containing the HTTP response. */ private Mono logResponse(final ClientLogger logger, final HttpResponse response, long startNs) { - LogLevel logLevel = LoggingUtil.getEnvironmentLoggingLevel(); - if (logLevel == null) { - return Mono.just(response); - } - - int numericLogLevel = logLevel.getLogLevel(); + int numericLogLevel = LoggingUtil.getEnvironmentLoggingLevel().getLogLevel(); if (shouldLoggingBeSkipped(numericLogLevel)) { return Mono.just(response); } diff --git a/sdk/core/azure-core/src/main/java/com/azure/core/util/Configuration.java b/sdk/core/azure-core/src/main/java/com/azure/core/util/Configuration.java index bf7b8cbd4a8e..bf1415d60c09 100644 --- a/sdk/core/azure-core/src/main/java/com/azure/core/util/Configuration.java +++ b/sdk/core/azure-core/src/main/java/com/azure/core/util/Configuration.java @@ -347,7 +347,7 @@ private boolean loadFrom(String name, Function loader, String lo } else { // Value changed, log it! configurations.put(name, value); - logger.verbose(logMessage, name); + //logger.verbose(logMessage, name); return true; } } 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 7b016a6f8b24..985dbe5816e4 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 @@ -179,8 +179,7 @@ private RuntimeException logException(RuntimeException runtimeException, LogLeve * @return Flag indicating if the environment and logger support logging at the given log level. */ public boolean canLogAtLevel(LogLevel logLevel) { - LogLevel environmentLoggingLevel = getEnvironmentLoggingLevel(); - return canLogAtLevel(logLevel, environmentLoggingLevel); + return canLogAtLevel(logLevel, getEnvironmentLoggingLevel()); } /* 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 4a7ff0b8f60d..7516e3df82c4 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 @@ -3,12 +3,8 @@ package com.azure.core.util.logging; -import java.util.Arrays; +import java.util.HashMap; import java.util.Locale; -import java.util.Map; -import java.util.stream.Collectors; -import reactor.util.function.Tuple2; -import reactor.util.function.Tuples; /** * Enum which represent logging levels used in Azure SDKs. @@ -17,26 +13,42 @@ public enum LogLevel { /** * Indicates that log level is at verbose level. */ - VERBOSE("1", "verbose"), + VERBOSE(1, "1", "verbose"), /** * Indicates that log level is at information level. */ - INFORMATIONAL("2", "info", "information", "informational"), + INFORMATIONAL(2, "2", "info", "information", "informational"), /** * Indicates that log level is at warning level. */ - WARNING("3", "warn", "warning"), + WARNING(3, "3", "warn", "warning"), /** * Indicates that log level is at error level. */ - ERROR("4", "err", "error"); + ERROR(4, "4", "err", "error"), + /** + * Indicates that no log level is set. + */ + NOT_SET(5); + + private final int numericValue; private final String[] allowedLogLevelVariables; + private static HashMap LOG_LEVEL_STRING_MAPPER = new HashMap<>(); - LogLevel(String... allowedLogLevelVariables) { + static{ + for (LogLevel logLevel: LogLevel.values()) { + for (String val: logLevel.allowedLogLevelVariables) { + LOG_LEVEL_STRING_MAPPER.put(val, logLevel); + } + } + } + + LogLevel(int numericValue, String... allowedLogLevelVariables) { + this.numericValue = numericValue; this.allowedLogLevelVariables = allowedLogLevelVariables; } @@ -46,22 +58,9 @@ public enum LogLevel { * @return The numeric representation of the log level. */ public int getLogLevel() { - return Integer.parseInt(allowedLogLevelVariables[0]); + return numericValue; } - /** - * Converts the log level into string representations used for comparisons. - * - * @return The string representations of the log level. - */ - private String[] getAllowedLogLevels() { - return allowedLogLevelVariables; - } - - private static final Map LOG_LEVEL_STRING_MAPPER = Arrays.stream(LogLevel.values()) - .flatMap(logLevel -> Arrays.stream(logLevel.getAllowedLogLevels()).map(v -> Tuples.of(v, logLevel))) - .collect(Collectors.toMap(Tuple2::getT1, Tuple2::getT2)); - /** * Converts the passed log level string to the corresponding {@link LogLevel}. * @@ -70,7 +69,11 @@ private String[] getAllowedLogLevels() { */ public static LogLevel fromString(String logLevelVal) { if (logLevelVal == null) { - return null; + return LogLevel.NOT_SET; + } + if (!LOG_LEVEL_STRING_MAPPER.containsKey(logLevelVal.toLowerCase(Locale.ROOT))) { + throw new IllegalArgumentException("We currently do not support the log level you set. LogLevel: " + + logLevelVal); } return LOG_LEVEL_STRING_MAPPER.get(logLevelVal.toLowerCase(Locale.ROOT)); } diff --git a/sdk/core/azure-core/src/test/java/com/azure/core/util/logging/ClientLoggerTests.java b/sdk/core/azure-core/src/test/java/com/azure/core/util/logging/ClientLoggerTests.java index cdf0602a033d..74f5b6e77ca8 100644 --- a/sdk/core/azure-core/src/test/java/com/azure/core/util/logging/ClientLoggerTests.java +++ b/sdk/core/azure-core/src/test/java/com/azure/core/util/logging/ClientLoggerTests.java @@ -18,6 +18,7 @@ import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertThrows; import static org.junit.jupiter.api.Assertions.assertTrue; /** @@ -107,15 +108,11 @@ public void logAtUnsupportedLevel(int logLevel) { @ValueSource(ints = { 1, 2, 3, 4 }) public void logWhenLoggingDisabled(int logLevel) { String logMessage = "This is a test"; - - String originalLogLevel = setupLogLevel(5); - logMessage(new ClientLogger(ClientLoggerTests.class), logLevel, logMessage); - setPropertyToOriginalOrClear(Configuration.PROPERTY_AZURE_LOG_LEVEL, originalLogLevel); - - String logValues = new String(logCaptureStream.toByteArray(), StandardCharsets.UTF_8); - assertFalse(logValues.contains(logMessage)); + setupLogLevel(5); + assertThrows(IllegalArgumentException.class, () -> + logMessage(new ClientLogger(ClientLoggerTests.class), logLevel, logMessage)); } - + /** * Tests that logging an exception when the log level isn't VERBOSE only log the exception message. */ @@ -278,18 +275,24 @@ public void logExceptionAsErrorStackTrace() { assertTrue(logValues.contains(runtimeException.getStackTrace()[0].toString())); } - @ParameterizedTest(name = "{index} from logLevelToConfigure = {0}, logLevelToValidate={1}, expected={2}") - @CsvSource({"1, 1, true", "1, 2, true", "1, 3, true", "1, 4, true", "2, 1, false", "2, 5, false", "1, VERBOSE, true", "1, info, true", "1, warning, true", "1, error, true", "2, verbose, false", "2, invalid, false"}) + @ParameterizedTest(name = "{index} from logLevelToConfigure = {0}, logLevelToValidate = {1}, expected = {2}") + @CsvSource({"1, 1, true", "1, 2, true", "1, 3, true", "1, 4, true", "2, 1, false", "1, VERBOSE, true", "1, info, true", "1, warning, true", "1, error, true", "2, verbose, false"}) public void canLogAtLevel(int logLevelToConfigure, String logLevelToValidate, boolean expected) { setupLogLevel(logLevelToConfigure); LogLevel logLevel = LogLevel.fromString(logLevelToValidate); assertEquals(new ClientLogger(ClientLoggerTests.class).canLogAtLevel(logLevel), expected); } + @ParameterizedTest(name = PARAMETERIZED_TEST_NAME_TEMPLATE) + @ValueSource(strings = {"5", "invalid"}) + public void canLogAtLevelInvalid(String logLevelToValidate) { + setupLogLevel(2); + assertThrows(IllegalArgumentException.class, () -> LogLevel.fromString(logLevelToValidate)); + } + private String setupLogLevel(int logLevelToSet) { String originalLogLevel = Configuration.getGlobalConfiguration().get(Configuration.PROPERTY_AZURE_CLOUD); System.setProperty(Configuration.PROPERTY_AZURE_LOG_LEVEL, Integer.toString(logLevelToSet)); - return originalLogLevel; } From 1ac0104ff6a8de801767538e9a6b0c62f43eb537 Mon Sep 17 00:00:00 2001 From: Sima Zhu Date: Wed, 18 Dec 2019 14:48:59 -0800 Subject: [PATCH 19/29] Fixed some changes --- .../src/main/java/com/azure/core/util/Configuration.java | 2 +- .../src/main/java/com/azure/core/util/logging/LogLevel.java | 4 ++-- .../java/com/azure/core/util/logging/ClientLoggerTests.java | 2 +- 3 files changed, 4 insertions(+), 4 deletions(-) diff --git a/sdk/core/azure-core/src/main/java/com/azure/core/util/Configuration.java b/sdk/core/azure-core/src/main/java/com/azure/core/util/Configuration.java index bf1415d60c09..bf7b8cbd4a8e 100644 --- a/sdk/core/azure-core/src/main/java/com/azure/core/util/Configuration.java +++ b/sdk/core/azure-core/src/main/java/com/azure/core/util/Configuration.java @@ -347,7 +347,7 @@ private boolean loadFrom(String name, Function loader, String lo } else { // Value changed, log it! configurations.put(name, value); - //logger.verbose(logMessage, name); + logger.verbose(logMessage, name); return true; } } 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 7516e3df82c4..aa2ecc50b2ac 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 @@ -13,7 +13,7 @@ public enum LogLevel { /** * Indicates that log level is at verbose level. */ - VERBOSE(1, "1", "verbose"), + VERBOSE(1, "1", "verbose", "debug"), /** * Indicates that log level is at information level. @@ -37,7 +37,7 @@ public enum LogLevel { private final int numericValue; private final String[] allowedLogLevelVariables; - private static HashMap LOG_LEVEL_STRING_MAPPER = new HashMap<>(); + private static final HashMap LOG_LEVEL_STRING_MAPPER = new HashMap<>(); static{ for (LogLevel logLevel: LogLevel.values()) { diff --git a/sdk/core/azure-core/src/test/java/com/azure/core/util/logging/ClientLoggerTests.java b/sdk/core/azure-core/src/test/java/com/azure/core/util/logging/ClientLoggerTests.java index 74f5b6e77ca8..1e779e08d67a 100644 --- a/sdk/core/azure-core/src/test/java/com/azure/core/util/logging/ClientLoggerTests.java +++ b/sdk/core/azure-core/src/test/java/com/azure/core/util/logging/ClientLoggerTests.java @@ -112,7 +112,7 @@ public void logWhenLoggingDisabled(int logLevel) { assertThrows(IllegalArgumentException.class, () -> logMessage(new ClientLogger(ClientLoggerTests.class), logLevel, logMessage)); } - + /** * Tests that logging an exception when the log level isn't VERBOSE only log the exception message. */ From 13de5d813a3258d004db729b2ac551f8856b8d5d Mon Sep 17 00:00:00 2001 From: Sima Zhu Date: Wed, 18 Dec 2019 15:00:22 -0800 Subject: [PATCH 20/29] Fixed linting issue --- .../main/java/com/azure/core/implementation/LoggingUtil.java | 2 +- .../main/java/com/azure/core/util/logging/ClientLogger.java | 4 ++-- .../src/main/java/com/azure/core/util/logging/LogLevel.java | 3 ++- 3 files changed, 5 insertions(+), 4 deletions(-) diff --git a/sdk/core/azure-core/src/main/java/com/azure/core/implementation/LoggingUtil.java b/sdk/core/azure-core/src/main/java/com/azure/core/implementation/LoggingUtil.java index 1867be65bf54..3f7eb257a99e 100644 --- a/sdk/core/azure-core/src/main/java/com/azure/core/implementation/LoggingUtil.java +++ b/sdk/core/azure-core/src/main/java/com/azure/core/implementation/LoggingUtil.java @@ -16,7 +16,7 @@ public final class LoggingUtil { *

The value returned from this method should be used throughout a single logging event as it may change during * the logging operation, this will help prevent difficult to debug timing issues.

* - * @return Environment logging level if set, otherwise null. + * @return Environment logging level if set, otherwise {@link LogLevel#NOT_SET}. */ public static LogLevel getEnvironmentLoggingLevel() { String environmentLogLevel = Configuration.getGlobalConfiguration().get(Configuration.PROPERTY_AZURE_LOG_LEVEL); 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 985dbe5816e4..0938669d75a2 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 @@ -245,8 +245,8 @@ private void performLogging(LogLevel logLevel, LogLevel environmentLogLevel, boo * @return Flag indicating if the environment and logger are configured to support logging at the given log level. */ private boolean canLogAtLevel(LogLevel logLevel, LogLevel environmentLoggingLevel) { - // Do not log if logLevel is null or env variable is not set. - if (logLevel == null || environmentLoggingLevel == null) { + // Do not log if logLevel is null is not set. + if (logLevel == null) { return false; } 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 aa2ecc50b2ac..c6eff761b07c 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 @@ -39,7 +39,7 @@ public enum LogLevel { private final String[] allowedLogLevelVariables; private static final HashMap LOG_LEVEL_STRING_MAPPER = new HashMap<>(); - static{ + static { for (LogLevel logLevel: LogLevel.values()) { for (String val: logLevel.allowedLogLevelVariables) { LOG_LEVEL_STRING_MAPPER.put(val, logLevel); @@ -66,6 +66,7 @@ public int getLogLevel() { * * @param logLevelVal The log level value which needs to convert * @return The LogLevel Enum. + * @throws IllegalArgumentException if the log level value is invalid. */ public static LogLevel fromString(String logLevelVal) { if (logLevelVal == null) { From dd01e2f19701175dc7945a7d1ddf1429907fa787 Mon Sep 17 00:00:00 2001 From: Sima Zhu Date: Wed, 18 Dec 2019 15:27:27 -0800 Subject: [PATCH 21/29] Added more test cases --- .../com/azure/core/util/logging/ClientLoggerTests.java | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/sdk/core/azure-core/src/test/java/com/azure/core/util/logging/ClientLoggerTests.java b/sdk/core/azure-core/src/test/java/com/azure/core/util/logging/ClientLoggerTests.java index 1e779e08d67a..3df9c95197c9 100644 --- a/sdk/core/azure-core/src/test/java/com/azure/core/util/logging/ClientLoggerTests.java +++ b/sdk/core/azure-core/src/test/java/com/azure/core/util/logging/ClientLoggerTests.java @@ -113,6 +113,14 @@ public void logWhenLoggingDisabled(int logLevel) { logMessage(new ClientLogger(ClientLoggerTests.class), logLevel, logMessage)); } + /** + * Tests that logging when the environment log level is disabled nothing is logged. + */ + @Test + public void logWhenLoggingNotSet() { + assertEquals(LogLevel.NOT_SET, LogLevel.fromString(null)); + } + /** * Tests that logging an exception when the log level isn't VERBOSE only log the exception message. */ From 0f201938f7ce8da27a27c8863a84f7fdc531abb5 Mon Sep 17 00:00:00 2001 From: Sima Zhu Date: Wed, 18 Dec 2019 15:56:18 -0800 Subject: [PATCH 22/29] Rename the tests --- .../java/com/azure/core/util/logging/ClientLoggerTests.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sdk/core/azure-core/src/test/java/com/azure/core/util/logging/ClientLoggerTests.java b/sdk/core/azure-core/src/test/java/com/azure/core/util/logging/ClientLoggerTests.java index 3df9c95197c9..f7794a541168 100644 --- a/sdk/core/azure-core/src/test/java/com/azure/core/util/logging/ClientLoggerTests.java +++ b/sdk/core/azure-core/src/test/java/com/azure/core/util/logging/ClientLoggerTests.java @@ -106,7 +106,7 @@ public void logAtUnsupportedLevel(int logLevel) { */ @ParameterizedTest(name = PARAMETERIZED_TEST_NAME_TEMPLATE) @ValueSource(ints = { 1, 2, 3, 4 }) - public void logWhenLoggingDisabled(int logLevel) { + public void logWhenLoggingInvalidNumeric(int logLevel) { String logMessage = "This is a test"; setupLogLevel(5); assertThrows(IllegalArgumentException.class, () -> From 18c16cb3bfd199eaf63e93558e86fbac78c81807 Mon Sep 17 00:00:00 2001 From: Sima Zhu Date: Fri, 20 Dec 2019 11:02:05 -0800 Subject: [PATCH 23/29] Some improvement --- .../java/com/azure/core/util/logging/LogLevel.java | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) 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 c6eff761b07c..223dba1385c0 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 @@ -3,8 +3,10 @@ package com.azure.core.util.logging; +import com.azure.core.util.Configuration; import java.util.HashMap; import java.util.Locale; +import java.util.Objects; /** * Enum which represent logging levels used in Azure SDKs. @@ -69,13 +71,14 @@ public int getLogLevel() { * @throws IllegalArgumentException if the log level value is invalid. */ public static LogLevel fromString(String logLevelVal) { - if (logLevelVal == null) { - return LogLevel.NOT_SET; - } - if (!LOG_LEVEL_STRING_MAPPER.containsKey(logLevelVal.toLowerCase(Locale.ROOT))) { + // logLevelVal cannot be null + Objects.requireNonNull(logLevelVal); + + String caseInsensitiveLogLevel = logLevelVal.toLowerCase(Locale.ROOT); + if (!LOG_LEVEL_STRING_MAPPER.containsKey(caseInsensitiveLogLevel)) { throw new IllegalArgumentException("We currently do not support the log level you set. LogLevel: " + logLevelVal); } - return LOG_LEVEL_STRING_MAPPER.get(logLevelVal.toLowerCase(Locale.ROOT)); + return LOG_LEVEL_STRING_MAPPER.get(caseInsensitiveLogLevel); } } From 950b4270a5c9864e4694f5c8d07b8529723e76b2 Mon Sep 17 00:00:00 2001 From: Sima Zhu Date: Fri, 20 Dec 2019 11:11:20 -0800 Subject: [PATCH 24/29] Remove the testing changes --- .../java/com/azure/core/util/logging/LogLevel.java | 14 ++++++-------- 1 file changed, 6 insertions(+), 8 deletions(-) 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 223dba1385c0..535df07fbb6f 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 @@ -3,10 +3,8 @@ package com.azure.core.util.logging; -import com.azure.core.util.Configuration; import java.util.HashMap; import java.util.Locale; -import java.util.Objects; /** * Enum which represent logging levels used in Azure SDKs. @@ -71,14 +69,14 @@ public int getLogLevel() { * @throws IllegalArgumentException if the log level value is invalid. */ public static LogLevel fromString(String logLevelVal) { - // logLevelVal cannot be null - Objects.requireNonNull(logLevelVal); - - String caseInsensitiveLogLevel = logLevelVal.toLowerCase(Locale.ROOT); - if (!LOG_LEVEL_STRING_MAPPER.containsKey(caseInsensitiveLogLevel)) { + if (logLevelVal == null) { + return LogLevel.NOT_SET; + } + String caseIncensitiveLogLevel = logLevelVal.toLowerCase(Locale.ROOT); + if (!LOG_LEVEL_STRING_MAPPER.containsKey(caseIncensitiveLogLevel)) { throw new IllegalArgumentException("We currently do not support the log level you set. LogLevel: " + logLevelVal); } - return LOG_LEVEL_STRING_MAPPER.get(caseInsensitiveLogLevel); + return LOG_LEVEL_STRING_MAPPER.get(caseIncensitiveLogLevel); } } From 2e35662ebf61c5bb463f23c4f9c290034fed7504 Mon Sep 17 00:00:00 2001 From: Sima Zhu Date: Fri, 20 Dec 2019 11:12:47 -0800 Subject: [PATCH 25/29] Fixed typo --- .../src/main/java/com/azure/core/util/logging/LogLevel.java | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) 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 535df07fbb6f..56e7775555b0 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 @@ -72,11 +72,11 @@ public static LogLevel fromString(String logLevelVal) { if (logLevelVal == null) { return LogLevel.NOT_SET; } - String caseIncensitiveLogLevel = logLevelVal.toLowerCase(Locale.ROOT); - if (!LOG_LEVEL_STRING_MAPPER.containsKey(caseIncensitiveLogLevel)) { + String caseInsensitiveLogLevel = logLevelVal.toLowerCase(Locale.ROOT); + if (!LOG_LEVEL_STRING_MAPPER.containsKey(caseInsensitiveLogLevel)) { throw new IllegalArgumentException("We currently do not support the log level you set. LogLevel: " + logLevelVal); } - return LOG_LEVEL_STRING_MAPPER.get(caseIncensitiveLogLevel); + return LOG_LEVEL_STRING_MAPPER.get(caseInsensitiveLogLevel); } } From da565b3fa801026354f48b5dc8e9d51a360ef862 Mon Sep 17 00:00:00 2001 From: Sima Zhu Date: Mon, 23 Dec 2019 10:36:11 -0800 Subject: [PATCH 26/29] added more description in javadoc --- .../src/main/java/com/azure/core/util/logging/LogLevel.java | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) 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 56e7775555b0..30fda2e794a1 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 @@ -65,7 +65,8 @@ 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. + * @return The LogLevel Enum if pass in the valid string listed in {@code allowedLogLevelVariables}. + * Returns NOT_SET if null is passed in. * @throws IllegalArgumentException if the log level value is invalid. */ public static LogLevel fromString(String logLevelVal) { From 81b0a341d3b493a6059d6b6d3a338344a22c0b98 Mon Sep 17 00:00:00 2001 From: Sima Zhu <48036328+sima-zhu@users.noreply.github.com> Date: Mon, 6 Jan 2020 12:14:44 -0800 Subject: [PATCH 27/29] Update LogLevel.java --- .../main/java/com/azure/core/util/logging/LogLevel.java | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) 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 30fda2e794a1..1fc26098a136 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 @@ -65,7 +65,12 @@ 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 listed in {@code allowedLogLevelVariables}. + * @return The LogLevel Enum if pass in the valid string. + * Valid strings for each log level are listing as below: + * VERBOSE ("verbose", "debug") + * INFORMATIONAL ("info", "information", "informational") + * WARNING("warn", "warning") + * ERROR("err", "error") * Returns NOT_SET if null is passed in. * @throws IllegalArgumentException if the log level value is invalid. */ From d00b87f63c331f4b601acc8d58a006eb263e546b Mon Sep 17 00:00:00 2001 From: Sima Zhu <48036328+sima-zhu@users.noreply.github.com> Date: Mon, 6 Jan 2020 12:32:29 -0800 Subject: [PATCH 28/29] Update LogLevel.java --- .../java/com/azure/core/util/logging/LogLevel.java | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) 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 1fc26098a136..83b603d17b1e 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 { /** @@ -66,11 +66,13 @@ public int getLogLevel() { * * @param logLevelVal The log level value which needs to convert * @return The LogLevel Enum if pass in the valid string. - * Valid strings for each log level are listing as below: - * VERBOSE ("verbose", "debug") - * INFORMATIONAL ("info", "information", "informational") - * WARNING("warn", "warning") - * ERROR("err", "error") + * The valid strings for {@link LogLevel} are: + *
    + *
  • VERBOSE: "verbose", "debug"
  • + *
  • INFO: "info", "information", "informational"
  • + *
  • WARNING: "warn", "warning"
  • + *
  • ERROR: "err", "error"
  • + *
* Returns NOT_SET if null is passed in. * @throws IllegalArgumentException if the log level value is invalid. */ From 21a51b424e753feef0d708eca81b5d8bb52b62be Mon Sep 17 00:00:00 2001 From: Sima Zhu <48036328+sima-zhu@users.noreply.github.com> Date: Mon, 6 Jan 2020 12:32:39 -0800 Subject: [PATCH 29/29] Update LogLevel.java