diff --git a/extensions/kermit-ktor/README.md b/extensions/kermit-ktor/README.md new file mode 100644 index 00000000..f2564d80 --- /dev/null +++ b/extensions/kermit-ktor/README.md @@ -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 + } +} +``` \ No newline at end of file diff --git a/extensions/kermit-ktor/api/kermit-ktor.api b/extensions/kermit-ktor/api/kermit-ktor.api new file mode 100644 index 00000000..35617cdf --- /dev/null +++ b/extensions/kermit-ktor/api/kermit-ktor.api @@ -0,0 +1,7 @@ +public final class co/touchlab/kermit/ktor/KermitKtorLogger : io/ktor/client/plugins/logging/Logger { + public fun (Lco/touchlab/kermit/Severity;Lco/touchlab/kermit/Logger;)V + public fun (Lco/touchlab/kermit/Severity;Lco/touchlab/kermit/LoggerConfig;Ljava/lang/String;)V + public synthetic fun (Lco/touchlab/kermit/Severity;Lco/touchlab/kermit/LoggerConfig;Ljava/lang/String;ILkotlin/jvm/internal/DefaultConstructorMarker;)V + public fun log (Ljava/lang/String;)V +} + diff --git a/extensions/kermit-ktor/build.gradle.kts b/extensions/kermit-ktor/build.gradle.kts new file mode 100644 index 00000000..c085edeb --- /dev/null +++ b/extensions/kermit-ktor/build.gradle.kts @@ -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 + } +} diff --git a/extensions/kermit-ktor/src/commonMain/kotlin/co/touchlab/kermit/ktor/KermitKtorLogger.kt b/extensions/kermit-ktor/src/commonMain/kotlin/co/touchlab/kermit/ktor/KermitKtorLogger.kt new file mode 100644 index 00000000..120c62b4 --- /dev/null +++ b/extensions/kermit-ktor/src/commonMain/kotlin/co/touchlab/kermit/ktor/KermitKtorLogger.kt @@ -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) + } + } +} diff --git a/gradle/libs.versions.toml b/gradle/libs.versions.toml index 2680c424..95c2f5ce 100644 --- a/gradle/libs.versions.toml +++ b/gradle/libs.versions.toml @@ -36,6 +36,7 @@ android-junitTest = "1.1.5" junit = "4.13.2" dokka = "2.0.0" touchlab-docusaurus-template = "0.1.11" +ktor = "3.3.1" ktlint-gradle = "12.3.0" @@ -52,6 +53,7 @@ androidx-runner = { module = "androidx.test:runner", version.ref = "android-test crashkios-crashlytics = { module = "co.touchlab.crashkios:crashlytics", version.ref = "crashkios" } crashkios-bugsnag = { module = "co.touchlab.crashkios:bugsnag", version.ref = "crashkios" } bugsnag-android = { module = "com.bugsnag:bugsnag-android", version.ref = "bugsnag" } +ktor-logging = { module = "io.ktor:ktor-client-logging", version.ref = "ktor" } stately-collections = { module = "co.touchlab:stately-collections", version.ref = "stately" } testhelp = { module = "co.touchlab:testhelp", version.ref = "testhelp" } diff --git a/samples/sample/app/src/main/AndroidManifest.xml b/samples/sample/app/src/main/AndroidManifest.xml index d0e97e26..6b458e7f 100644 --- a/samples/sample/app/src/main/AndroidManifest.xml +++ b/samples/sample/app/src/main/AndroidManifest.xml @@ -11,6 +11,9 @@ + + + + +