Log4j - PatternLayout

Log4j provides org.apache.log4j.PattrernLayout class to generate your logging information in a particular format based on a pattern.

The PatternLayout extends the abstract org.apache.log4j.Layout class and overrides the format() method to structure the logging information according to a supplied pattern.

PatternLayout is also a simple layout object that provides the Bean Property i.e. conversionPattern, which can be set using the configuration file:

conversionPattern: This property is used to set the conversion pattern. Default is %r [%t] %p %c %x - %m%n

Pattern Conversion Characters

Let's see the following table describes the characters used in the conversion pattern and all other characters that we can use in our custom pattern:

Conversion CharacterMeaning
cIt is used to output the category of the logging event. For Example: for the category name x.y.z the pattern %c{2} will output y.z.
CIt is used to output the fully qualified class name of the caller issuing the logging request. For example, for the class name "org.apache.abc.MyClass", the pattern %C{1} will output "MyClass".
dIt is used to output the date of the logging event. For example, %d{HH:mm:ss,SSS} or %d{dd MMM yyyy HH:mm:ss,SSS}.
FIt is used to output the file name where the logging request was issued.
lIt is used to output location information of the caller which generated the logging event.
LIt is used to output the line number from where the logging request was issued.
mIt is used to output the application supplied message associated with the logging event.
MIt is used to output the method name where the logging request was issued.
nIt is used to give the output of platform-dependent line separator character or characters.
pOutputs the priority of the logging event.
rIt is used to output the number of milliseconds elapsed from the construction of the layout until the creation of the logging event.
tIt is used to output the name of the thread that generated the logging event.
xIt is used to output the NDC (nested diagnostic context) associated with the thread that generated the logging event.
XThe X conversion character is followed by the key for the MDC (Mapped Diagnostic Context). For example, X{clientIP} prints the information stored in the MDC against the key clientIP.
%The literal percent sign. %% will print a % sign.

Format Modifiers

By default, the relevant information is displayed as a normal output. However, log4j provides the format modifiers; with the help of this, it is possible to change the maximum field width, the minimum field width, and justification.

Let's see some modifiers:

Format modifierleft justifyminimum widthmaximum widthcomment
%20cfalse20noneLeft pad with spaces if the name of the category is less than 20 characters long.
%-20ctrue20noneRight pad with spaces if the name of the category is less than 20 characters long.
%.30cNAnone30Truncate from the beginning if the name of the category is longer than 30 characters.
%20.30cfalse2030Left pad with spaces if the name of the category is shorter than 20 characters. However, if the name of the category is longer than 30 characters, then truncate from the beginning.
%-20.30ctrue2030Right pad with spaces if the name of the category is shorter than 20 characters. However, if the category name is longer than 30 characters, then truncate from the beginning.

PatternLayout Example

Let's see one simple example for Patternlayout.

Following is a simple configuration file for PatternLayout:

log4j.properties:

Log4jExample.java:

When you compile and run the above program, you will get a log.out file in c:/usr/home/log4j directory which would have the following log information:

Output:

2019-09-16-main--DEBUG-Log4jExample:Hello this is an debug message
2019-09-16-main--INFO - Log4jExample:Hello this is an info message





Latest Courses