Loading
LOG4J HTML log file

Java Quick Notes

Refresh Your Java - Before Java Interview

We are Agile, believe in less Documentation - Only Quick notes (Java Interview Questions) of Java/J2ee Read more....


Not Included Yet
Not Included Yet
Not Included Yet
Not Included Yet
Not Included Yet
Not Included Yet
Not Included Yet
Not Included Yet
Not Included Yet
Not Included Yet
Not Included Yet
Not Included Yet
Not Included Yet
Not Included Yet
Not Included Yet
Not Included Yet
Not Included Yet
Not Included Yet
Not Included Yet
Not Included Yet

Go to SiteMap

Q)  How to create a HTML log file ?


Ans)

Step 1:

You need to create a Log4J property files , take a look at a sample below.

log = C://logs//log

log4j.rootLogger = INFO, FILE

# Define the file appender
log4j.appender.FILE=util.ownHTMLFileAppender

log4j.appender.FILE.File=${log}/ServiceResult.html

# Define the layout for file appender
log4j.appender.FILE.layout=org.apache.log4j.HTMLLayout
log4j.appender.FILE.layout.Title=HTML Layout Example
log4j.appender.FILE.layout.LocationInfo=true

Step 2:

Create your own "ownHTMLFileAppender"


package util;

import java.io.File;
import java.io.IOException;

import org.apache.log4j.FileAppender;
import org.apache.log4j.Layout;
import org.apache.log4j.spi.ErrorCode;

/**
* This is a customized log4j appender, which will create a new file for every
* run of the application.
*
*
*/
public class OwnHTMLFileAppender extends FileAppender {

public OwnHTMLFileAppender() {
}

public OwnHTMLFileAppender(Layout layout, String filename,
  boolean append, boolean bufferedIO, int bufferSize)
  throws IOException {
 super(layout, filename, append, bufferedIO, bufferSize);
}

public OwnHTMLFileAppender(Layout layout, String filename,
  boolean append) throws IOException {
 super(layout, filename, append);
}

public OwnHTMLFileAppender(Layout layout, String filename)
  throws IOException {
 super(layout, filename);
}

public void activateOptions() {
if (fileName != null) {
 try {
  fileName = getNewLogFileName();
  setFile(fileName, fileAppend, bufferedIO, bufferSize);
 } catch (Exception e) {
  errorHandler.error("Error while activating log options", e,
    ErrorCode.FILE_OPEN_FAILURE);
 }
}
}

private String getNewLogFileName() {
if (fileName != null) {
 final String DOT = ".";
 final String HIPHEN = "-";
 final File logFile = new File(fileName);
 final String fileName = logFile.getName();
 String newFileName = "";

 final int dotIndex = fileName.indexOf(DOT);
 if (dotIndex != -1) {
  // the file name has an extension. so, insert the time stamp
  // between the file name and the extension
  newFileName = fileName.substring(0, dotIndex) + HIPHEN
    + +System.currentTimeMillis() + DOT
    + fileName.substring(dotIndex + 1);
 } else {
  // the file name has no extension. So, just append the timestamp
  // at the end.
  newFileName = fileName + HIPHEN + System.currentTimeMillis();
 }
 return logFile.getParent() + File.separator + newFileName;
}
return null;
}
}

Step 3:

Read the Properties File

static {

PropertyConfigurator.configure("C://ServiceTestNew//conf//log4j.properties");

}

Step4:

private final static Logger log = Logger.getLogger(ValidateLogger.class);



Back to top

------------------------- We hope you got necessary Info On -----------------------------------------

LOG4J HTML log file


LOG4J HTML log file

-------------------------------------------------------------------------------------------------------



Face Book
Request for a Mock Interview/Training

Get a PDF

Face Book
Same look (Read) on any device, this is Ads free