-
Notifications
You must be signed in to change notification settings - Fork 320
Lazy logs #6439
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
Merged
Merged
Lazy logs #6439
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
264ce4e
introduced lazy logging
VysotskiVadim 9858d51
made log inline
VysotskiVadim 773484f
fixed styles + added lazy messages to all log options
VysotskiVadim 60c5793
fixed tests
VysotskiVadim 5a5c55c
clarified docs
VysotskiVadim d138292
fixed docs
VysotskiVadim 0a76da4
fixed getting of log level from common
VysotskiVadim File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2,6 +2,7 @@ package com.mapbox.navigation.utils.internal | |
|
|
||
| import androidx.annotation.VisibleForTesting | ||
| import com.mapbox.base.common.logger.Logger | ||
| import com.mapbox.common.LoggingLevel | ||
|
|
||
| /** | ||
| * Singleton provider of [Logger]. | ||
|
|
@@ -39,6 +40,18 @@ fun logD(msg: String, category: String? = null) { | |
| LoggerProvider.frontend.logD(msg, category) | ||
| } | ||
|
|
||
| /** | ||
| * @param category optional string to identify the source or category of the log message. | ||
| * @param lazyMsg is a lazy message to log. The lazy message isn't executed if current log level is less verbose than Debug. | ||
| * Noting that the category is appended to the log message to give extra context along with the `[nav-sdk]` parent category. | ||
| * As an example, this is how the logs would look like `D/Mapbox: [nav-sdk] [ConnectivityHandler] NetworkStatus=ReachableViaWiFi`. | ||
| */ | ||
| inline fun logD(category: String? = null, lazyMsg: () -> String) { | ||
| if (logLevel().atLeast(LoggingLevel.DEBUG)) { | ||
| logD(lazyMsg(), category) | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * @param msg to log. | ||
| * @param category optional string to identify the source or category of the log message. | ||
|
|
@@ -49,6 +62,18 @@ fun logI(msg: String, category: String? = null) { | |
| LoggerProvider.frontend.logI(msg, category) | ||
| } | ||
|
|
||
| /** | ||
| * @param category optional string to identify the source or category of the log message. | ||
| * @param lazyMsg is a lazy message to log. The lazy message isn't executed if current log level is less verbose than Info. | ||
| * Noting that the category is appended to the log message to give extra context along with the `[nav-sdk]` parent category. | ||
| * As an example, this is how the logs would look like `I/Mapbox: [nav-sdk] [ConnectivityHandler] NetworkStatus=ReachableViaWiFi`. | ||
| */ | ||
| inline fun logI(category: String? = null, lazyMsg: () -> String) { | ||
| if (logLevel().atLeast(LoggingLevel.INFO)) { | ||
| logI(lazyMsg(), category) | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * @param msg to log. | ||
| * @param category optional string to identify the source or category of the log message. | ||
|
|
@@ -59,6 +84,18 @@ fun logW(msg: String, category: String? = null) { | |
| LoggerProvider.frontend.logW(msg, category) | ||
| } | ||
|
|
||
| /** | ||
| * @param category optional string to identify the source or category of the log message. | ||
| * @param lazyMsg is a lazy message to log. The lazy message isn't executed if current log level is less verbose than Warning. | ||
| * Noting that the category is appended to the log message to give extra context along with the `[nav-sdk]` parent category. | ||
| * As an example, this is how the logs would look like `W/Mapbox: [nav-sdk] [ConnectivityHandler] NetworkStatus=ReachableViaWiFi`. | ||
| */ | ||
| inline fun logW(category: String? = null, lazyMsg: () -> String) { | ||
| if (logLevel().atLeast(LoggingLevel.WARNING)) { | ||
| logW(lazyMsg(), category) | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * @param msg to log. | ||
| * @param category optional string to identify the source or category of the log message. | ||
|
|
@@ -68,3 +105,21 @@ fun logW(msg: String, category: String? = null) { | |
| fun logE(msg: String, category: String? = null) { | ||
| LoggerProvider.frontend.logE(msg, category) | ||
| } | ||
|
|
||
| /** | ||
| * @param category optional string to identify the source or category of the log message. | ||
| * @param lazyMsg is a lazy message to log. The lazy message isn't executed if current log level is less verbose than Error. | ||
| * Noting that the category is appended to the log message to give extra context along with the `[nav-sdk]` parent category. | ||
| * As an example, this is how the logs would look like `E/Mapbox: [nav-sdk] [ConnectivityHandler] NetworkStatus=ReachableViaWiFi`. | ||
| */ | ||
| inline fun logE(category: String? = null, lazyMsg: () -> String) { | ||
| if (logLevel().atLeast(LoggingLevel.ERROR)) { | ||
| logE(lazyMsg(), category) | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Should not be used directly. | ||
| * Added to support inline calls. Public inline functions can use only public API inside. | ||
| */ | ||
| fun logLevel() = LoggerProvider.frontend.getLogLevel() | ||
|
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. By the way, you can make it
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. Amazing! ❤️ |
||
15 changes: 15 additions & 0 deletions
15
libnavigation-util/src/main/java/com/mapbox/navigation/utils/internal/LoggingLevelUtil.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,15 @@ | ||
| package com.mapbox.navigation.utils.internal | ||
|
|
||
| import com.mapbox.common.LoggingLevel | ||
|
|
||
| fun LoggingLevel?.atLeast(loggingLevel: LoggingLevel): Boolean { | ||
| return toPriority(this) >= toPriority(loggingLevel) | ||
| } | ||
|
|
||
| private fun toPriority(loggingLevel: LoggingLevel?) = when (loggingLevel) { | ||
| null -> 0 | ||
| LoggingLevel.DEBUG -> 1 | ||
| LoggingLevel.INFO -> 2 | ||
| LoggingLevel.WARNING -> 3 | ||
| LoggingLevel.ERROR -> 4 | ||
| } |
135 changes: 135 additions & 0 deletions
135
...igation-util/src/test/java/com/mapbox/navigation/utils/internal/LoggingLevelUtilKtTest.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,135 @@ | ||
| package com.mapbox.navigation.utils.internal | ||
|
|
||
| import com.mapbox.common.LoggingLevel | ||
| import org.junit.Assert.assertEquals | ||
| import org.junit.Test | ||
| import org.junit.runner.RunWith | ||
| import org.junit.runners.Parameterized | ||
|
|
||
| @RunWith(Parameterized::class) | ||
| class LoggingLevelUtilKtTest(val parameter: LogLevelData) { | ||
|
|
||
| companion object { | ||
|
|
||
| @JvmStatic | ||
| @Parameterized.Parameters(name = "{0}") | ||
| fun parameters() = listOf( | ||
| LogLevelData( | ||
| what = null, | ||
| atLeast = LoggingLevel.ERROR, | ||
| expected = false | ||
| ), | ||
| LogLevelData( | ||
| what = null, | ||
| atLeast = LoggingLevel.WARNING, | ||
| expected = false | ||
| ), | ||
| LogLevelData( | ||
| what = null, | ||
| atLeast = LoggingLevel.INFO, | ||
| expected = false | ||
| ), | ||
| LogLevelData( | ||
| what = null, | ||
| atLeast = LoggingLevel.DEBUG, | ||
| expected = false | ||
| ), | ||
|
|
||
| LogLevelData( | ||
| what = LoggingLevel.ERROR, | ||
| atLeast = LoggingLevel.ERROR, | ||
| expected = true | ||
| ), | ||
| LogLevelData( | ||
| what = LoggingLevel.ERROR, | ||
| atLeast = LoggingLevel.WARNING, | ||
| expected = true | ||
| ), | ||
| LogLevelData( | ||
| what = LoggingLevel.ERROR, | ||
| atLeast = LoggingLevel.INFO, | ||
| expected = true | ||
| ), | ||
| LogLevelData( | ||
| what = LoggingLevel.ERROR, | ||
| atLeast = LoggingLevel.DEBUG, | ||
| expected = true | ||
| ), | ||
|
|
||
| LogLevelData( | ||
| what = LoggingLevel.WARNING, | ||
| atLeast = LoggingLevel.ERROR, | ||
| expected = false | ||
| ), | ||
| LogLevelData( | ||
| what = LoggingLevel.WARNING, | ||
| atLeast = LoggingLevel.WARNING, | ||
| expected = true | ||
| ), | ||
| LogLevelData( | ||
| what = LoggingLevel.WARNING, | ||
| atLeast = LoggingLevel.INFO, | ||
| expected = true | ||
| ), | ||
| LogLevelData( | ||
| what = LoggingLevel.WARNING, | ||
| atLeast = LoggingLevel.DEBUG, | ||
| expected = true | ||
| ), | ||
|
|
||
| LogLevelData( | ||
| what = LoggingLevel.INFO, | ||
| atLeast = LoggingLevel.ERROR, | ||
| expected = false | ||
| ), | ||
| LogLevelData( | ||
| what = LoggingLevel.INFO, | ||
| atLeast = LoggingLevel.WARNING, | ||
| expected = false | ||
| ), | ||
| LogLevelData( | ||
| what = LoggingLevel.INFO, | ||
| atLeast = LoggingLevel.INFO, | ||
| expected = true | ||
| ), | ||
| LogLevelData( | ||
| what = LoggingLevel.INFO, | ||
| atLeast = LoggingLevel.DEBUG, | ||
| expected = true | ||
| ), | ||
|
|
||
| LogLevelData( | ||
| what = LoggingLevel.DEBUG, | ||
| atLeast = LoggingLevel.ERROR, | ||
| expected = false | ||
| ), | ||
| LogLevelData( | ||
| what = LoggingLevel.DEBUG, | ||
| atLeast = LoggingLevel.WARNING, | ||
| expected = false | ||
| ), | ||
| LogLevelData( | ||
| what = LoggingLevel.DEBUG, | ||
| atLeast = LoggingLevel.INFO, | ||
| expected = false | ||
| ), | ||
| LogLevelData( | ||
| what = LoggingLevel.DEBUG, | ||
| atLeast = LoggingLevel.DEBUG, | ||
| expected = true | ||
| ), | ||
| ) | ||
| } | ||
|
|
||
| @Test | ||
| fun `test atLeast`() { | ||
| val result = parameter.what.atLeast(parameter.atLeast) | ||
| assertEquals(parameter.expected, result) | ||
| } | ||
|
|
||
| data class LogLevelData( | ||
| val what: LoggingLevel?, | ||
| val atLeast: LoggingLevel, | ||
| val expected: Boolean | ||
| ) | ||
| } |
4 changes: 4 additions & 0 deletions
4
...ng-navigation-util/src/main/java/com/mapbox/navigation/testing/LoggingFrontendTestRule.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.