Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Lombok SLF4J impl inheritance issues

I'm facing an issue with Lombok in my multimodule maven project. I have two classes :

@Slf4j
public class Parent {}

@Slf4j
public class Child extends Parent {
    public void m() {
        log.debug("hello");
    }
}

There is also a parent maven project in which the lombok dependency is defined. And a child maven project in which I define the parent project as maven parent.

When I use generated log instance in Child class, compilation error occurs :

[ERROR] log has private access in <Child class>

I could resolved the issue in two ways:

  • I create myself the private field named "log" by hand.
  • I define the Lombok dependency in the child maven project.

Why so? Any workaround / best way to define the lombok dependency ?

Thanks for your help.

Using :

  • lombok 1.16.18
  • JDK 1.8
  • Maven 3.5.3
like image 849
PacDroid Avatar asked Apr 15 '26 11:04

PacDroid


1 Answers

Inheritance should not impact your issue in any way. When you use @Slf4j annotation Lombok creates private static final field and static fields are not inherited. In your case generated code looks like this:

public class Parent {
    private static final Logger log = LoggerFactory.getLogger(Parent.class);

    public Parent() {}
}

public class Child extends Parent {
    private static final Logger log = LoggerFactory.getLogger(Child.class);

    public Child() {}

    public void m() {
        log.debug("hello");
    }
}

With multi-module maven project you need to ensure that inter-module dependencies are specified correctly. If lombok is on the classpath everything should work perfectly fine out of the box.

like image 112
k13i Avatar answered Apr 18 '26 02:04

k13i



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!