我一直在用Java配置log4j2時遇到問題。與log4j Java配置有關的問題
我有一個工作的XML配置:
<?xml version="1.0" encoding="UTF-8"?>
<configuration status="DEBUG">
<appenders>
<Console name="Console" target="SYSTEM_OUT">
<PatternLayout pattern="%d{HH:mm:ss.SSS} [%t] %-5level - %msg%n" />
</Console>
<File name="DM_PREPUB" fileName="logs/41_2015/DM_PREPUB.log">
<MarkerFilter marker="DM_PREPUB" onMatch="ACCEPT" onMismatch="DENY" />
<PatternLayout>
<pattern>%d %p %C{10} [%t] %m%n</pattern>
</PatternLayout>
</File>
<File name="BACKUP" fileName="logs/41_2015/BACKUP.log">
<MarkerFilter marker="BACKUP" onMatch="ACCEPT" onMismatch="DENY" />
<PatternLayout>
<pattern>%d %p %C{10} [%t] %m%n</pattern>
</PatternLayout>
</File>
</appenders>
<loggers>
<logger name="foo.bar" level="DEBUG">
<appender-ref ref="DM_PREPUB" />
<appender-ref ref="BACKUP" />
</logger>
<root level="ALL">
<appender-ref ref="Console" />
</root>
</loggers>
</configuration>
正如你所看到的,這個配置創建了相應的標記兩種文件追加程序。非常簡單。
我試圖在Java中實現相同的配置,但沒有運氣:
ConfigurationBuilder<BuiltConfiguration> builder = ConfigurationBuilderFactory.newConfigurationBuilder();
builder.setStatusLevel(org.apache.logging.log4j.Level.ALL);
builder.setConfigurationName("DMWorkflow");
builder.add(builder.newFilter("ThresholdFilter", Filter.Result.ACCEPT, Filter.Result.NEUTRAL).addAttribute("level", org.apache.logging.log4j.Level.DEBUG));
// console appender
AppenderComponentBuilder appenderBuilder = builder.newAppender("Stdout", "CONSOLE").addAttribute("target", ConsoleAppender.Target.SYSTEM_OUT);
appenderBuilder.add(builder.newLayout("PatternLayout").addAttribute("pattern", "%d{HH:mm:ss.SSS} [%t] %-5level - %msg%n"));
builder.add(appenderBuilder);
// main log appender
appenderBuilder = builder.newAppender("DM_PREPUB" /*appender name*/, "FILE" /*appender type*/)
.addAttribute("fileName", "logs/41_2015/DM_PREPUB.log");
appenderBuilder.add(builder.newLayout("PatternLayout").addAttribute("pattern", "%d %p %C{10} [%t] %m%n"));
appenderBuilder.add(builder.newFilter("MarkerFilter", Filter.Result.ACCEPT, Filter.Result.DENY).addAttribute("marker", "DM_PREPUB"));
builder.add(appenderBuilder);
// step appenders
appenderBuilder = builder.newAppender("BACKUP" /*appender name*/, "FILE" /*appender type*/)
.addAttribute("fileName", "logs/41_2015/BACKUP.log");
appenderBuilder.add(builder.newLayout("PatternLayout").addAttribute("pattern", "%d %p %C{10} [%t] %m%n"));
appenderBuilder.add(builder.newFilter("MarkerFilter", Filter.Result.ACCEPT, Filter.Result.DENY).addAttribute("marker", "BACKUP"));
builder.add(appenderBuilder);
// main logger
LoggerComponentBuilder componentBuilder = builder.newLogger("foo.bar", org.apache.logging.log4j.Level.DEBUG).add(builder.newAppenderRef("DM_PREPUB"));
// step loggers
componentBuilder.add(builder.newAppenderRef("BACKUP"));
builder.add(componentBuilder);
builder.add(builder.newRootLogger(org.apache.logging.log4j.Level.ALL).add(builder.newAppenderRef("Stdout")));
Configurator.initialize(builder.build());
在這兩種情況下,我實例化的記錄是這樣的:
logger = LogManager.getLogger("foo.bar");
globalMarker = MarkerManager.getMarker("DM_PREPUB");
Java的配置代碼不看起來很複雜,這就是爲什麼我不能看清楚我做錯了什麼。
如果你可以有任何幫助,那可能會很棒。
謝謝!
艾萬
我忘了指定,在運行時,文件被創建好了!但是它們仍然是空的,就像控制檯一樣...... – Erwann
你可以使用「編輯」鏈接將其添加到問題本身而不是評論中。 –
並且您將標記作爲參數進行記錄? (例如logger.info(globalMarker,「ACCEPT 1」)對嗎? –