简体   繁体   English

如何使用属性文件在 log4j.xml 中设置值?

[英]How to set value in log4j.xml using property file?

I would like to know if there is possibility to set an attribute in log4j.xml by using property file.我想知道是否有可能通过使用属性文件在 log4j.xml 中设置属性。 For example the log4j.xml:例如 log4j.xml:

<?xml version="1.0" encoding="iso-8859-1"?>
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">
<log4j:configuration>
 <appender name="CONSOLE" class="org.apache.log4j.ConsoleAppender">
    <param name="target" value="System.out" />
    <layout class="org.apache.log4j.PatternLayout">
        <param name="ConversionPattern" value="%d{ISO8601} %-5p [%c{1}] - %l - %m%n "/>
    </layout>        
</appender>
   <appender name="FILE" class="org.apache.log4j.FileAppender">
<param name="File" value="\D:\ReadText_File\log4jlogss.txt"/>
<param name="Append" value="true"/>
         <layout class="org.apache.log4j.PatternLayout">
                    <param name="ConversionPattern" value="%d{ISO8601} %-5p [%c{1}]- %l - %m%n"/>
        </layout>
</appender>
<logger name="org.apache">
<level value="WARN"/>
</logger>
<root>
    <level value="DEBUG"/>
    <appender-ref ref="FILE"/>
    <appender-ref ref="CONSOLE"/>
    </root>
   </log4j:configuration>

I wan to specify the path for the file using Properties file Any idea how can we do it ????我想使用属性文件指定文件的路径知道我们该怎么做吗????

about log4j, this type of configuration that XML configuration which embed the property file is not supported in current version.关于 log4j,当前版本不支持这种嵌入属性文件的 XML 配置的配置。

for details, you can view the log4j's source code .详情可以查看log4j的源码。 the XML parse details is in class DOMConfigurator.configure XML 解析详细信息在类 DOMConfigurator.configure 中

You can use Log4j Lookups with PropertySubstitution :您可以将 Log4j LookupsPropertySubstitution一起使用:

See http://logging.apache.org/log4j/2.x/manual/configuration.html#PropertySubstitution and http://logging.apache.org/log4j/2.x/manual/lookups.html请参阅http://logging.apache.org/log4j/2.x/manual/configuration.html#PropertySubstitutionhttp://logging.apache.org/log4j/2.x/manual/lookups.html

Here is an example on StackOverflow: Log4J2 property substitution - default (to make it short, you can use, as far as I know, the System Property using -Dkey=value )这是 StackOverflow 上的一个示例: Log4J2 属性替换 - 默认(为了简短起见,据我所知,您可以使用 System Property using -Dkey=value

You can also try to use the ${bundle:com.package.Messages:MyKey} syntax to use a properties file (using log4j 2.0-rc1 or greater), see https://stackoverflow.com/a/19303208/1149528 .您还可以尝试使用${bundle:com.package.Messages:MyKey}语法来使用属性文件(使用 log4j 2.0-rc1 或更高版本),请参阅https://stackoverflow.com/a/19303208/1149528

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM