File=tm1event.log log4j.appender.S2.Format=TM1Event: Format. Specifies the format of the logs. For example: log4j.appender.S2.Format=TM1Event. Where the format is TM1Event or TM1Top. For example, to dynamically monitor the threads that are running and outputs thread status to the tm1top.log. In the example, the FileAppender appends log events to the log file named crawl.log (which is created in the current working directory). The ConsoleAppender writes to the console using a simple pattern in which only the messages are printed, but not the more verbose information (logging level, timestamp, and so on).
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 Character | Meaning |
---|---|
c | It 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. |
C | It 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'. |
d | It 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}. |
F | It is used to output the file name where the logging request was issued. |
l | It is used to output location information of the caller which generated the logging event. |
L | It is used to output the line number from where the logging request was issued. |
m | It is used to output the application supplied message associated with the logging event. |
M | It is used to output the method name where the logging request was issued. |
n | It is used to give the output of platform-dependent line separator character or characters. |
p | Outputs the priority of the logging event. |
r | It is used to output the number of milliseconds elapsed from the construction of the layout until the creation of the logging event. |
t | It is used to output the name of the thread that generated the logging event. |
x | It is used to output the NDC (nested diagnostic context) associated with the thread that generated the logging event. |
X | The 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 modifier | left justify | minimum width | maximum width | comment |
---|---|---|---|---|
%20c | false | 20 | none | Left pad with spaces if the name of the category is less than 20 characters long. |
%-20c | true | 20 | none | Right pad with spaces if the name of the category is less than 20 characters long. |
%.30c | NA | none | 30 | Truncate from the beginning if the name of the category is longer than 30 characters. |
%20.30c | false | 20 | 30 | Left 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.30c | true | 20 | 30 | Right 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:
Log4j Appender File File For Mac Os
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: