45

I have a Kotlin Gradle project. I added Lombok as a dependency and also registered it with kapt

compileOnly("org.projectlombok:lombok:$lombokVersion")
kapt("org.projectlombok:lombok:$lombokVersion")

I would like to use the @Slf4j annotation for automatic logger generation. It works for Java classes but not for the Kotlin ones.

Is using Kotlin and Lombok together even possible as of now? If I annotate a Kotlin class with @Slf4j and use log inside it I get

Unresolved reference: log

Evidently no annotation processing is applied.

Michael
  • 41,989
  • 11
  • 82
  • 128
ps-aux
  • 11,627
  • 25
  • 81
  • 128
  • 1
    If its just for logging you may want to check https://discuss.kotlinlang.org/t/best-practices-for-loggers/226/9 – Emanuel Sep 03 '17 at 21:22
  • Does this answer your question? [Kotlin doesn't see Java Lombok accessors?](https://stackoverflow.com/questions/35517325/kotlin-doesnt-see-java-lombok-accessors) – knittl Oct 04 '22 at 09:42

8 Answers8

23

Lombok does not run on your source code, but on the AST. Anyway, it is an annotation processor that is run at compile-time by the Java compiler. The Kotlin compiler does not use these annotation processors. See also the answer https://stackoverflow.com/a/35530223/2621917 straight from the horse’s mouth.

Michael Piefel
  • 18,660
  • 9
  • 81
  • 112
10

If all you want to use Lombok for is @Slf4j, then I'd suggest using kotlin-logging instead: https://github.com/MicroUtils/kotlin-logging

It's a simple wrapper around slf4j, so instead of annotating your class with @Slf4j, you use:

// Place definition above class declaration to make field static
private val logger = KotlinLogging.logger {}
// ...

logger.debug { "A message only logged if debug is enabled. With $variable support." }
YetAnotherMatt
  • 391
  • 4
  • 9
10

You cannot use annotation @Slf4j, but manually create its object in the class required.

Refer https://www.reddit.com/r/Kotlin/comments/8gbiul/slf4j_loggers_in_3_ways/

Sahil Chhabra
  • 10,621
  • 4
  • 63
  • 62
3

Lombok's builder annotation support has been added to kotlin 1.8 as of late December 2022.

You can learn how to configure the plugin here.

In Short, add

plugins {
    id 'org.jetbrains.kotlin.plugin.lombok' version '1.8.0'
    id 'io.freefair.lombok' version '5.3.0'
}

to your Groovy/Gradle files, and/or take a look at the sample project.

Martin Marconcini
  • 26,875
  • 19
  • 106
  • 144
1

It's not supported and, by the looks of things, it isn't going to be.

Michael
  • 41,989
  • 11
  • 82
  • 128
1

from kotlin 1.7.20 with K2 compiler it is possible.

https://kotlinlang.org/docs/whatsnew1720.html#support-for-kotlin-k2-compiler-plugins

0

For logging the best I could do - because @Slf4j did not work - was like creating abstract log class like:

package org.example

import org.slf4j.LoggerFactory
import org.slf4j.Logger

abstract class Log {
    val log: Logger = LoggerFactory.getLogger(this.javaClass)
}

and usage:

package org.example

class MyClass { 
    companion object : Log() {}
    @Test
    fun someFun() {
        log.info("Logging info")
    }
}
pirho
  • 11,565
  • 12
  • 43
  • 70
  • @Michael it is not per instance since the companion object inherits from Log, but what I don't like is that you can't use this solution if you actually need to inherit from another class – Osman Oct 11 '22 at 14:52
  • 1
    @Osman A misunderstanding of the semantics of a companion object. My bad, thanks for the correction. – Michael Oct 11 '22 at 16:31
-1

I can't see how it would work without additional support from the lombok team. Lombok is based on annotation processing so it runs during compilation time and runs on your source code, so I guess it assumes Java's syntax.

Eyal
  • 11
  • 1
    Lombok doesn't run directly on your source code; rather, it exploits the Java compiler's built-in support for compile-time annotation processors. – ruakh Sep 03 '17 at 21:19
  • If I'm not mistaken, some of the advanced features of Lombok actually need access to the Java sources. – hotkey Sep 03 '17 at 22:03
  • AFAIK Lombok runs at compile-time. So it has access to your source code. It also uses some techniques to modify the generated AST – Alberto S. Sep 04 '17 at 06:26