slf4j + lombok + 附加记录器

use*_*066 5 java slf4j lombok

我想使用 lombok + @slf4j 在我的类中添加一个额外的记录器。目前,我正在使用 @Slf4j 它创建

私有静态最终org.slf4j.Logger日志= org.slf4j.LoggerFactory.getLogger(LogExample.class)。

我将其用于标准日志记录,我想为类中的特定日志记录创建另一个记录器。

private static final Logger testLog = LoggerFactory.getLogger(LogExample.class.getName()+".TestLog")

将特定日志输出到单独的文件。这是手动进行的。如何使用 lombok @Slf4j 进行配置

Lor*_*ore 2

看起来 Lombok 没有这个功能:问题在这里

也许将来我们会有这个有用的功能。

但是,如果您使用 CDI 框架,则可以在代码中注入额外的记录器。

J2EE方式:

@Produces
Logger produceLogger(InjectionPoint injectionPoint) {
    return LoggerFactory.getLogger(injectionPoint.getMember().getDeclaringClass().getName());
}
Run Code Online (Sandbox Code Playgroud)

注射:

@Inject
private Logger auxLogger;
Run Code Online (Sandbox Code Playgroud)

弹簧方式:

@Bean
@Scope("prototype")
Logger logger(InjectionPoint injectionPoint){
    return LoggerFactory.getLogger(injectionPoint.getMethodParameter().getContainingClass());
}
Run Code Online (Sandbox Code Playgroud)

注入:

@Autowired
private Logger auxLogger;
Run Code Online (Sandbox Code Playgroud)