Log4j允许我轻松地为每个类设置日志记录。我想在初始化时更改我的JavaFX应用程序的属性。我想要做的是在用户的%appdata%/<app>/<version>
目录下创建日志文件(为了示例)。我发现documentation以编程方式设置了logger的属性。
我有一个非常相似的Log4j ConfigurationFactory
,如下所示。
public class Log4jConfigurationFactory extends ConfigurationFactory {
@Override
protected String[] getSupportedTypes() {
return new String[] {"*"};
}
private static Configuration createConfiguration(final String name, ConfigurationBuilder<BuiltConfiguration> builder) {
builder.setConfigurationName(name);
builder.setStatusLevel(Level.DEBUG);
builder.add(builder.newFilter("ThresholdFilter", Filter.Result.ACCEPT, Filter.Result.NEUTRAL).
addAttribute("level", Level.DEBUG));
AppenderComponentBuilder appenderBuilder = builder.newAppender("Stdout", "CONSOLE").
addAttribute("target", ConsoleAppender.Target.SYSTEM_OUT);
appenderBuilder.add(builder.newLayout("PatternLayout").
addAttribute("pattern", "%d [%t] %-5level: %msg%n%throwable"));
appenderBuilder.add(builder.newFilter("MarkerFilter", Filter.Result.DENY,
Filter.Result.NEUTRAL).addAttribute("marker", "FLOW"));
builder.add(appenderBuilder);
builder.add(builder.newLogger("org.apache.logging.log4j", Level.DEBUG).
add(builder.newAppenderRef("Stdout")).
addAttribute("additivity", false));
builder.add(builder.newRootLogger(Level.ERROR).add(builder.newAppenderRef("Stdout")));
return builder.build();
}
@Override
public Configuration getConfiguration(final LoggerContext loggerContext, final ConfigurationSource source) {
return getConfiguration(loggerContext, source.toString(), null);
}
@Override
public Configuration getConfiguration(final LoggerContext loggerContext, final String name, final URI configLocation) {
ConfigurationBuilder<BuiltConfiguration> builder = newConfigurationBuilder();
return createConfiguration(name, builder);
}
}
我有一个主要课程如下:
@Log4j2
public class App extends GenericApplication { // this GenericApplication extends Application, it is from a library I use, no worries.
@Override
public void init() throws Exception {
super.init();
log.debug("Hey."); // for testing purposes
}
@Override
public void start(Stage primaryStage) {
super.start(primaryStage);
}
@Override
public void stop() throws Exception {
log.error("Hey."); // for testing purposes
}
public static void main(String[] args ) {
GenericApplication.launch(args);
}
}
问题是Log4j 自然找不到我的自定义Log4jConfigurationFactory
。似乎Lombok没有Log4j
注释来提供自定义ConfigurationFactory
(根据其documentation,唯一的参数Log
注释可能具有topic
目的这是一个人类可读的(?)Logger
标题。)。
那么,如何在Lombok中使用自定义ConfigurationFactory
?
提前致谢。
Group ID: org.projectlombok
Artifact ID: lombok
Version: 1.16.20
Group ID: org.apache.logging.log4j
Artifact ID: log4j-core
Version: 2.11.0