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.1.21"
id("com.google.devtools.ksp") version "2.1.21-2.0.2"
}
repositories {
mavenCentral()
}
dependencies {
ksp("io.github.doljae:kotlin-logging-extensions:2.1.21-0.0.1")
implementation("io.github.doljae:kotlin-logging-extensions:2.1.21-0.0.1")
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).
Important: This project is highly dependent on Kotlin and KSP versions due to its nature as a symbol processing library.
- Kotlin:
2.1.21
⭐ Most Important - KSP:
2.1.21-2.0.2
⭐ Most Important - kotlin-logging: Any version (API-compatible)
Please ensure your project uses the exact same Kotlin and KSP versions as shown above. Version mismatches may cause:
- Compilation failures
- Missing log property generation
- Runtime issues
// In your build.gradle.kts, check:
plugins {
kotlin("jvm") version "2.1.21" // ← Should match exactly
id("com.google.devtools.ksp") version "2.1.21-2.0.2" // ← Should match exactly
}
If your Kotlin/KSP versions don't match, consider upgrading:
plugins {
kotlin("jvm") version "2.1.21"
id("com.google.devtools.ksp") version "2.1.21-2.0.2"
}
This project follows the same versioning strategy as KSP:
- Library versions are aligned with KSP releases (e.g.,
2.1.21-0.0.1
for KSP2.1.21-2.0.2
) - The first part (
2.1.21
) matches the Kotlin version - The last part (
0.0.1
) is our library's patch version
This ensures clear compatibility mapping and reduces version confusion.
We're working on improving version compatibility in future releases. Have ideas or suggestions? We'd love to hear from you! Please open an issue in the Issues tab.
This project is compatible with any version of kotlin-logging as long as the logger declaration API remains unchanged:
kotlin-logging-jvm
for JVM environmentskotlin-logging-js
for JavaScript/Node.js environmentskotlin-logging-linuxx64
,kotlin-logging-mingwx64
for native targets- Other platform-specific variants
Note: The kotlin-logging API is stable, so version compatibility should not be an issue.
If you're already using kotlin-logging successfully in your project, you don't need to add any additional logger implementation dependencies.
If you're setting up logging for the first time, you'll need a logger implementation like Logback or Log4j2.
- 🔧 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
plugins {
kotlin("jvm") version "2.1.21"
id("com.google.devtools.ksp") version "2.1.21-2.0.2"
}
repositories {
mavenCentral()
}
dependencies {
ksp("io.github.doljae:kotlin-logging-extensions:2.1.21-0.0.1")
implementation("io.github.doljae:kotlin-logging-extensions:2.1.21-0.0.1")
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! ⭐