Home Tutorials Log4j Use of HTMLLayout in Log4j
Questions:Ask|Latest


 
 

Share on Google+Share on Google+

Use of HTMLLayout in Log4j

Advertisement
In this section we will describe you about HTMLLayout class. This layout outputs events in a HTML table.

Use of HTMLLayout in Log4j

     

In this section we will describe you about HTMLLayout class. This layout outputs events in a HTML table. Appenders using this layout should have their encoding set to UTF-8 or UTF-16, otherwise events containing non ASCII characters could result in corrupted log files. 

The HTMLLayout outputs events in a fixed format table. Each row of the table corresponds to an event while five columns: Time, Thread, Level, Logger Category and Message correspond to member fields of the logging event.

In this example we have used FileAppender for appending output into the HTML file.

Here is the example code of HTMLLayout in Log4j :

import org.apache.log4j.*;

public class HTMLLayoutExample {
  static Logger logger = Logger.getLogger("HTMLLayoutExample.class");
  public static void main(String[] args)throws Exception {
  FileAppender fileappender = 
  new 
FileAppender(new HTMLLayout(),"HTMLFile.html");
  logger.addAppender(fileappender);
  logger.info("Welcome");
  logger.info("to");
  logger.info("Rose India");
  logger.info("-----------");
  }
}

To run this example code follow these steps :

  1. Create and Save HTMLLayoutExample.java
  2. Compile and execute HTMLLayoutExample

You will have an HTMLFile.html file as output with the following representation:

Output:

Download Source Code

     

Advertisements

Liked it!  Share this Tutorial


Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Posted on: September 12, 2008

Ask Questions?    Discuss: Use of HTMLLayout in Log4j  

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments
emrah
September 25, 2012
change the column

Thank you for the code. How can i change the columns, which should be shown?or the names of the column?
DMCA.com