以编程方式加载Log4j2配置文件 [英] Load Log4j2 configuration file programmatically
本文介绍了以编程方式加载Log4j2配置文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想以编程方式从我的应用程序加载Log4j2 XML配置文件。
I want to load Log4j2 XML configuration file programmatically from my application.
试过这个:
ConfigurationSource source = new ConfigurationSource();
source.setLocation(logConfigurationFile);
Configurator.initialize(null, source);
这个:
ConfigurationSource source = new ConfigurationSource();
source.setLocation(logConfigurationFile);
ConfigurationFactory factory = (ConfigurationFactory) XMLConfigurationFactory.getInstance().getConfiguration(source);
ConfigurationFactory.setConfigurationFactory(factory);
但是没有任何效果。
推荐答案
自己找到答案。有人可能会发现它很有用。
Found the answer myself. Someone might find it useful.
ConfigurationSource source = new ConfigurationSource();
source.setLocation(logConfigurationFile);
source.setFile(new File(logConfigurationFile));
source.setInputStream(new FileInputStream(logConfigurationFile));
Configurator.initialize(null, source);
这篇关于以编程方式加载Log4j2配置文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文