目前,我们的应用程序使用 Log4J 1.2 并使用以下任一方式对其进行配置
File file = ...
PropertyConfigurator.configure(file.getAbsolutePath());
or
URL url = ...
PropertyConfigurator.configure(url);
我知道属性文件格式已从 1.2 更改为 2,但是使用任意文件或 URL 处的属性文件配置 Log4J 2 的类似方法是什么?
您可以使用PropertiesConfigurationBuilder
如下:
// 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
类看起来像是一个 hack,但作者本人在他的教程中使用了它们:https://logging.apache.org/log4j/log4j-2.3/manual/customconfig.html https://logging.apache.org/log4j/log4j-2.3/manual/customconfig.html .
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)