log4j Tutorial 9, Htmllayout

Source: Internet
Author: User
Tags log4j

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.