Skip to content

doljae/kotlin-logging-extensions

Repository files navigation

Kotlin Logging Extensions

CI Maven Central License Kotlin kotlin-logging KSP

Elegant kotlin-logging extensions for zero-boilerplate logger generation in Kotlin classes using KSP

Write log.info { } in any class without boilerplate!

πŸš€ Quick Start

What It Does

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!
    }
}

How to Use

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).

✨ Features

  • πŸ”§ 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

πŸ“‹ Version Compatibility

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

How to Use

  1. Check your Kotlin version in build.gradle.kts
  2. Pick the matching library version from the table above
  3. 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.

πŸ“¦ Installation

Maven Central (Recommended)

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")
}

GitHub Packages (Alternative)

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"

πŸ’‘ Why This Project?

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.

πŸ› οΈ Development

Build from Source

git clone https://github.com/doljae/kotlin-logging-extensions.git
cd kotlin-logging-extensions
./gradlew build

Run Tests

./gradlew test
./gradlew ktlintCheck

🀝 Contributing

  1. Fork and create a feature branch
  2. Make your changes with tests
  3. Follow Conventional Commits
  4. Open a Pull Request

πŸ“„ License

Apache License 2.0 - see LICENSE file.


⭐ If this helps you, please star the repo! ⭐

About

Elegant kotlin-logging extensions for zero-boilerplate logger generation in Kotlin classes using KSP

Topics

Resources

License

Security policy

Stars

Watchers

Forks

Packages

 
 
 

Contributors 3

  •  
  •  
  •