简体   繁体   中英

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. For example the 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.

for details, you can view the log4j's source code . the XML parse details is in class DOMConfigurator.configure

You can use Log4j Lookups with PropertySubstitution :

See http://logging.apache.org/log4j/2.x/manual/configuration.html#PropertySubstitution and http://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 )

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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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