XML configuration for log4j in order to show line No. in HTMLLayout

708 views Asked by At

I have a question on the log4j XML configuration. I want to have a HTMLLayout. But in the generated HTML file, there is no Line No. category. But I want to see it. I searched and it seems like to set LocationInfo to be true. But I don't know how to modify my XML.

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd" >
<log4j:configuration>
 <appender name="log" class="org.apache.log4j.FileAppender">
  <param  name="File"      value= "log4j.html"/>
  <param  name="Append"    value= "false"   />
  <layout class="org.apache.log4j.HTMLLayout"/> 
 </appender>

 <appender name="stdout" class="org.apache.log4j.ConsoleAppender">
  <layout class="org.apache.log4j.PatternLayout">
   <param name="ConversionPattern" value="%d [%06r] [%F:%L] %-10c %x  %m%n" />
  </layout>
 </appender>

 <root>
  <priority value="debug"/>
  <appender-ref ref="log"/>
    <!--appender-ref ref="stdout"/-->
 </root>
</log4j:configuration>
1

There are 1 answers

0
Paul Vargas On BEST ANSWER

In the javadoc 1 of org.apache.log4j.HTMLLayout states:

The LocationInfo option takes a boolean value. By default, it is set to false which means there will be no location information output by this layout. If the the option is set to true, then the file name and line number of the statement at the origin of the log statement will be output.

So, you just need to set it to true. Therefore, the configuration of your appender could be similar to:

<appender name="log" class="org.apache.log4j.FileAppender">
    <param name="file" value="log4j.html"/>
    <param name="threshold" value="debug"/>
    <param name="immediateFlush" value="true"/>
    <param name="append" value="false"/>
    <layout class="org.apache.log4j.HTMLLayout">
        <param name="Title" value="false"/>
        <param name="LocationInfo" value="true"/>
    </layout>
</appender>

Notes

  1. https://logging.apache.org/log4j/1.2/apidocs/org/apache/log4j/HTMLLayout.html