Converting log4j.properties to log4j.xml

15,109

Solution 1

You can convert your complete log4j.properties using this online service, where you can paste your log4j.properties, press convert and copy your new log4j.xml:

http://log4j-props2xml.appspot.com/

If that webapp is offline ... you can also start it in your own servlet container ... you find downloads and sources here:

https://github.com/jroyals/log4j-properties-converter/

Use of variables in log4j.xml:

This is explained in an answer to another question ... using XML internal entities here and using Java System Properties here.

Solution 2

You can set the log file location and name as a parameter element in the log4J xml

<param name="File" value="C:\\logs\\application\\ApplicationLog.log" />

Example file below:

<log4j:configuration>
    <appender name="STDOUT" class="org.apache.log4j.RollingFileAppender">
        <param name="File"     value="C:\\logs\\application\\ApplicationLog.${user.name}.log" />
        <param name="MaxFileSize" value="5000KB" />
        <param name="MaxBackupIndex" value="10" />
        <layout class="org.apache.log4j.PatternLayout">
            <param name="ConversionPattern"
        value="[%d{yyyy-MMM-dd HH:mm:ss}] [%t] %-5p %c{1}: %m %n" />
        </layout>
        <filter class="org.apache.log4j.varia.LevelRangeFilter">
            <param name="LevelMin" value="DEBUG"/>
            <param name="LevelMax" value="FATAL"/>
        </filter>
    </appender>

    <root>
        <level value="all" />
        <appender-ref ref="STDOUT"/>
    </root>
</log4j:configuration>
Share:
15,109
Hari Menon
Author by

Hari Menon

Updated on June 27, 2022

Comments

  • Hari Menon
    Hari Menon almost 2 years

    I couldn't find anywhere how to specify constants in log4j.xml. For example, I have this constant in my log4j.properties:

    #Log directory
    dal.log.dir=/var/log/pojodal/
    # Log filename
    dal.log.file=pojodal.log
    

    And I use these constants as follows, in other parts of the properties file:

    log4j.appender.DRFA1.File=${dal.log.dir}/${dal.log.file}
    

    How to achieve the same behavior in log4j.xml?

  • Hari Menon
    Hari Menon over 12 years
    But these are meant for params that come with the appender only right? What I need is custom variables, because our log4j.properties files tends to get messy, so we wanted to keep all variables at the top
  • Steve
    Steve over 12 years
    Then I think you'll need to set these outside the log4j.xml file. You'll need a separate properties file and use System.setProperty before Log4J gets configured. Then you can use them as ${prop1} throughout the log4J.xml