728x90 AdSpace

Friday, 6 September 2013

Log4j – HTML Layout


In this tutorial we are going to see how to generate HTML formatted file using log4j, org.apache.log4j.HTMLLayout is used to generate HTML formatted log file.
The HTMLLayout class extends the org.apache.log4j.Layout class which is abstract class and overrides the format() method from its base class.
Create java project called Log4jExample in your Eclipse IDE, then create package called com.javatutorialscorner.log4j.htmllayout under log4j example,add log4j jar in your build path.I used log4j-1.2.17.jar jar for this application.you can download this jar from Apache web site.Create log4j.property  in your project directory or source directory, add the following configuration
log4j.property
#The root logger 
logFile = C:/log
log4j.rootLogger = DEBUG, HTML

#The file appender
log4j.appender.HTML=org.apache.log4j.FileAppender
log4j.appender.HTML.File=${logFile}/javatutorialscorner.html

#The layout for file appender
log4j.appender.HTML.layout=org.apache.log4j.HTMLLayout
log4j.appender.HTML.layout.Title=Java Tutorials Corner - Log4j HTML Layout Example

rootLogger used to configure Level and Appender, in this example I used DEBUG as Level and org.apache.log4j.FileAppender is used as appender(HTML is name of appender).

HTMLLayout is used to configure log4j message in HTML format.

Now create java class called Log4jHTMLLayout under com.javatutorialscorner.log4j.htmllayout package.

Log4jHTMLLayout.java

package com.javatutorialscorner.log4j.htmllayout;

import org.apache.log4j.BasicConfigurator;
import org.apache.log4j.Logger;
import org.apache.log4j.PropertyConfigurator;

public class Log4jHTMLLayout {

private static final String FILE_PATH = "log4j.properties";
private static final Logger log = Logger.getLogger(Log4jHTMLLayout.class);

/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
if (FILE_PATH == null) {
BasicConfigurator.configure();
log.info("Log4j Configured using Basic Configurator");
} else {
PropertyConfigurator.configure(FILE_PATH);
log.info("Log4j Configured using Property Configurator");
}

log.debug("Debug message by Java Tutorials Corner");
log.info("Info message by Java Tutorials Corner");
log.warn("Warn message by Java Tutorials Corner");
log.error("Error message by Java Tutorials Corner");
log.fatal("Fatal message by Java Tutorials Corner");
}
}

Create instance for Logger class , getLogger(Log4jHTMLLayout.class) method accept fully qualified class name as its argument. In this example if property file not exist in specified location then BasicConfigurator.configure() used to initialize log4j application otherwise  DOMConfigurator.configure(FILE_PATH)  loads the configuration from property file .

now you can run the application, you will see the following output at specified location as HTML file. 

Note: If you edit output html you can see </body> and </html> missing

OUTPUT:

htmlLayout
Annamalai Thangaraj

Annamalai is working as Technical Lead in Leading Telecom company with 5+ years experience in Identity and Access Management , Telecom and Networks, BigData, Java, Spring, Struts, Hibernate, AngularJS, and Enterprise Web Application Development.

Website: Java Tutorials Corner

Shop and help us

Flipkart Offer Snapdeal offer Amazon.in offer Amazon.com offer
  • Blogger Comments
  • Facebook Comments
  • Disqus Comments

0 comments:

Post a Comment

Item Reviewed: Log4j – HTML Layout Rating: 5 Reviewed By: Annamalai Thangaraj