龙目岛@ Slf4j和接口?

问题描述

我正在尝试将日志添加到我的界面方法中。例如:

@Slf4j // not allowed
interface MyIFace {
    default ThickAndThin doThisAndThat() {
        log.error("default doThisAndThat() called"); 
    }
}

问题是我得到了

@ Slf4j仅在类和枚举上合法。

解决这个问题的正确方法是什么?

解决方法

您不能在此处使用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"); 
   }

}