-
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 19 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 |
|---|---|---|
|
|
@@ -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; | ||
|
|
@@ -173,6 +172,17 @@ 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. | ||
| * @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); | ||
|
||
| } | ||
|
|
||
| /* | ||
| * Performs the logging. | ||
| * | ||
|
|
@@ -198,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.getLogLevel() > LogLevel.VERBOSE.getLogLevel()) { | ||
| args = removeThrowable(args); | ||
| } | ||
| } | ||
|
|
@@ -236,8 +246,13 @@ 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()) { | ||
| if (logLevel.getLogLevel() < environmentLoggingLevel.getLogLevel()) { | ||
| return false; | ||
| } | ||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,77 @@ | ||
| // 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.Arrays; | ||
| 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. | ||
| */ | ||
| 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", "verbose"), | ||
|
|
||
| /** | ||
| * Indicates that log level is at information level. | ||
| */ | ||
| INFORMATIONAL("2", "info", "information", "informational"), | ||
|
|
||
| /** | ||
| * Indicates that log level is at warning level. | ||
| */ | ||
| WARNING("3", "warn", "warning"), | ||
|
|
||
| /** | ||
| * Indicates that log level is at error level. | ||
| */ | ||
| ERROR("4", "err", "error"); | ||
|
|
||
| private final String[] allowedLogLevelVariables; | ||
|
|
||
| LogLevel(String... allowedLogLevelVariables) { | ||
| 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 Integer.parseInt(allowedLogLevelVariables[0]); | ||
sima-zhu marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| } | ||
|
|
||
| /** | ||
| * Converts the log level into string representations used for comparisons. | ||
| * | ||
| * @return The string representations of the log level. | ||
| */ | ||
| private String[] getAllowedLogLevels() { | ||
sima-zhu marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| return allowedLogLevelVariables; | ||
| } | ||
|
|
||
| private static final Map<String, LogLevel> 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}. | ||
| * | ||
| * @param logLevelVal The log level value which needs to convert | ||
| * @return The LogLevel Enum. | ||
| */ | ||
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 null; | ||
| } | ||
| return LOG_LEVEL_STRING_MAPPER.get(logLevelVal.toLowerCase(Locale.ROOT)); | ||
sima-zhu marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| } | ||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.