'Slf4j logger log fields from parent class

I have following code:

@Slf4j
public class Main {

@Data
public static class AClass {
    private String title;
}

@Data
public static class BClass extends AClass {
    private Integer amount;
}

public static void main(String[] args) {
    BClass bClass = new BClass();
    bClass.setAmount(12);
    bClass.setTitle("title");

    log.info("{}", bClass);
}
}

In logs I can see only fields from BClass and not AClass :

17:52:28.151 [main] INFO Main - Main.BClass(amount=12)

But I want to see all BClass fields including ones that are in parent class(title field). How can I do it?



Solution 1:[1]

That's normal - what you're looking at (specifically, the Main.BClass(amount=12) part in your log) is the output of the toString() method of BClass. The @Data annotation is making a toString implementation for you. However, lombok intentionally does not include superclass information normally. You should be seeing a warning on your @Data annotation that explains this. One way to solve that problem is to add @ToString(callSuper = true) next to your @Data annotation.

This isn't about SLF4J or any logging framework - it's about what is and isn't in the generated toString() implementation.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 rzwitserloot