[英]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:在日志中,我只能看到来自 BClass 而不是 AClass 的字段:
17:52:28.151 [main] INFO Main - Main.BClass(amount=12)
17:52:28.151 [main] INFO Main - Main.BClass(数量=12)
But I want to see all BClass fields including ones that are in parent class(title field).但我想查看所有 BClass 字段,包括父类(标题字段)中的字段。 How can I do it?
我该怎么做?
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
.这很正常 - 您正在查看的内容(特别是日志中的
Main.BClass(amount=12)
部分)是 BClass 的toString()
方法的BClass
。 The @Data
annotation is making a toString implementation for you. @Data
注释正在为您创建一个 toString 实现。 However, lombok intentionally does not include superclass information normally.但是,lombok 通常有意不包含超类信息。 You should be seeing a warning on your
@Data
annotation that explains this.您应该会在
@Data
注释上看到一条警告,解释了这一点。 One way to solve that problem is to add @ToString(callSuper = true)
next to your @Data
annotation.解决该问题的一种方法是在
@Data
注释旁边添加@ToString(callSuper = true)
。
This isn't about SLF4J or any logging framework - it's about what is and isn't in the generated toString()
implementation.这与 SLF4J 或任何日志记录框架无关——它与生成的
toString()
实现中的内容和内容无关。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.