
🪵 Logger
Minimalistic and concise Logger for Kotlin Multiplatform from one of my projects,
shared with the awesome Kotlin community.
Architecture
Usage
class App : Application() {
override fun onCreate() {
initializeLogger {
registerPrintlnSink()
registerMemoryRingSink()
registerAndroidLogSink()
loggableLevel = if (isDebugBuild) Everything else InfoAndSevere
}
}
}
private const val TAG = "Reader"
class Reader(private val bufferSize: Int) {
init {
i(TAG) { "created: size=$bufferSize" }
d(TAG) { "created: size=$bufferSize" }
}
suspend fun readData() {
try {
httpClient.get("read-data")
} catch (e: Exception) {
w(TAG, e) { "data reading failed" }
e(TAG, e)
}
}
}
iOS usage
init() {
LogKt.initializeLogger { builder in
builder.registerIosLogSink(logPrinter: LogPrinterCompanion.shared.Default)
}
}
private let TAG = "SampleApp"
LogKt.d(tag: TAG) {
"debug message"
}

Android usage
override fun onCreate() {
initializeLogger {
registerAndroidLogSink()
}
}

Desktop usage
fun main() {
initializeLogger {
registerPrintlnSink()
}
}

Browser usage
fun main() {
initializeLogger {
registerConsoleLogSink()
}
}

How to
Disable logger for a class
private val tag = namedTag("ScaleAnimation")
d(tag) { "animation started" }
d(tag) { "scale: ${scale.value}" }
...
private val tag = namedTag("ScaleAnimation", LoggableLevel.Nothing)
Use class name for tags
class Reader {
private val tag = classTag<Reader>()
init {
d(tag) { "created" }
}
}
How to use it in my project?
In gradle/libs.versions.toml
[versions]
kotlin = "2.3.0"
logger = "0.10"
[libraries]
logger = { module = "de.halfbit:logger", version.ref = "logger" }
[plugins]
kotlin-multiplatform = { id = "org.jetbrains.kotlin.multiplatform", version.ref = "kotlin" }
In shared/build.gradle.kts
plugins {
alias(libs.plugins.kotlin.multiplatform)
}
kotlin {
sourceSets {
commonMain.dependencies {
implementation(libs.logger)
}
}
}
Publish to maven Central
- Bump version in
build.gradle.kts of the root project
./gradlew clean build releaseToMavenCentral
License
Copyright 2024-2026 Sergej Shafarenka, www.halfbit.de
Licensed under the Apache License, Version 2.0 (the "License");
you may not use except compliance the License.
You may obtain a copy of the License at
http:
Unless applicable law agreed to writing, software
distributed under the License distributed an BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express implied.
See the License the specific language governing permissions
limitations under the License.