本文介绍了以编程方式从Log4j2 XML配置获取属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的Log4j2配置文件中,我有这个:

 <?xml version =1.0encoding =UTF -8\" >?; 
< Configuration status =WARNstrict =truename =XMLConfigpackages =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); 
配置配置= context.getConfiguration();
configuration.getProperties();

但是返回的地图有hostname和contextName键。不是我正在寻找的属性地图。



我认为我可以从rootLogger获取它:

  LoggerContext context =(LoggerContext)LogManager.getContext(false); 
配置配置= context.getConfiguration()
for(属性p:configuration.getRootLogger()。getPropertyList())
{
...
}

但这会产生NullPointerException,因为getPropertyList返回null。



解决方案

context.getConfiguration()返回的类 Configuration 不是一种 PropertyConfiguration 类。它不能用于访问log4j2.xml值,因为它是非常不同的 Configuration 类,专为日志记录设置而定。



它是可以将 baseDir 定义提取到单独的属性文件中。这为编程和非编程log4j配置提供了通用源:以编程方式,它可以作为常规属性配置文件进行访问; log4j2配置可以作为属性查找来访问它。



这看起来像这样:


  1. 外部属性文件logsCommons.properties与log4j2.xml位于同一文件夹中,并具有以下属性:

      baseDir = log-dir 


  2. log4j2xml的定义如下:

     <性状> 
    < Property name =baseDir> $ {bundle:logsCommons:baseDir} /< / Property>
    < Property name =defaultLogfileName> default-log-file< / Property>
    < / Properties>







来自OP:



除了将baseDir的值移动到属性文件并将其引用为 $ {bundle:logsCommon:baseDir} 如上所述,我这样做是为了得到我的实际解决方案:



获取StrSubstitutor:

  String baseDirVar = configuration.getStrSubstitutor()。getVariableResolver()。look up(baseDir); 

然后我需要更换:

  String baseDir = configuration.getStrSubstitutor()。replace(baseDirVar); 

现在我可以可靠地获取(并在必要时修改)用于记录的基本目录,现在来自属性文件。


In my Log4j2 config file, I have this:

<?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>

Now, in some of my code, I create custom loggers. I need to access the value of "baseDir" and change it. I've tried using getProperties from the context like this:

LoggerContext context = (LoggerContext) LogManager.getContext(false);
Configuration configuration = context.getConfiguration();
configuration.getProperties();

But the map that comes back has the keys "hostname" and "contextName". Not the properties map that I was looking for.

I thought that I might be able to get it from the rootLogger:

LoggerContext context = (LoggerContext) LogManager.getContext(false);
Configuration configuration = context.getConfiguration()
for (Property p : configuration.getRootLogger().getPropertyList())
{
    ...
}

But this yields a NullPointerException because getPropertyList returns null.

So, how can I access the property with the name "baseDir" so that I can programmatically create a new logger, but with a different base directory?

解决方案

The class Configuration, which is returned by context.getConfiguration() is not a kind of PropertyConfiguration class. It cannot be used for access of log4j2.xml values, since it is the very different Configuration class, tailored for logging settings.

It is possible to extract baseDir definition into separate properties file. This provides the common source for both programmatic and non-programmatic log4j configuration: programmatically it can be accessed as a regular property configuration file; log4j2 configuration can access it as a properties lookup.

This looks something like this:

  1. The external property file logsCommons.properties is located in the same folder with log4j2.xml and has the property:

    baseDir=log-dir
    

  2. The log4j2xml is defined as following:

    <Properties>
        <Property name="baseDir">${bundle:logsCommons:baseDir}/</Property>
        <Property name="defaultLogfileName">default-log-file</Property>
    </Properties>
    


From the OP:

In addition to moving the value of baseDir to a properties file and references it as ${bundle:logsCommon:baseDir} as described above, I did this to get to my actual solution:

get the StrSubstitutor:

String baseDirVar = configuration.getStrSubstitutor().getVariableResolver().look‌​up("baseDir"); 

Then I needed to do a replacement:

String baseDir = configuration.getStrSubstitutor().replace(baseDirVar);

Now I can reliably get (and modify when necessary) the base directory for logging that now comes from the properties file.

这篇关于以编程方式从Log4j2 XML配置获取属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-20 04:30