Users more often wish to customize their output according to their output format. Layout can be set by attaching it with the appender. The Layout is responsible for formatting the logging output.
There are following layout classes in Log4j:
Pattern Layout :
In our this section we are describing about most general layout PatternLayout. It is a flexible layout configurable with its pattern string. A conversion pattern consists of literal text and format control expressions. Here is the list of some of the conversion characters used for formatting:
|c||Used to output the category of logging event|
|C||Used to output the fully qualified class name of the caller issuing the logging request|
|d||Used to output date of the logging event|
|m||Used to output the application supplied message associated with the logging event|
|n||Outputs the platform dependent line separator character or characters|
|Used to output the priority of the logging event|
|r||Used to output the number of milliseconds elapsed from the construction of the layout until the creation of the logging event|
|t||Used to output name of thread of logging event|
|F||Used to output the file name|
There are two constructors in the PatternLayout class:
Here is the example which uses PatternLayout class for creating output in the "%r [%t] %-5p %c %x - %m%n" pattern. Following is the source code for PatternLayoutExample.java :
After creating and executing this file there will be created an "Output.log" file and log events will be added into it. As output you will have an "Output.log" file with the following line.
|0 [main] INFO PatternLayoutExample - Hello !
0 [main] INFO PatternLayoutExample - Welcome
0 [main] DEBUG PatternLayoutExample - Exiting from main method
Liked it! Share this Tutorial