If you want to generate an HTML-formatted file, log information, you can use Org.apache.log4j.HTMLLayout to format the log information.
The Htmllayout class extends the abstract Org.apache.log4j.Layout class and overrides the format () method of its base class to provide HTML-style formatting.
This provides the following information to display:
The time elapsed from the beginning of the application before a specific log event is generated
The name of the thread that called the record request
The level associated with this record request
Log recorder (Logger) and name of the logged message
Optional program file location information from which to record the called line number
Htmllayout is a very simple layout object that provides the following methods:
S.N. |
Method & Description |
1 |
setContentType (String) Sets the content type of text/html as HTML content. Default is text/html |
2 |
Setlocationinfo (String) Set location information to log events. Default is False |
3 |
Settitle (String) Set to the title of the HTML file. The default value is log4j log information |
htmllayout Example:
Here is a simple configuration file for Htmllayout:
=/usr/home/= DEBUG, file# Define the FILE appenderlog4j.appender.FILE= Org.apache.log4j.FileAppenderlog4j.appender.FILE.File=${log}/ for FILE Appenderlog4j.appender.FILE.layout=Org.apache.log4j.HTMLLayoutlog4j.appender.FILE.layout.Title= HTML Layout Examplelog4j.appender.FILE.layout.LocationInfo=true
Now consider the following Java example for generating log information:
ImportOrg.apache.log4j.Logger;ImportJava.io.*;Importjava.sql.SQLException;ImportJava.util.*; Public classlog4jexample{/*Get Actual class name to being printed on*/ StaticLogger log =Logger.getlogger (log4jexample.class. GetName ()); Public Static voidMain (string[] args)throwsioexception,sqlexception{Log.debug ("Hello This is a debug message"); Log.info ("Hello This is an info message"); }}
Compile and run the above program, which will create the htmllayout.html file in the/usr/home/log4j directory, which will have the following log information:
Log session start time Mon Mar 13:30:24 AST 2014
You can open a htmllayout.html file by using a Web browser. It is also important to note that the footer
One big advantage of having an HTML-formatted log file is that it can be published as a Web page that can be viewed remotely.
log4j Tutorial 9, Htmllayout