-
Notifications
You must be signed in to change notification settings - Fork 0
Allow clients to configure logging #69
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
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
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
37 changes: 37 additions & 0 deletions
37
...-client/src/main/kotlin/io/getstream/feeds/android/client/api/logging/HttpLoggingLevel.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,37 @@ | ||
/* | ||
* Copyright (c) 2014-2025 Stream.io Inc. All rights reserved. | ||
* | ||
* Licensed under the Stream License; | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* https://github.com/GetStream/stream-feeds-android/blob/main/LICENSE | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package io.getstream.feeds.android.client.api.logging | ||
|
||
/** | ||
* Represents the logging levels for HTTP requests and responses. | ||
* - None: No logging. | ||
* - Basic: Logs request and response lines. | ||
* - Headers: Logs request and response lines along with their respective headers. | ||
* - Body: Logs request and response lines, headers, and bodies (if present). | ||
*/ | ||
public enum class HttpLoggingLevel { | ||
/** No logging. */ | ||
None, | ||
|
||
/** Logs request and response lines. */ | ||
Basic, | ||
|
||
/** Logs request and response lines along with their respective headers. */ | ||
Headers, | ||
|
||
/** Logs request and response lines, headers, and bodies (if present). */ | ||
Body, | ||
} |
37 changes: 37 additions & 0 deletions
37
...ds-android-client/src/main/kotlin/io/getstream/feeds/android/client/api/logging/Logger.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,37 @@ | ||
/* | ||
* Copyright (c) 2014-2025 Stream.io Inc. All rights reserved. | ||
* | ||
* Licensed under the Stream License; | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* https://github.com/GetStream/stream-feeds-android/blob/main/LICENSE | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package io.getstream.feeds.android.client.api.logging | ||
|
||
/** A logger interface for logging messages at different levels. */ | ||
public interface Logger { | ||
/** | ||
* Logs a message at the specified level with an optional throwable. | ||
* | ||
* @param level The log level. | ||
* @param tag A tag to identify the source of the log message. | ||
* @param throwable An optional throwable associated with the log message. | ||
* @param message A lambda that produces the log message. | ||
*/ | ||
public fun log(level: Level, tag: String, throwable: Throwable? = null, message: () -> String) | ||
|
||
public enum class Level { | ||
Verbose, | ||
Debug, | ||
Info, | ||
Warning, | ||
Error, | ||
} | ||
} |
29 changes: 29 additions & 0 deletions
29
...oid-client/src/main/kotlin/io/getstream/feeds/android/client/api/logging/LoggingConfig.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,29 @@ | ||
/* | ||
* Copyright (c) 2014-2025 Stream.io Inc. All rights reserved. | ||
* | ||
* Licensed under the Stream License; | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* https://github.com/GetStream/stream-feeds-android/blob/main/LICENSE | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package io.getstream.feeds.android.client.api.logging | ||
|
||
/** | ||
* Configuration for logging within the FeedsClient. | ||
* | ||
* @property customLogger An optional custom logger implementation. If not provided, a default | ||
* logger will be used. | ||
* @property httpLoggingLevel What level of HTTP logging to use. Defaults to | ||
* [HttpLoggingLevel.Basic]. | ||
*/ | ||
public class LoggingConfig( | ||
public val customLogger: Logger? = null, | ||
public val httpLoggingLevel: HttpLoggingLevel = HttpLoggingLevel.Basic, | ||
) |
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
40 changes: 40 additions & 0 deletions
40
...src/main/kotlin/io/getstream/feeds/android/client/internal/logging/FeedsLoggerProvider.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,40 @@ | ||
/* | ||
* Copyright (c) 2014-2025 Stream.io Inc. All rights reserved. | ||
* | ||
* Licensed under the Stream License; | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* https://github.com/GetStream/stream-feeds-android/blob/main/LICENSE | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package io.getstream.feeds.android.client.internal.logging | ||
|
||
import io.getstream.android.core.api.log.StreamLogger | ||
import io.getstream.android.core.api.log.StreamLogger.LogLevel as CoreLogLevel | ||
import io.getstream.android.core.api.log.StreamLoggerProvider | ||
import io.getstream.feeds.android.client.api.logging.Logger | ||
|
||
internal class FeedsLoggerProvider(private val customLogger: Logger) : StreamLoggerProvider { | ||
override fun taggedLogger(tag: String): StreamLogger = FeedsTaggedLogger(customLogger, tag) | ||
} | ||
|
||
private class FeedsTaggedLogger(private val customLogger: Logger, private val tag: String) : | ||
StreamLogger { | ||
override fun log(level: CoreLogLevel, throwable: Throwable?, message: () -> String) = | ||
customLogger.log(level.map(), tag, throwable, message) | ||
} | ||
|
||
private fun CoreLogLevel.map(): Logger.Level = | ||
when (this) { | ||
CoreLogLevel.Verbose -> Logger.Level.Verbose | ||
CoreLogLevel.Debug -> Logger.Level.Debug | ||
CoreLogLevel.Info -> Logger.Level.Info | ||
CoreLogLevel.Warning -> Logger.Level.Warning | ||
CoreLogLevel.Error -> Logger.Level.Error | ||
} |
42 changes: 42 additions & 0 deletions
42
...ient/src/main/kotlin/io/getstream/feeds/android/client/internal/logging/LoggingFactory.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,42 @@ | ||
/* | ||
* Copyright (c) 2014-2025 Stream.io Inc. All rights reserved. | ||
* | ||
* Licensed under the Stream License; | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* https://github.com/GetStream/stream-feeds-android/blob/main/LICENSE | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package io.getstream.feeds.android.client.internal.logging | ||
|
||
import io.getstream.android.core.api.log.StreamLoggerProvider | ||
import io.getstream.feeds.android.client.api.logging.HttpLoggingLevel | ||
import io.getstream.feeds.android.client.api.logging.Logger | ||
import okhttp3.Interceptor | ||
import okhttp3.logging.HttpLoggingInterceptor | ||
|
||
internal fun createLoggerProvider(customLogger: Logger?): StreamLoggerProvider = | ||
customLogger?.let(::FeedsLoggerProvider) ?: StreamLoggerProvider.defaultAndroidLogger() | ||
|
||
internal fun createLoggingInterceptor( | ||
provider: StreamLoggerProvider, | ||
level: HttpLoggingLevel, | ||
): Interceptor { | ||
val logger = provider.taggedLogger("FeedHTTP") | ||
|
||
return HttpLoggingInterceptor(logger = { logger.i { it } }).setLevel(level.toOkHttpLevel()) | ||
} | ||
|
||
private fun HttpLoggingLevel.toOkHttpLevel(): HttpLoggingInterceptor.Level = | ||
when (this) { | ||
HttpLoggingLevel.None -> HttpLoggingInterceptor.Level.NONE | ||
HttpLoggingLevel.Basic -> HttpLoggingInterceptor.Level.BASIC | ||
HttpLoggingLevel.Headers -> HttpLoggingInterceptor.Level.HEADERS | ||
HttpLoggingLevel.Body -> HttpLoggingInterceptor.Level.BODY | ||
} |
61 changes: 61 additions & 0 deletions
61
...est/kotlin/io/getstream/feeds/android/client/internal/logging/CustomLoggerProviderTest.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,61 @@ | ||
/* | ||
* Copyright (c) 2014-2025 Stream.io Inc. All rights reserved. | ||
* | ||
* Licensed under the Stream License; | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* https://github.com/GetStream/stream-feeds-android/blob/main/LICENSE | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package io.getstream.feeds.android.client.internal.logging | ||
|
||
import io.getstream.android.core.api.log.StreamLogger | ||
import io.getstream.feeds.android.client.api.logging.Logger | ||
import io.mockk.mockk | ||
import io.mockk.verify | ||
import org.junit.Test | ||
import org.junit.runner.RunWith | ||
import org.junit.runners.Parameterized | ||
|
||
@RunWith(Parameterized::class) | ||
internal class CustomLoggerProviderTest( | ||
private val logLevel: StreamLogger.LogLevel, | ||
private val expectedLevel: Logger.Level, | ||
) { | ||
@Test | ||
fun `on taggedLogger, return a logger that forwards the tag`() { | ||
val customLogger = mockk<Logger>(relaxed = true) | ||
val taggedLogger = FeedsLoggerProvider(customLogger).taggedLogger("a tag") | ||
val exception = Exception("an exception") | ||
|
||
taggedLogger.log(logLevel, exception) { "a message" } | ||
|
||
verify { | ||
customLogger.log( | ||
level = expectedLevel, | ||
tag = "a tag", | ||
throwable = exception, | ||
message = match { it() == "a message" }, | ||
) | ||
} | ||
} | ||
|
||
companion object { | ||
@JvmStatic | ||
@Parameterized.Parameters | ||
fun data(): Collection<Array<Any>> = | ||
listOf( | ||
arrayOf(StreamLogger.LogLevel.Verbose, Logger.Level.Verbose), | ||
arrayOf(StreamLogger.LogLevel.Debug, Logger.Level.Debug), | ||
arrayOf(StreamLogger.LogLevel.Info, Logger.Level.Info), | ||
arrayOf(StreamLogger.LogLevel.Warning, Logger.Level.Warning), | ||
arrayOf(StreamLogger.LogLevel.Error, Logger.Level.Error), | ||
) | ||
} | ||
} |
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.