首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Log4J 1.2 PropertyConfigurator -> Log4J2

Log4J 1.2 PropertyConfigurator -> Log4J2
EN

Stack Overflow用户
提问于 2018-01-05 14:00:30
回答 2查看 13.5K关注 0票数 9

目前,我们的应用程序使用Log4J 1.2并使用

代码语言:javascript
复制
File file = ...
PropertyConfigurator.configure(file.getAbsolutePath());

代码语言:javascript
复制
URL url = ...
PropertyConfigurator.configure(url);

我知道属性文件的格式已经从1.2改为2,但是使用任意文件或Log4J配置属性文件的类似方法是什么呢?

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2018-01-05 14:12:34

来自Log4J 2文档

代码语言:javascript
复制
// import org.apache.logging.log4j.core.LoggerContext;

LoggerContext context = (org.apache.logging.log4j.core.LoggerContext) LogManager.getContext(false);
File file = new File("path/to/a/different/log4j2.xml");

// this will force a reconfiguration
context.setConfigLocation(file.toURI());

确保引用org.apache.logging.log4j.core.LoggerContext (在log4j-core工件中定义,而不是在log4j-api工件中定义)和将 not 引用到org.apache.logging.log4j.spi.LoggerContext

票数 4
EN

Stack Overflow用户

发布于 2020-01-13 10:21:44

您可以按以下方式使用PropertiesConfigurationBuilder

代码语言:javascript
复制
// Custom-loaded properties.
Properties props = ... 
// Beware it should be org.apache.logging.log4j.core.LoggerContext class,
// not the one ins spi package!
// Not sure about the meaning of "false".
LoggerContext context = (LoggerContext)LogManager.getContext(false);
Configuration config = new PropertiesConfigurationBuilder()
            .setConfigurationSource(ConfigurationSource.NULL_SOURCE)
            .setRootProperties(props)
            .setLoggerContext(context)
            .build();
 context.setConfiguration(config);
 Configurator.initialize(config);

的确,使用core类看起来像一个黑客,但作者自己在他的教程中使用了这些类:https://logging.apache.org/log4j/log4j-2.3/manual/customconfig.html

票数 7
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/48115143

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档