-
Notifications
You must be signed in to change notification settings - Fork 31
Allow clients to provide a custom logger instance #159
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
Open
mbarta
wants to merge
18
commits into
main
Choose a base branch
from
mb/custom_logger
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 11 commits
Commits
Show all changes
18 commits
Select commit
Hold shift + click to select a range
1eb8deb
Extract HotwireLogger and DefaultHotwireLogger
mbarta dc7cff4
Allow setting logger through Hotwire config
mbarta d6eabbc
Refactor existing logging to use HotwireLogger
mbarta 1aad44f
Fix copy-paste error
mbarta 501a047
Allow passing throwable in error log
mbarta 63823d1
Clarify logger documentation in HotwireConfig
mbarta 03b954f
Add custom HTTP logger for OkHttp interceptor
mbarta 896cc48
Always add a loggingInterceptor and let logger decide
mbarta 18a5cfb
Optimise imports
mbarta 4ba61d1
Drop debugLoggingEnabled flag
mbarta 957e657
Update comment for logger config
mbarta e78df38
Add verbose and info log methods to HotwireLogger
mbarta 496377a
Use string interpolation for padded event logging
mbarta f1ae753
Refactor logging to use lambda message providers
mbarta 6b159a4
Update logger documentation
mbarta 45539c0
Merge branch 'main' into mb/custom_logger
jayohms 6f362bf
Introduce a HotwireLogLevel so we don't have to make assumptions abou…
jayohms a23ad2b
Rename logEvent() -> logDebug() to match other internal logging funct…
jayohms 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
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 |
|---|---|---|
| @@ -1,45 +1,32 @@ | ||
| package dev.hotwire.core.logging | ||
|
|
||
| import android.util.Log | ||
| import dev.hotwire.core.config.Hotwire | ||
| import okhttp3.logging.HttpLoggingInterceptor | ||
|
|
||
| internal object CoreLog { | ||
| private const val DEFAULT_TAG = "Hotwire-Core" | ||
|
|
||
| private val debugEnabled get() = Hotwire.config.debugLoggingEnabled | ||
|
|
||
| internal fun d(msg: String) = log(Log.DEBUG, msg) | ||
|
|
||
| internal fun w(msg: String) = log(Log.WARN, msg) | ||
|
|
||
| internal fun e(msg: String) = log(Log.ERROR, msg) | ||
| private const val DEFAULT_TAG = "Hotwire-Core" | ||
| private const val PAD_END_LENGTH = 35 | ||
|
|
||
| private fun log(logLevel: Int, msg: String) { | ||
| when (logLevel) { | ||
| Log.DEBUG -> if (debugEnabled) Log.d(DEFAULT_TAG, msg) | ||
| Log.WARN -> Log.w(DEFAULT_TAG, msg) | ||
| Log.ERROR -> Log.e(DEFAULT_TAG, msg) | ||
| } | ||
| internal class HotwireHttpLogger : HttpLoggingInterceptor.Logger { | ||
| override fun log(message: String) { | ||
| Hotwire.config.logger.d(DEFAULT_TAG, message) | ||
| } | ||
| } | ||
|
|
||
| private const val PAD_END_LENGTH = 35 | ||
|
|
||
| internal fun logEvent(event: String, details: String = "") { | ||
| CoreLog.d("$event ".padEnd(PAD_END_LENGTH, '.') + " [$details]") | ||
| Hotwire.config.logger.d(DEFAULT_TAG, "$event ".padEnd(PAD_END_LENGTH, '.') + " [$details]") | ||
mbarta marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| } | ||
|
|
||
| internal fun logEvent(event: String, attributes: List<Pair<String, Any>>) { | ||
| val description = attributes.joinToString(prefix = "[", postfix = "]", separator = ", ") { | ||
| "${it.first}: ${it.second}" | ||
| } | ||
| CoreLog.d("$event ".padEnd(PAD_END_LENGTH, '.') + " $description") | ||
| Hotwire.config.logger.d(DEFAULT_TAG, "$event ".padEnd(PAD_END_LENGTH, '.') + " $description") | ||
| } | ||
|
|
||
| internal fun logWarning(event: String, details: String) { | ||
| CoreLog.w("$event ".padEnd(PAD_END_LENGTH, '.') + " [$details]") | ||
| Hotwire.config.logger.w(DEFAULT_TAG, "$event ".padEnd(PAD_END_LENGTH, '.') + " [$details]") | ||
| } | ||
|
|
||
| internal fun logError(event: String, error: Exception) { | ||
| CoreLog.e("$event: ${error.stackTraceToString()}") | ||
| } | ||
| internal fun logError(event: String, throwable: Throwable) { | ||
| Hotwire.config.logger.e(DEFAULT_TAG, "$event: ${throwable.stackTraceToString()}", throwable) | ||
| } | ||
25 changes: 25 additions & 0 deletions
25
core/src/main/kotlin/dev/hotwire/core/logging/DefaultHotwireLogger.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,25 @@ | ||
| package dev.hotwire.core.logging | ||
|
|
||
| import android.util.Log | ||
| import dev.hotwire.core.BuildConfig | ||
|
|
||
| internal object DefaultHotwireLogger : HotwireLogger { | ||
| private val debugEnabled get() = BuildConfig.DEBUG | ||
jayohms marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| override fun d(tag: String, msg: String) = | ||
| log(Log.DEBUG, tag, msg) | ||
|
|
||
| override fun w(tag: String, msg: String) = | ||
| log(Log.WARN, tag, msg) | ||
|
|
||
| override fun e(tag: String, msg: String, throwable: Throwable?) = | ||
| log(Log.ERROR, tag, msg, throwable) | ||
|
|
||
| private fun log(logLevel: Int, tag: String, msg: String, throwable: Throwable? = null) { | ||
| when (logLevel) { | ||
| Log.DEBUG -> if (debugEnabled) Log.d(tag, msg) | ||
| Log.WARN -> Log.w(tag, msg) | ||
| Log.ERROR -> Log.e(tag, msg, throwable) | ||
jayohms marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| } | ||
| } | ||
| } | ||
7 changes: 7 additions & 0 deletions
7
core/src/main/kotlin/dev/hotwire/core/logging/HotwireLogger.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,7 @@ | ||
| package dev.hotwire.core.logging | ||
|
|
||
| interface HotwireLogger { | ||
mbarta marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| fun d(tag: String, msg: String) | ||
| fun w(tag: String, msg: String) | ||
| fun e(tag: String, msg: String, throwable: Throwable? = null) | ||
mbarta marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| } | ||
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
7 changes: 0 additions & 7 deletions
7
navigation-fragments/src/main/java/dev/hotwire/navigation/destinations/HotwireDestination.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
2 changes: 1 addition & 1 deletion
2
...ation-fragments/src/main/java/dev/hotwire/navigation/fragments/HotwireFragmentDelegate.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
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
32 changes: 6 additions & 26 deletions
32
navigation-fragments/src/main/java/dev/hotwire/navigation/logging/NavigationLog.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 |
|---|---|---|
| @@ -1,45 +1,25 @@ | ||
| package dev.hotwire.navigation.logging | ||
|
|
||
| import android.util.Log | ||
| import dev.hotwire.core.config.Hotwire | ||
|
|
||
| internal object NavigationLog { | ||
| private const val DEFAULT_TAG = "Hotwire-Navigation" | ||
|
|
||
| private val debugEnabled get() = Hotwire.config.debugLoggingEnabled | ||
|
|
||
| internal fun d(msg: String) = log(Log.DEBUG, msg) | ||
|
|
||
| internal fun w(msg: String) = log(Log.WARN, msg) | ||
|
|
||
| internal fun e(msg: String) = log(Log.ERROR, msg) | ||
|
|
||
| private fun log(logLevel: Int, msg: String) { | ||
| when (logLevel) { | ||
| Log.DEBUG -> if (debugEnabled) Log.d(DEFAULT_TAG, msg) | ||
| Log.WARN -> Log.w(DEFAULT_TAG, msg) | ||
| Log.ERROR -> Log.e(DEFAULT_TAG, msg) | ||
| } | ||
| } | ||
| } | ||
|
|
||
| private const val DEFAULT_TAG = "Hotwire-Navigation" | ||
| private const val PAD_END_LENGTH = 35 | ||
|
|
||
| internal fun logEvent(event: String, details: String = "") { | ||
| NavigationLog.d("$event ".padEnd(PAD_END_LENGTH, '.') + " [$details]") | ||
| Hotwire.config.logger.d(DEFAULT_TAG, "$event ".padEnd(PAD_END_LENGTH, '.') + " [$details]") | ||
| } | ||
|
|
||
| internal fun logEvent(event: String, attributes: List<Pair<String, Any>>) { | ||
| val description = attributes.joinToString(prefix = "[", postfix = "]", separator = ", ") { | ||
| "${it.first}: ${it.second}" | ||
| } | ||
| NavigationLog.d("$event ".padEnd(PAD_END_LENGTH, '.') + " $description") | ||
| Hotwire.config.logger.d(DEFAULT_TAG, "$event ".padEnd(PAD_END_LENGTH, '.') + " $description") | ||
| } | ||
|
|
||
| internal fun logWarning(event: String, details: String) { | ||
| NavigationLog.w("$event ".padEnd(PAD_END_LENGTH, '.') + " [$details]") | ||
| Hotwire.config.logger.w(DEFAULT_TAG, "$event ".padEnd(PAD_END_LENGTH, '.') + " [$details]") | ||
| } | ||
|
|
||
| internal fun logError(event: String, error: Exception) { | ||
| NavigationLog.e("$event: ${error.stackTraceToString()}") | ||
| internal fun logError(event: String, throwable: Throwable) { | ||
| Hotwire.config.logger.e(DEFAULT_TAG, "$event: ${throwable.stackTraceToString()}", throwable) | ||
| } |
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
2 changes: 0 additions & 2 deletions
2
navigation-fragments/src/test/kotlin/dev/hotwire/navigation/navigator/NavigatorHostTest.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.