英文:
Lombok @Slf4j and interfaces?
问题
我正在尝试为我的接口默认方法添加日志记录。例如:
@Slf4j // 不允许
interface MyIFace {
default ThickAndThin doThisAndThat() {
log.error("default doThisAndThat() called");
}
}
问题是我得到了:
@Slf4j 只能用于类和枚举。
有什么适当的方法来处理这个问题呢?
英文:
I am trying to add logging to my interface default method. For example:
@Slf4j // not allowed
interface MyIFace {
default ThickAndThin doThisAndThat() {
log.error("default doThisAndThat() called");
}
}
Problem is that I get:
> @Slf4j is legal only on classes and enums.
What would be the proper way to deal with this?
答案1
得分: 24
你不能在这里使用 Lombok,因为该注解会生成:
private static final org.slf4j.Logger log = org.slf4j.LoggerFactory.getLogger(YourClass.class);
你不能在接口中使用 private 关键字。
解决方法是直接编写:
interface MyIFace {
org.slf4j.Logger log = org.slf4j.LoggerFactory.getLogger(MyIFace.class);
default ThickAndThin doThisAndThat() {
log.error("default doThisAndThat() called");
}
}
英文:
You can't use Lombok here because the annotation generates:
private static final org.slf4j.Logger log = org.slf4j.LoggerFactory.getLogger(YourClass.class);
You can't use private keyword with an interface.
The workaround is to write directly
interface MyIFace {
org.slf4j.Logger log = org.slf4j.LoggerFactory.getLogger(MyIFace.class);
default ThickAndThin doThisAndThat() {
log.error("default doThisAndThat() called");
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论