The pattern layout is a flexible layout approach for file appenders. You specify what information you want to include in the log file by providing a string of characters in the Pattern field of a file appender. These instructions are interpreted by the system, so that the resulting log file contains the information you need.
The pattern for a file appender is constructed using conversion specifiers and static characters you might want to include, as demonstrated in the Pattern Example.
Conversion specifiers indicate what information to write to the log. The following table describes all possible conversion specifiers for pattern layouts.
Log4j® Pattern Layout Conversion Specifiers
Conversion specifier |
Output in log file |
%c |
The name of the logger with which the logging event is associated. If desired, you can specify the desired number of right-most components of the logger name to be printed in the log by following the %c with a precision specifier (a decimal constant within braces). By default, the logger name is printed in full. For an example of how the Default system log uses a precision specifier with this conversion specifier, see Pattern Example. |
%C |
The fully qualified class name of the calling object issuing the logging request. If desired, you can specify the desired number of right-most components of the class name to be printed in the log by following the %C with a precision specifier (a decimal constant within braces). See Pattern Example for an example of how the Default system log uses a precision specifier with this conversion specifier. Caution: Generating this information for a log is extremely slow. |
%d |
The date of the logging event. If desired, you can follow the %d with a date format specifier within braces. For example, %d{HH:mm:ss,SSS} and %d{dd MMM yyyy HH:mm:ss} are acceptable. By default, ISO8601 format is used. Log4j® has its own date formatters. These can be specified using one of the following: •ABSOLUTE •DATE •ISO8601 See the Pattern Example for an example of how the Default system log uses a date format specifier with this conversion specifier. |
%F |
The file name where the logging request was issued. Caution: Generating this information for the log is extremely slow. |
%l |
The location information of the calling object which generated the logging event. Location information includes the file name where the logging request was issued, the line number in the file, the class name, and the method name from which the message was logged. Caution: Generating this information for the log is extremely slow. |
%L |
The line number of the file where the logging request was issued. Caution: Generating this information for the log is extremely slow. |
%m |
The message associated with the logging event. |
%M |
The method name where the logging request was issued. Caution: Generating this information for the log is extremely slow. |
%n |
The platform-dependent line separator character or characters. |
%p |
The level of the logging event. |
%r |
The number of milliseconds elapsed from the start of TeamConnect until the creation of the logging event. |
%t |
The name of the thread that generated the logging event. |
%% |
A single percent sign. |
The following pattern shows the use of several conversion specifiers that write information to the log. This is the pattern used by the Default system log:
%d{ABSOLUTE} [%t] [%p] [%c{1}] %C{1}.%M - %m%n
This can be roughly translated as the following:
date [thread that generated the logging event] [level of the logging event] [logger of the logging event] class.method - message followed by a line return
For example, an entry in the Default system log might be:
11:20:44,005 [jessica] [DEBUG] [sql] TCLog$SQLLogWriter.write - UnitOfWork(2420720)—end unit of work commit
Notice that the spaces, brackets, hyphen and period in this example are printed in the generated line of text in the log as static text.