Java – Lombok SLF4J implements inheritance issues

Lombok SLF4J implements inheritance issues… here is a solution to the problem.

Lombok SLF4J implements inheritance issues

I’m having Lombok issues in a multi-module 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 where lombok dependencies are defined. There is also a child Maven project where I define the parent project as a Maven parent.

When I use the generated log instance in the Child class, I get a compilation error:

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

I can solve this problem in two ways:

  • I created a private field called “log” myself.
  • I defined Lombok dependencies in my child maven project.

Why is this? Any workaround/best way to define lombok dependencies?

Thanks for your help.

Use:

  • Lombok 1.16.18
  • JDK 1.8
  • maven 3.5.3

Solution

Inheritance should not affect your problem in any way. When you use @Slf4j annotations, Lombok creates a private static final field and the static field is not inherited. In your case, the 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");
    }
}

For multi-module Maven projects, you need to ensure that inter-module dependencies are specified correctly. If lombok is on the classpath, everything should work out of the box.

Related Problems and Solutions