【发布时间】:2017-05-23 18:37:03
【问题描述】:
在我的 Log4j2 配置文件中,我有这个:
<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="WARN" strict="true" name="XMLConfig" packages="org.apache.logging.log4j.test">
<Properties>
<Property name="baseDir">log-dir/</Property>
<Property name="defaultLogfileName">default-log-file</Property>
</Properties>
现在,在我的一些代码中,我创建了自定义记录器。我需要访问“baseDir”的值并更改它。我试过在这样的上下文中使用getProperties:
LoggerContext context = (LoggerContext) LogManager.getContext(false);
Configuration configuration = context.getConfiguration();
configuration.getProperties();
但是返回的地图有键“hostname”和“contextName”。不是我正在寻找的属性地图。
我以为我可以从rootLogger获取它:
LoggerContext context = (LoggerContext) LogManager.getContext(false);
Configuration configuration = context.getConfiguration()
for (Property p : configuration.getRootLogger().getPropertyList())
{
...
}
但这会产生 NullPointerException,因为 getPropertyList 返回 null。
那么,我如何访问名为“baseDir”的属性,以便以编程方式创建一个新的记录器,但使用不同的基本目录?
【问题讨论】: