gpt4 book ai didi

java - 以编程方式更改 Log4j 2.x 的输出格式

转载 作者:行者123 更新时间:2023-11-30 10:32:15 26 4
gpt4 key购买 nike

我正在使用以下代码从 manual 配置 Log4j .我想改变模式。无论我如何编辑 pattern 属性,输出格式都保持不变。如何更改输出格式?我想将 pattern 设置为 %-5level: %msg%n%throwable

ConfigurationBuilder<BuiltConfiguration> builder = ConfigurationBuilderFactory.newConfigurationBuilder();
builder.setStatusLevel(Level.ERROR);
builder.setConfigurationName("BuilderTest");
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")));
ctx = Configurator.initialize(builder.build());

最佳答案

似乎工作正常。我在测试类中有以下内容(实例初始化程序中的代码):

{
ConfigurationBuilder<BuiltConfiguration> builder = ConfigurationBuilderFactory.newConfigurationBuilder();
builder.setStatusLevel(Level.ERROR);
builder.setConfigurationName("BuilderTest");
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 Thread:[%t] %-5level: MSG: %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")));
Configurator.initialize(builder.build());
}

使用 Configurator.initialize() 确保新构建的配置用作新的默认配置,因此在创建 Logger 时不必显式使用创建的上下文.

@Test
public void testLogger() throws Exception {
Logger log = LogManager.getLogger("loggerName");
log.error("Error");
log.warn("Warning");
log.debug("Debug");
log.info("Info");
}

以上结果(请注意修改后的模式):

2017-03-14 16:29:03,188 Thread:[main] ERROR: MSG:Error
2017-03-14 16:29:03,190 Thread:[main] WARN : MSG:Warning
2017-03-14 16:29:03,190 Thread:[main] DEBUG: MSG:Debug
2017-03-14 16:29:03,190 Thread:[main] INFO : MSG:Info


编辑:最初显式使用 LoggerContext.getLogger() 不是必需的,常规 LogManager.getLogger() 可确保自定义配置在创建记录器时使用。

关于java - 以编程方式更改 Log4j 2.x 的输出格式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42790663/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com