4

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

Developer
  • 161
  • 2
  • 3
  • 15

2 Answers2

1

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

  • we have a properties tag as well in log4j.xml so can u please guide me how to define a lookup for it ??? – Developer Apr 24 '14 at 06:40
1

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.

Community
  • 1
  • 1
xav
  • 5,452
  • 7
  • 48
  • 57
  • Yes I have read about it but don't know how to set Properties in Lookups?? How to use them is described but how to actually set properties in them is my prob. Any guidance would be appreciable Thank you for your response! – Developer Apr 24 '14 at 21:11
  • Here is an example: http://stackoverflow.com/questions/22028399/log4j2-property-substitution-default – xav Apr 24 '14 at 21:34