
Lightweight logging framework offering a performant logging facade with lazy-eval string logging, exception handling, and fluent logging style. Simplifies logger initialization and supports various SLF4J implementations.
Lightweight Multiplatform logging framework for Kotlin, written in
.
A convenient and performant logging facade.
logger.debug { "Some $expensive message!" }Behind the scenes the expensive message do not get evaluated if debug is not enabled:
// This is what happens when you write the above ^^^
if (logger.isDebugEnabled) logger.debug("Some $expensive message!")// Place definition above class declaration to make field static
private val logger = KotlinLogging.logger {}Behind the scenes val logger will be created in the class, with the class/file name:
// This is what happens when you write the above ^^^
val logger = LoggerFactory.getLogger("package.ClassName")// exception as first parameter with message as lambda
logger.error(exception) { "a $fancy message about the $exception" }logger.atWarn {
message = "foo $bar"
cause = exception
payload = buildMap(capacity = 3) {
put("foo", 1)
put("bar", "x")
put("obj", Pair(2, 3))
}
}import io.github.oshai.kotlinlogging.KotlinLogging
private val logger = KotlinLogging.logger {}
class FooWithLogging {
val message = "world"
fun bar() {
logger.debug { "hello $message" }
}
}Important note: On JVM, kotlin-logging-jvm wraps slf4j-api (compileOnly since version 5, supporting both SLF4J 2.x and 1.7.x). At runtime, add an SLF4J logging implementation such as Logback or Log4j2. More details in the SLF4J user manual and a-guide-to-logging-in-java.
In short, if you just want to log statements to stdout, add org.slf4j:slf4j-simple:2.0.20.
dependencies {
implementation("io.github.oshai:kotlin-logging-jvm:8.0.7")
}[versions]
kotlin-logging = "8.0.7"
[libraries]
kotlin-logging-jvm = { module = "io.github.oshai:kotlin-logging-jvm", version.ref = "kotlin-logging" }
# For Kotlin Multiplatform commonMain:
kotlin-logging = { module = "io.github.oshai:kotlin-logging", version.ref = "kotlin-logging" }kotlin {
sourceSets {
commonMain.dependencies {
implementation("io.github.oshai:kotlin-logging:8.0.7")
}
}
}implementation 'io.github.oshai:kotlin-logging-jvm:8.0.7'<dependency>
<groupId>io.github.oshai</groupId>
<artifactId>kotlin-logging-jvm</artifactId>
<version>8.0.7</version>
</dependency>See the full example in kotlin-logging-example-maven.
Alternatively, download the JAR from GitHub Releases or Maven Central.
kotlin-logging provides full production Kotlin Multiplatform support across 20+ targets from a single commonMain dependency (io.github.oshai:kotlin-logging:8.0.7):
-Dkotlin-logging-to-logback=true), java.util.logging (-Dkotlin-logging-to-jul=true), or stdout direct logging (-Dkotlin-logging-to-direct=true).kotlin-logging-android) — SLF4J (default) or native android.util.Log (-Dkotlin-logging-to-android-native=true).js).wasmJs (Browser) and wasmWasi (Node.js / WASI).macos (Arm64, X64), ios (Arm64, SimulatorArm64, X64), watchos, and tvos backed by Apple Unified Logging (os_log).linuxX64, linuxArm64, mingwX64, and androidNative (Arm64, Arm32, X64, X86).On JS, Wasm, and Native platforms, configure the console log level via KotlinLoggingConfiguration.direct.logLevel = Level.DEBUG. More details and platform-specific options are documented on the Multiplatform Wiki.
After seeing many questions like Idiomatic way of logging in Kotlin and Best practices for loggers, it seems like there should be a standard for logging and obtaining a logger in Kotlin. kotlin-logging provides a wrapper for slf4j-api to be used by Kotlin classes with the following advantages:
{}.Version 5+ is not backward compatible with pre-5 versions (v3, v2, v1). Maven group ID and package names changed, making it possible to use io.github.oshai (5.x–8.x) and legacy io.github.microutils (1.x–3.x) side-by-side in the same dependency graph during incremental migrations.
Main changes are:
io.github.microutils -> io.github.oshai.mu -> io.github.oshai.kotlinlogging.8.0.0 removed deprecated eager String? overloads (logger.info("msg"), logger.error("msg", ex)); use lazy {} lambdas (logger.info { "msg" }, logger.error(ex) { "msg" }) or fluent builders (logger.atInfo { ... }).Automated migration: See docs/openrewrite/README.md for an OpenRewrite recipe that automatically upgrades io.github.microutils dependencies and rewrites mu.* imports to io.github.oshai 8.x.
More details in issue #264, and in the change log
And many more... (add your name above)
logger.debug, my IntelliJ IDEA run console doesn't show any output. Do you know how I could set the console logger to debug or trace levels? Is this an IDE setting, or can it be set in the call to KLogging()? Setting log level is done per implementation. kotlin-logging and slf4j are just facades for the underlying logging lib (log4j, logback etc') more details here.DelegatingKLogger.underlyingLogger property.KotlinLogging.logger {} inside a nested or inner class use the outer class name on JVM? On JVM and Android, logger name resolution strips everything from the first $ in the binary class name so lambdas and Companion objects resolve to the enclosing class. To use a separate logger name for a nested or inner class, pass it explicitly: KotlinLogging.logger(Outer.Inner::class.java.name) (or KotlinLogging.logger("package.Outer.Inner")).llms.txt (https://oshai.github.io/kotlin-logging/llms.txt)It is possible to configure IntelliJ live templates. For file level logger configure the following:
private val logger = io.github.oshai.kotlinlogging.KotlinLogging.logger {}.Kotlin: top-level.Older 1.x articles (note: 5.x+ uses Maven group io.github.oshai and package io.github.oshai.kotlinlogging):
Any contribution is appreciated.
Pull requests are welcome! See instructions in https://github.com/oshai/kotlin-logging/blob/master/CONTRIBUTING.md.
Thank you to all the developers who have contributed to kotlin-logging!
See the full contributors list in: https://github.com/oshai/kotlin-logging/graphs/contributors
Lightweight Multiplatform logging framework for Kotlin, written in
.
A convenient and performant logging facade.
logger.debug { "Some $expensive message!" }Behind the scenes the expensive message do not get evaluated if debug is not enabled:
// This is what happens when you write the above ^^^
if (logger.isDebugEnabled) logger.debug("Some $expensive message!")// Place definition above class declaration to make field static
private val logger = KotlinLogging.logger {}Behind the scenes val logger will be created in the class, with the class/file name:
// This is what happens when you write the above ^^^
val logger = LoggerFactory.getLogger("package.ClassName")// exception as first parameter with message as lambda
logger.error(exception) { "a $fancy message about the $exception" }logger.atWarn {
message = "foo $bar"
cause = exception
payload = buildMap(capacity = 3) {
put("foo", 1)
put("bar", "x")
put("obj", Pair(2, 3))
}
}import io.github.oshai.kotlinlogging.KotlinLogging
private val logger = KotlinLogging.logger {}
class FooWithLogging {
val message = "world"
fun bar() {
logger.debug { "hello $message" }
}
}Important note: On JVM, kotlin-logging-jvm wraps slf4j-api (compileOnly since version 5, supporting both SLF4J 2.x and 1.7.x). At runtime, add an SLF4J logging implementation such as Logback or Log4j2. More details in the SLF4J user manual and a-guide-to-logging-in-java.
In short, if you just want to log statements to stdout, add org.slf4j:slf4j-simple:2.0.20.
dependencies {
implementation("io.github.oshai:kotlin-logging-jvm:8.0.7")
}[versions]
kotlin-logging = "8.0.7"
[libraries]
kotlin-logging-jvm = { module = "io.github.oshai:kotlin-logging-jvm", version.ref = "kotlin-logging" }
# For Kotlin Multiplatform commonMain:
kotlin-logging = { module = "io.github.oshai:kotlin-logging", version.ref = "kotlin-logging" }kotlin {
sourceSets {
commonMain.dependencies {
implementation("io.github.oshai:kotlin-logging:8.0.7")
}
}
}implementation 'io.github.oshai:kotlin-logging-jvm:8.0.7'<dependency>
<groupId>io.github.oshai</groupId>
<artifactId>kotlin-logging-jvm</artifactId>
<version>8.0.7</version>
</dependency>See the full example in kotlin-logging-example-maven.
Alternatively, download the JAR from GitHub Releases or Maven Central.
kotlin-logging provides full production Kotlin Multiplatform support across 20+ targets from a single commonMain dependency (io.github.oshai:kotlin-logging:8.0.7):
-Dkotlin-logging-to-logback=true), java.util.logging (-Dkotlin-logging-to-jul=true), or stdout direct logging (-Dkotlin-logging-to-direct=true).kotlin-logging-android) — SLF4J (default) or native android.util.Log (-Dkotlin-logging-to-android-native=true).js).wasmJs (Browser) and wasmWasi (Node.js / WASI).macos (Arm64, X64), ios (Arm64, SimulatorArm64, X64), watchos, and tvos backed by Apple Unified Logging (os_log).linuxX64, linuxArm64, mingwX64, and androidNative (Arm64, Arm32, X64, X86).On JS, Wasm, and Native platforms, configure the console log level via KotlinLoggingConfiguration.direct.logLevel = Level.DEBUG. More details and platform-specific options are documented on the Multiplatform Wiki.
After seeing many questions like Idiomatic way of logging in Kotlin and Best practices for loggers, it seems like there should be a standard for logging and obtaining a logger in Kotlin. kotlin-logging provides a wrapper for slf4j-api to be used by Kotlin classes with the following advantages:
{}.Version 5+ is not backward compatible with pre-5 versions (v3, v2, v1). Maven group ID and package names changed, making it possible to use io.github.oshai (5.x–8.x) and legacy io.github.microutils (1.x–3.x) side-by-side in the same dependency graph during incremental migrations.
Main changes are:
io.github.microutils -> io.github.oshai.mu -> io.github.oshai.kotlinlogging.8.0.0 removed deprecated eager String? overloads (logger.info("msg"), logger.error("msg", ex)); use lazy {} lambdas (logger.info { "msg" }, logger.error(ex) { "msg" }) or fluent builders (logger.atInfo { ... }).Automated migration: See docs/openrewrite/README.md for an OpenRewrite recipe that automatically upgrades io.github.microutils dependencies and rewrites mu.* imports to io.github.oshai 8.x.
More details in issue #264, and in the change log
And many more... (add your name above)
logger.debug, my IntelliJ IDEA run console doesn't show any output. Do you know how I could set the console logger to debug or trace levels? Is this an IDE setting, or can it be set in the call to KLogging()? Setting log level is done per implementation. kotlin-logging and slf4j are just facades for the underlying logging lib (log4j, logback etc') more details here.DelegatingKLogger.underlyingLogger property.KotlinLogging.logger {} inside a nested or inner class use the outer class name on JVM? On JVM and Android, logger name resolution strips everything from the first $ in the binary class name so lambdas and Companion objects resolve to the enclosing class. To use a separate logger name for a nested or inner class, pass it explicitly: KotlinLogging.logger(Outer.Inner::class.java.name) (or KotlinLogging.logger("package.Outer.Inner")).llms.txt (https://oshai.github.io/kotlin-logging/llms.txt)It is possible to configure IntelliJ live templates. For file level logger configure the following:
private val logger = io.github.oshai.kotlinlogging.KotlinLogging.logger {}.Kotlin: top-level.Older 1.x articles (note: 5.x+ uses Maven group io.github.oshai and package io.github.oshai.kotlinlogging):
Any contribution is appreciated.
Pull requests are welcome! See instructions in https://github.com/oshai/kotlin-logging/blob/master/CONTRIBUTING.md.
Thank you to all the developers who have contributed to kotlin-logging!
See the full contributors list in: https://github.com/oshai/kotlin-logging/graphs/contributors