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

⚠️ Version Compatibility

Important: This project is highly dependent on Kotlin and KSP versions due to its nature as a symbol processing library.

🔧 Critical Requirements

  • Kotlin: 2.1.21Most Important
  • KSP: 2.1.21-2.0.2Most Important
  • kotlin-logging: Any version (API-compatible)

📋 Before Using

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

🔍 How to Check Your Versions

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

🚀 Upgrade if Needed

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

📦 Versioning Policy

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 KSP 2.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.

💡 Future Improvements

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.

🌐 kotlin-logging Compatibility

This project is compatible with any version of kotlin-logging as long as the logger declaration API remains unchanged:

  • kotlin-logging-jvm for JVM environments
  • kotlin-logging-js for JavaScript/Node.js environments
  • kotlin-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.

⚠️ Logger Implementation

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.

✨ 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

📦 Installation

Maven Central (Recommended)

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

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!