-
Notifications
You must be signed in to change notification settings - Fork 46
Ktor Extension #458
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
KevinSchildhorn
wants to merge
10
commits into
main
Choose a base branch
from
ks/KtorExtension
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
Ktor Extension #458
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
b1d684c
Adding initial extension
KevinSchildhorn fb1a9f5
updating call and readme
KevinSchildhorn 5227521
updating API
KevinSchildhorn 4cfb2a6
Updating readme and website
KevinSchildhorn 0887c9b
Adding Ktor Logger to sample
KevinSchildhorn 89ed118
Update website/docs/extensions/KTOR.md
KevinSchildhorn aa30a29
Update build_mac.yml
KevinSchildhorn 9357b3d
Update build_mac.yml
KevinSchildhorn 8297cf4
Revert "Update build_mac.yml"
KevinSchildhorn 7c2c50c
Merge branch 'main' into ks/KtorExtension
KevinSchildhorn 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,37 @@ | ||
| # Kermit-Ktor Integration | ||
|
|
||
| The Kermit-Ktor module provides a way to add multiplatform logging to your Ktor client. | ||
|
|
||
| Firstly, add the gradle dependency to your project. | ||
|
|
||
| ```kotlin | ||
| sourceSets { | ||
| commonMain { | ||
| dependencies { | ||
| implementation("co.touchlab:kermit-ktor:x.y.z") // Add the latest version | ||
| } | ||
| } | ||
| } | ||
| ``` | ||
|
|
||
| Then add the Kermit logger when you create your Ktor client - | ||
|
|
||
| ```kotlin | ||
| val httpClient = HttpClient { | ||
| install(Logging) { | ||
| logger = KermitKtorLogger(severity = Severity.Info, logger = KermitLogger) | ||
| level = LogLevel.INFO | ||
| } | ||
| } | ||
| ``` | ||
|
|
||
| or alternatively | ||
|
|
||
| ```kotlin | ||
| val httpClient = HttpClient { | ||
| install(Logging) { | ||
| logger = KermitKtorLogger(severity = Severity.Info, config = loggerConfigInit(CommonWriter()), tag = "") | ||
| level = LogLevel.INFO | ||
| } | ||
| } | ||
| ``` |
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 @@ | ||
| public final class co/touchlab/kermit/ktor/KermitKtorLogger : io/ktor/client/plugins/logging/Logger { | ||
| public fun <init> (Lco/touchlab/kermit/Severity;Lco/touchlab/kermit/Logger;)V | ||
| public fun <init> (Lco/touchlab/kermit/Severity;Lco/touchlab/kermit/LoggerConfig;Ljava/lang/String;)V | ||
| public synthetic fun <init> (Lco/touchlab/kermit/Severity;Lco/touchlab/kermit/LoggerConfig;Ljava/lang/String;ILkotlin/jvm/internal/DefaultConstructorMarker;)V | ||
| public fun log (Ljava/lang/String;)V | ||
| } | ||
|
|
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,57 @@ | ||
| /* | ||
| * Copyright (c) 2025 Touchlab | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except | ||
| * in compliance with the License. You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * 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. | ||
| */ | ||
|
|
||
| plugins { | ||
| alias(libs.plugins.android.library) | ||
| alias(libs.plugins.kotlin.multiplatform) | ||
| id("kermit-jvm-target") | ||
| id("kermit-publish") | ||
| } | ||
|
|
||
| kotlin { | ||
| androidTarget { | ||
| publishAllLibraryVariants() | ||
| } | ||
| macosX64() | ||
| macosArm64() | ||
| iosX64() | ||
| iosArm64() | ||
| iosSimulatorArm64() | ||
| tvosArm64() | ||
| tvosSimulatorArm64() | ||
| tvosX64() | ||
| watchosArm32() | ||
| watchosArm64() | ||
| watchosSimulatorArm64() | ||
| watchosDeviceArm64() | ||
| watchosX64() | ||
|
|
||
| sourceSets { | ||
| commonMain.dependencies { | ||
| implementation(libs.ktor.logging) | ||
| implementation(project(":kermit")) | ||
| } | ||
| } | ||
| } | ||
|
|
||
| android { | ||
| namespace = "co.touchlab.kermit.ktor" | ||
| compileSdk = libs.versions.compileSdk.get().toInt() | ||
| defaultConfig { | ||
| minSdk = libs.versions.minSdk.get().toInt() | ||
| } | ||
| compileOptions { | ||
| sourceCompatibility = JavaVersion.VERSION_1_8 | ||
| targetCompatibility = JavaVersion.VERSION_1_8 | ||
| } | ||
| } | ||
39 changes: 39 additions & 0 deletions
39
extensions/kermit-ktor/src/commonMain/kotlin/co/touchlab/kermit/ktor/KermitKtorLogger.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,39 @@ | ||
| /* | ||
| * Copyright (c) 2025 Touchlab | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * 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 co.touchlab.kermit.ktor | ||
|
|
||
| import co.touchlab.kermit.Logger as KermitLogger | ||
| import co.touchlab.kermit.LoggerConfig | ||
| import co.touchlab.kermit.Severity | ||
| import io.ktor.client.plugins.logging.Logger as KtorLogger | ||
|
|
||
| class KermitKtorLogger(private val severity: Severity, private val logger: KermitLogger) : KtorLogger { | ||
|
|
||
| constructor( | ||
| severity: Severity, | ||
| config: LoggerConfig, | ||
| tag: String = "", | ||
| ) : this( | ||
| logger = KermitLogger(config = config, tag = tag), | ||
| severity = severity, | ||
| ) | ||
|
|
||
| override fun log(message: String) { | ||
| when (severity) { | ||
| Severity.Verbose -> logger.v(messageString = message) | ||
| Severity.Debug -> logger.d(messageString = message) | ||
| Severity.Info -> logger.i(messageString = message) | ||
| Severity.Warn -> logger.w(messageString = message) | ||
| Severity.Error -> logger.e(messageString = message) | ||
| Severity.Assert -> logger.a(messageString = message) | ||
| } | ||
| } | ||
| } |
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
31 changes: 31 additions & 0 deletions
31
samples/sample/shared/src/commonMain/kotlin/co/touchlab/kermitsample/CommonClient.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,31 @@ | ||
| /* | ||
| * Copyright (c) 2025 Touchlab | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * 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 co.touchlab.kermitsample | ||
|
|
||
| import co.touchlab.kermit.Logger | ||
| import co.touchlab.kermit.Severity | ||
| import co.touchlab.kermit.ktor.KermitKtorLogger | ||
| import io.ktor.client.HttpClient | ||
| import io.ktor.client.engine.cio.CIO | ||
| import io.ktor.client.plugins.logging.Logging | ||
| import io.ktor.client.request.get | ||
|
|
||
| class CommonClient { | ||
| private val client = HttpClient(CIO) { | ||
| install(Logging) { | ||
| logger = KermitKtorLogger(Severity.Info, Logger.withTag("Ktor")) | ||
| } | ||
| } | ||
|
|
||
| suspend fun testNetworkCall() { | ||
| client.get("https://dog.ceo/api/breeds/image/random") | ||
| } | ||
| } |
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 |
|---|---|---|
| @@ -0,0 +1,41 @@ | ||
| # Ktor Integration | ||
|
|
||
| Kermit's Ktor integration allows you to pass in a Kermit logger instance to your Ktor client. | ||
|
|
||
| ## Setup | ||
|
|
||
| ### Add the dependency | ||
|
|
||
| ```kotlin | ||
| commonMain { | ||
| dependencies { | ||
| implementation("co.touchlab:kermit-ktor:{{LATEST_GITHUB_VERSION}}") | ||
| } | ||
| } | ||
| ``` | ||
|
|
||
| ### Add the logger to your client | ||
|
|
||
| ```kotlin | ||
| val httpClient = HttpClient { | ||
| install(Logging) { | ||
| logger = KermitKtorLogger(severity = Severity.Info, logger = KermitLogger) | ||
| level = LogLevel.INFO | ||
| } | ||
| } | ||
| ``` | ||
|
|
||
| or alternatively | ||
|
|
||
| ```kotlin | ||
| val httpClient = HttpClient { | ||
| install(Logging) { | ||
| logger = KermitKtorLogger(severity = Severity.Info, config = loggerConfigInit(CommonWriter()), tag = "") | ||
| level = LogLevel.INFO | ||
| } | ||
| } | ||
| ``` | ||
|
|
||
| This takes in a Kermit logger instance, or alternatively a Kermit logger configuration to create a logger from. | ||
| This should allow some flexibility in how you want to set up your logging. You will also need to pass in a severity | ||
| level to log to, since the Ktor logger interface does not have levels itself. |
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
Oops, something went wrong.
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.