Saturday 17 October 2015

log4j2: HTMLLayout: generate log message in HTML format

The HTMLLayout generates an HTML page and adds each LogEvent to a row in a table.
log4j2.xml
<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="warn" name="MyApp" packages="">
 <Appenders>
  <File name="my_file_appender" fileName="/Users/harikrishna_gurram/log.html">
   <HTMLLayout />
  </File>
 </Appenders>

 <Loggers>
  <Root level="info">
   <AppenderRef ref="my_file_appender" />
  </Root>
 </Loggers>
</Configuration>

package log4j_tutorial;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

public class HelloWorld {
 private static final Logger logger = LogManager.getLogger();

 public static void main(String args[]) throws InterruptedException {
  int val1 = 10, val2 = 11, val3 = 12;

  logger.trace("val1={}, val2={}, val3={}", val1, val2, val3);
  logger.debug("val1={}, val2={}, val3={}", val1, val2, val3);
  logger.info("val1={}, val2={}, val3={}", val1, val2, val3);
  logger.warn("val1={}, val2={}, val3={}", val1, val2, val3);
  logger.error("val1={}, val2={}, val3={}", val1, val2, val3);
  logger.fatal("val1={}, val2={}, val3={}", val1, val2, val3);

 }

}

Run HelloWorld application, you will get following kind of output.

Following are the HTML Layout Parameters.
Parameter
Type
Description
charset
String
The character set to use when converting the HTML String to a byte array. Default is UTF-8.
contentType
String
The value to assign to the Content-Type header. The default is "text/html".
locationInfo
boolean
Default value is false. If true, the filename and line number will be included in the HTML output. Generating location information is an expensive operation and may impact performance.
title
String
Specifies HTML title.



Prevoius                                                 Next                                                 Home

No comments:

Post a Comment