log4j Tutorial 8, log formatting

Source: Internet
Author: User

Apache log4j provides a variety of layout objects, each of which can record data based on a variety of layout formats. Alternatively, you can create a layout object to format the specific application in the logging data method.

All layout objects-the Appender object receives the Loggingevent object. The layout object retrieves the message parameters from the Loggingevent and applies the appropriate objectrenderer to get the string representation of the message.

Layout type:

The top-level class in the hierarchy is an abstract class that is org.apache.log4j.Layout. This is the base class for all other layout classes in the log4j API.

The layout class is defined as abstract in the application, not directly using this class; Instead, use its subclass to work as follows:

    • Datelayout

    • Htmllayout (explained in this tutorial)

    • Patternlayout (explained in this tutorial)

    • Simplelayout

    • Xmllayout

Layout method:

This class provides a framework to implement all common operations on all other layout objects, and declares two abstract methods.

S.N. Method & Description
1 Public abstract Boolean ignoresthrowable ()
This method represents whether the log information handles any java.lang.Throwable objects that are passed to it as part of the logging event. If the layout object processes the Throwable object, the layout object does not ignore it and returns false.
2 Public abstract String Format (Loggingevent event)
Unique layout Subclasses will implement the layout-specific format of this method

In addition to these abstract methods, the layout class provides specific implementations of the following methods:

S.N. Method & Description
1 Public String getContentType ()
Returns the content type of the object using the layout. The base class will return Text/plain as the default content type
2 Public String Getfooter ()
Specify footer information for log messages
3 Public String GetHeader ()
Specify header information for log messages

Each subclass can return class-specific information by overriding the specific implementation of these methods.

log4j Tutorial 8, log formatting

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.