Lombok的@Slf4j和接口部分呢?

huangapple go评论67阅读模式
英文:

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"); 
   }

}


huangapple
  • 本文由 发表于 2020年10月17日 23:30:52
  • 转载请务必保留本文链接:https://go.coder-hub.com/64404180.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定