-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Added canLogAtLevel API in logging. #6886
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 24 commits
4e5cfa4
e6ad3c6
19c6422
9fc91ba
21f1273
b8104d4
210302c
6211513
d3b9fff
ec2c5b1
1399319
15edb3d
ceb458a
c3d2f5b
d441a04
f6ed817
12d82d9
9235b21
96b9133
a2862b7
5334113
1ac0104
13de5d8
dd01e2f
0f20193
18c16cb
950b427
2e35662
da565b3
81b0a34
d00b87f
21a51b4
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
This file was deleted.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,81 @@ | ||
| // Copyright (c) Microsoft Corporation. All rights reserved. | ||
| // Licensed under the MIT License. | ||
|
|
||
| package com.azure.core.util.logging; | ||
JonathanGiles marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| import java.util.HashMap; | ||
| import java.util.Locale; | ||
|
|
||
| /** | ||
| * Enum which represent logging levels used in Azure SDKs. | ||
| */ | ||
| public enum LogLevel { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. slf4j has distinct log levels for trace vs debug. Any reason we don't have equivalent of trace here?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We have a discussion here, as there are different LogLevel for different logging framework, there is no need to have 1 on 1 corresponding. We are having minimum set here which is enough for current use. We can add more enums if there is need in future. |
||
| /** | ||
| * Indicates that log level is at verbose level. | ||
| */ | ||
| VERBOSE(1, "1", "verbose", "debug"), | ||
|
|
||
| /** | ||
| * Indicates that log level is at information level. | ||
| */ | ||
| INFORMATIONAL(2, "2", "info", "information", "informational"), | ||
|
|
||
| /** | ||
| * Indicates that log level is at warning level. | ||
| */ | ||
| WARNING(3, "3", "warn", "warning"), | ||
|
|
||
| /** | ||
| * Indicates that log level is at error level. | ||
| */ | ||
| 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 final HashMap<String, LogLevel> LOG_LEVEL_STRING_MAPPER = new HashMap<>(); | ||
|
|
||
| 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; | ||
| } | ||
|
|
||
| /** | ||
| * Converts the log level into a numeric representation used for comparisons. | ||
| * | ||
| * @return The numeric representation of the log level. | ||
| */ | ||
| public int getLogLevel() { | ||
| return numericValue; | ||
| } | ||
|
|
||
| /** | ||
| * 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. | ||
| * @throws IllegalArgumentException if the log level value is invalid. | ||
| */ | ||
JonathanGiles marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| public static LogLevel fromString(String logLevelVal) { | ||
| if (logLevelVal == null) { | ||
sima-zhu marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| 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)); | ||
sima-zhu marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -9,13 +9,16 @@ | |
| import org.junit.jupiter.api.BeforeEach; | ||
| import org.junit.jupiter.api.Test; | ||
| import org.junit.jupiter.params.ParameterizedTest; | ||
| import org.junit.jupiter.params.provider.CsvSource; | ||
| 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.assertThrows; | ||
| import static org.junit.jupiter.api.Assertions.assertTrue; | ||
|
|
||
| /** | ||
|
|
@@ -105,13 +108,17 @@ public void logAtUnsupportedLevel(int logLevel) { | |
| @ValueSource(ints = { 1, 2, 3, 4 }) | ||
| public void logWhenLoggingDisabled(int logLevel) { | ||
|
||
| String logMessage = "This is a test"; | ||
| setupLogLevel(5); | ||
| assertThrows(IllegalArgumentException.class, () -> | ||
| logMessage(new ClientLogger(ClientLoggerTests.class), logLevel, logMessage)); | ||
| } | ||
|
|
||
| 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)); | ||
| /** | ||
| * Tests that logging when the environment log level is disabled nothing is logged. | ||
| */ | ||
| @Test | ||
| public void logWhenLoggingNotSet() { | ||
| assertEquals(LogLevel.NOT_SET, LogLevel.fromString(null)); | ||
| } | ||
|
|
||
| /** | ||
|
|
@@ -276,10 +283,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", "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; | ||
| } | ||
|
|
||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.