Javatpoint Logo
Javatpoint Logo

Log4J Log Formatting

Apache log4j supports various Layout objects, each of which can format logging data according to various layouts. We can also create a Layout object that formats logging data in an application-specific way.

All Layout objects receive a LoggingEvent object from the Appender objects. The Layout objects then retrieve the message argument from the LoggingEvent and apply the appropriate ObjectRenderer to get the String representation of the message.

Layout Types

There is one top-level abstract class in log4j is org.apache.log4j.Layout. It is the base class for all other layout classes in the log4j API.

The Layout class is defined as an abstract class within an application; we don?t need to use this class directly; instead, we work on subclasses which are as follows:

Layout Methods

This class provides a skeleton implementation of all common operations across all other objects and declares two abstract methods:

S.No. Methods Description
1. public abstract boolean ignoresThrowable() This method defines whether the logging information handles any java.lang.Throwable object passed to it as a part of the logging event. If the Layout objects manage the Throwable object, then the Layout object does not ignore it and returns false.
2. public abstract String format(LoggingEvent event) This method is implemented by individual layout subclasses for layout specific formatting.

Apart from these abstract methods, the Layout class provides concrete implementation for the methods given below:

S.No. Methods Description
1. public String getContentType() This method returns the content type used by the Layout objects. The base class returns text or plain as the default content type.
2. public String getFooter() This method is used to specify the footer information of the logging message.
3. public String getHeader() This method is used to specify the header information of the logging message.

Each subclass can return information related to class by overriding the concrete implementation of these methods.

Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Trending Technologies

B.Tech / MCA