11 May 2015

Logging to File using log4j.xml

Here's log4j properties using xml to output log messages to a file.

log4j.xml

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">
<log4j:configuration debug="true"
  xmlns:log4j='http://jakarta.apache.org/log4j/'> 
  <appender name="file" class="org.apache.log4j.RollingFileAppender">
     <param name="append" value="true" />
     <param name="file" value="C:/mylogs/myLogFile.log" />
     <layout class="org.apache.log4j.PatternLayout">
        <param name="ConversionPattern" 
    value="%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n" />
     </layout>
  </appender>
  <root>
    <level value="DEBUG" />
    <appender-ref ref="file" />
  </root>
 </log4j:configuration>
From the above configuration the log messages will be written to a file inside 'C:/mylogs/' directory.





Popular Posts

Write to Us
Name
Email
Message