Elegant kotlin-logging extensions for zero-boilerplate logger generation in Kotlin classes using KSP
Write log.info { }
in any class without boilerplate!
Automatically generates logger extensions for every Kotlin class during compilation. No manual logger declarations needed - just use log
directly in any class.
// β Before: Manual logger in every class
class UserService {
private val log = KotlinLogging.logger {} // Boilerplate!
fun createUser() {
log.info { "Creating user" }
}
}
// β
After: Just use log directly
class UserService {
fun createUser() {
log.info { "Creating user" } // Auto-generated!
}
}
Step 1: Add Dependencies
Add to your build.gradle.kts
:
plugins {
kotlin("jvm") version "2.2.0"
id("com.google.devtools.ksp") version "2.2.0-2.0.2"
}
repositories {
mavenCentral()
}
dependencies {
ksp("io.github.doljae:kotlin-logging-extensions:2.2.0-0.0.2")
implementation("io.github.doljae:kotlin-logging-extensions:2.2.0-0.0.2")
implementation("io.github.oshai:kotlin-logging-jvm:7.0.7")
implementation("ch.qos.logback:logback-classic:1.5.18") // Logger implementation required
}
Step 2: Use log
in Any Class
class OrderProcessor {
fun processOrder(id: String) {
log.info { "Processing order: $id" }
try {
// Business logic here
log.debug { "Order processed successfully" }
} catch (e: Exception) {
log.error(e) { "Failed to process order: $id" }
}
}
}
Step 3: Generate Logger Code After writing your code, run KSP to generate the logger extensions:
./gradlew kspKotlin kspTestKotlin
This will generate the log
property and resolve any compilation errors in your IDE.
That's it! The logger is automatically available with the class name (OrderProcessor
in this example).
- π§ Zero Boilerplate: No logger declarations needed - just use
log.info { }
- β‘ Compile-time Generation: Uses KSP for compile-time safety with zero runtime overhead
- π¦ Package-aware Naming: Logger names automatically match fully qualified class names
- ποΈ kotlin-logging Integration: Works seamlessly with the standard kotlin-logging library
- π― Works Everywhere: Compatible with any package depth and class structure
Choose the library version that matches your project's Kotlin version. Our versioning follows the KOTLIN_VERSION-LIBRARY_VERSION
pattern (same as KSP).
Library | Kotlin | KSP |
---|---|---|
2.2.0-0.0.2 |
2.2.0 |
2.2.0-2.0.2 |
2.2.0-0.0.1 |
2.2.0 |
2.1.21-2.0.2 |
2.1.21-0.0.1 |
2.1.21 |
2.1.21-2.0.2 |
- Check your Kotlin version in
build.gradle.kts
- Pick the matching library version from the table above
- Use the exact KSP version shown in the table
// For Kotlin 2.2.0 projects:
plugins {
kotlin("jvm") version "2.2.0"
id("com.google.devtools.ksp") version "2.2.0-2.0.2"
}
dependencies {
ksp("io.github.doljae:kotlin-logging-extensions:2.2.0-0.0.2")
implementation("io.github.doljae:kotlin-logging-extensions:2.2.0-0.0.2")
implementation("io.github.oshai:kotlin-logging-jvm:7.0.7") // 5.0.0+
}
kotlin-logging compatibility: This library requires kotlin-logging 5.0.0+ due to package name changes. Versions 5.x+ use io.github.oshai.kotlinlogging
package, while older versions used mu
package.
Logger implementation: If you're already using kotlin-logging in your project, no additional setup needed. For new projects, add a logger implementation like Logback or Log4j2.
plugins {
kotlin("jvm") version "2.2.0"
id("com.google.devtools.ksp") version "2.2.0-2.0.2"
}
repositories {
mavenCentral()
}
dependencies {
ksp("io.github.doljae:kotlin-logging-extensions:2.2.0-0.0.2")
implementation("io.github.doljae:kotlin-logging-extensions:2.2.0-0.0.2")
implementation("io.github.oshai:kotlin-logging-jvm:7.0.7")
implementation("ch.qos.logback:logback-classic:1.5.18")
}
For development or specific use cases, you can also use GitHub Packages:
repositories {
mavenCentral()
maven {
url = uri("https://maven.pkg.github.com/doljae/kotlin-logging-extensions")
credentials {
username = System.getenv("GITHUB_USERNAME")
password = System.getenv("GITHUB_TOKEN")
}
}
}
Note: GitHub Packages requires authentication. Set environment variables:
export GITHUB_USERNAME="your-github-username"
export GITHUB_TOKEN="your-personal-access-token"
Problem: Kotlin developers miss Java's Lombok @Slf4j
simplicity. Current solutions require either:
- Top-level logger declarations (violates "one class per file")
- Manual logger in every class (repetitive boilerplate)
Solution: Automatic logger generation that "just works" - inspired by Lombok's elegance, built with Kotlin's KSP power.
git clone https://github.com/doljae/kotlin-logging-extensions.git
cd kotlin-logging-extensions
./gradlew build
./gradlew test
./gradlew ktlintCheck
- Fork and create a feature branch
- Make your changes with tests
- Follow Conventional Commits
- Open a Pull Request
Apache License 2.0 - see LICENSE file.
β If this helps you, please star the repo! β