blob: adc11a4636f1531ce2b3ea383ee8a6ba847aa582 [file] [log] [blame]
<?xml version="1.0"?>
<!--
Licensed to the Apache Software Foundation (ASF) under one or more
contributor license agreements. See the NOTICE file distributed with
this work for additional information regarding copyright ownership.
The ASF licenses this file to You under the Apache License, Version 2.0
(the "License"); you may not use this file except in compliance with
the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->
<document>
<properties>
<title>Log4j 2 Layouts</title>
<author email="rgoers@apache.org">Ralph Goers</author>
</properties>
<body>
<section name="Layouts">
<p>
Layouts are used by Appenders to format the LogEvent into a form that meets the needs of whoever will be
consuming the log events. In Log4j 1.x and Logback Layouts were expected to transform an event into a
String. In Log4j 2.0 Layouts return a byte array. This allows the result of the Layout to be useful in
many more types of Appenders. However, this means most Layouts need to be configured with a
<a href="http://download.oracle.com/javase/6/docs/api/java/nio/charset/Charset.html">Charset</a> to
insure the byte array contains correct values.
</p>
<a name="HTMLLayout"/>
<subsection name="HTMLLayout">
<p>
The HTMLLayout generates an HTML page and adds each LogEvent to a row in a table.
</p>
<table border="1" width="100%">
<tr>
<th>Parameter Name</th>
<th>Type</th>
<th>Description</th>
</tr>
<tr>
<td>charset</td>
<td>String</td>
<td>The character set to use when converting the HTML String to a byte array. The value must be
a valid <a href="http://download.oracle.com/javase/6/docs/api/java/nio/charset/Charset.html">Charset</a>.
If not specified, the default system Charset will be used.</td>
</tr>
<tr>
<td>contentType</td>
<td>String</td>
<td>The value to assign to the Content-Type header. The default is "text/html".</td>
</tr>
<tr>
<td>locationInfo</td>
<td>boolean</td>
<td>If true, the filename and line number will be included in the HTML output. The default value is
false.</td>
</tr>
<tr>
<td>title</td>
<td>String</td>
<td>A String that will appear as the HTML title.</td>
</tr>
<caption align="top">HTML Layout Parameters</caption>
</table>
</subsection>
<a name="PatternLayout"/>
<subsection name="PatternLayout">
<p>
A flexible layout configurable with pattern string. The goal of this class is to format a LogEvent and
return the results. The format of the result depends on the <em>conversion pattern</em>.
</p>
<p>
The conversion pattern is closely related to the conversion pattern of the printf function in C.
A conversion pattern is composed of literal text and format control expressions called
<em>conversion specifiers</em>.
</p>
<p>
<i>Note that any literal text may be included in the conversion pattern.</i>
</p>
<p>
Each conversion specifier starts with a percent sign (%) and is followed by optional <em>format
modifiers</em> and a <em>conversion character</em>. The conversion character specifies the type of
data, e.g. category, priority, date, thread name. The format modifiers control such things as field width,
padding, left and right justification. The following is a simple example.
</p>
<p>
Let the conversion pattern be <b>"%-5p [%t]: %m%n"</b> and assume that the log4j environment was set to
use a PatternLayout. Then the statements
<pre>
Logger logger = LogManager.getLogger("MyLogger");
logger.debug("Message 1");
logger.warn("Message 2");
</pre>
would yield the output
<pre>
DEBUG [main]: Message 1
WARN [main]: Message 2
</pre>
</p>
<p>
Note that there is no explicit separator between text and conversion specifiers. The pattern parser
knows when it has reached the end of a conversion specifier when it reads a conversion character.
In the example above the conversion specifier <b>%-5p</b> means the priority of the logging event should
be left justified to a width of five characters.
</p>
<table border="1" width="100%">
<tr>
<th>Parameter Name</th>
<th>Type</th>
<th>Description</th>
</tr>
<tr>
<td>charset</td>
<td>String</td>
<td>The character set to use when converting the syslog String to a byte array. The String must be
a valid<a href="http://download.oracle.com/javase/6/docs/api/java/nio/charset/Charset.html">Charset</a>.
If not specified, the default system Charset will be used.
</td>
</tr>
<tr>
<td>pattern</td>
<td>String</td>
<td></td>
</tr>
<tr>
<td>replace</td>
<td>RegexReplacement</td>
<td>Allows portions of the resulting String to be replaced. If configured, the replace element must
specify the regular expression to match and the substitution. This performs a function similar to
the RegexReplacement converter but applies to the whole message while the converter only
applies to the String its pattern generates.
</td>
</tr>
<caption align="top">Pattern Layout Parameters</caption>
</table>
<h4>Patterns</h4>
<p>
The conversions that are provided with Log4j are:
</p>
<table border="1" width="100%">
<tr>
<th>Conversion Pattern</th>
<th>Description</th>
</tr>
<tr>
<td align="center">
<b>c</b>{precision}<br />
<b>logger</b>{precision}
</td>
<td>
<p>
Used to output the name of the logger that published the logging event. The logger conversion
specifier can be optionally followed by<em>precision specifier</em>, which consists of a
decimal integer, or a pattern starting with a decimal integer.
</p>
<p>If a precision specifier is given and it is an integer value, then only the corresponding number
of right most components of the logger name will be printed. If the precision contains
other non-integer characters then the name will be abbreviated based on the pattern. If the
precision integer is less than one the right-most token will still be printed in full.
By default the logger name is printed in full.
</p>
<table border="1" width="100%">
<tr>
<th>Conversion Pattern</th>
<th>Logger Name</th>
<th>Result</th>
</tr>
<tr>
<td>%c{1}</td>
<td>org.apache.commons.Foo</td>
<td>Foo</td>
</tr>
<tr>
<td>%c{2}</td>
<td>org.apache.commons.Foo</td>
<td>commons.Foo</td>
</tr>
<tr>
<td>%c{1.}</td>
<td>org.apache.commons.Foo</td>
<td>o.a.c.Foo</td>
</tr>
<tr>
<td>%c{1.1.~.~}</td>
<td>org.apache.commons.test.Foo</td>
<td>o.a.~.~.Foo</td>
</tr>
<tr>
<td>%c{.}</td>
<td>org.apache.commons.test.Foo</td>
<td>....Foo</td>
</tr>
</table>
</td>
</tr>
<tr>
<td align="center">
<b>C</b>{precision}<br />
<b>class</b>{precision}
</td>
<td>
<p>
Used to output the fully qualified class name of the caller issuing the logging request.
This conversion specifier can be optionally followed by<em>precision specifier</em>, that
follows the same rules as the logger name converter.
</p>
</td>
</tr>
<tr>
<td align="center">
<b>d</b>{pattern}<br />
<b>date</b>{pattern}
</td>
<td>
<p>
Used to output the date of the logging event. The date conversion specifier may be
followed by a set of braces containing a date and time pattern strings
<a href="http://docs.oracle.com/javase/6/docs/api/java/text/SimpleDateFormat.html">
SimpleDateFormat</a>,
<em>ABSOLUTE</em>,
<em>DATE</em>
or
<em>ISO8601</em>
and a set of braces containing a time zone id per
<a href="http://docs.oracle.com/javase/6/docs/api/java/util/TimeZone.html#getTimeZone(java.lang.String)">
java.util.TimeZone.getTimeZone</a>.
For example,<b>%d{HH:mm:ss,SSS}</b>,<b>%d{dd&nbsp;MMM&nbsp;yyyy&nbsp;HH:mm:ss,SSS}</b>,
<b>%d{DATE}</b>
or<b>%d{HH:mm:ss}{GMT+0}</b>. If no date format specifier is given then
ISO8601 format is assumed.
</p>
</td>
</tr>
<tr>
<td align="center">
<b>ex</b>{depth}<br />
<b>exception</b>{depth}<br />
<b>throwable</b>{depth}
</td>
<td>
<p>Used to output the Throwable trace that has been bound to the LoggingEvent, by
default this will output the full trace as one would normally find by a call to
Throwable.printStackTrace().
The throwable conversion word can be followed by an option in the form
<b>%throwable{short}</b>
which will only output the first line of the Throwable or <b>%throwable{n}</b> where
the first n lines of the stacktrace will be printed.
</p>
</td>
</tr>
<tr>
<td align="center">
<b>F</b><br />
<b>file</b>
</td>
<td>
Used to output the file name where the logging request was issued.
</td>
</tr>
<tr>
<td align="center">
<b>K</b>{key}<br />
<b>map</b>{key}<br />
<b>MAP</b>{key}
</td>
<td>
<p>Used to output the entries in a <a href="../log4j2-api/apidocs/index.html">MapMessage</a>,
if one is present in the event. The <b>K</b> conversion character can be followed by the key
for the map placed between braces, as in
<b>%K{clientNumber}</b> where <code>clientNumber</code> is the key. The value in the Map
corresponding to the key will be output. If no additional sub-option
is specified, then the entire contents of the Map key value pair set
is output using a format {{key1,val1},{key2,val2}}
</p>
</td>
</tr>
<tr>
<td align="center">
<b>l</b><br />
<b>location</b>
</td>
<td>
<p>
Used to output location information of the caller which generated the logging event.
</p>
<p>
The location information depends on the JVM implementation but usually consists of the fully
qualified name of the calling method followed by the callers source the file name and line
number between parentheses.
</p>
</td>
</tr>
<tr>
<td align="center">
<b>L</b><br />
<b>line</b>
</td>
<td>Used to output the line number from where the logging request
was issued.
</td>
</tr>
<tr>
<td align="center">
<b>m</b><br />
<b>msg</b><br />
<b>message</b>
</td>
<td>Used to output the application supplied message associated with the logging event.
</td>
</tr>
<tr>
<td align="center">
<b>M</b><br />
<b>method</b>
</td>
<td>Used to output the method name where the logging request was issued.
</td>
</tr>
<tr>
<td align="center">
<b>marker</b>
</td>
<td>The name of the marker, if one is present.</td>
</tr>
<tr>
<td align="center">
<b>n</b>
</td>
<td>
<p>
Outputs the platform dependent line separator character or characters.
</p>
<p>This conversion character offers practically the same
performance as using non-portable line separator strings such as
"\n", or "\r\n". Thus, it is the preferred way of specifying a
line separator.
</p>
</td>
</tr>
<tr>
<td align="center">
<b>p</b><br />
<b>level</b>
</td>
<td>Used to output the level of the logging event.</td>
</tr>
<tr>
<td align="center">
<b>r</b><br />
<b>relative</b>
</td>
<td>Used to output the number of milliseconds elapsed since the JVM was started until the creation
of the logging event.
</td>
</tr>
<tr>
<td align="center">
<b>replace</b>{pattern}{regex}{substitution}
</td>
<td>
<p>
Replaces occurrences of 'regex', a regular expression, with its replacement 'substitution' in the
string resulting from evaluation of the pattern. For example, "%replace(%msg}{\s}{}" will remove
all spaces contained in the event message.
</p>
<p>
The pattern can be arbitrarily complex and in particular can contain multiple conversion keywords.
For instance, "%replace{%logger %msg}{\.}{/}" will replace all dots in the logger or the message of
the event with a forward slash.
</p>
</td>
</tr>
<tr>
<td align="center">
<b>rEx</b>{depth}<br />
<b>rException</b>{depth}<br />
<b>rThrowable</b>{depth}
</td>
<td>
<p>
The same as the %throwable conversion word but the stack trace is printed starting with the
first exception that was thrown followed by each subsequent wrapping exception.
</p>
</td>
</tr>
<tr>
<td align="center">
<b>sn</b><br />
<b>sequenceNumber</b>
</td>
<td>Includes a sequence number that will be incremented in every event. The counter is a
static variable so will only be unique within applications that share the same converter Class
object.</td>
</tr>
<tr>
<td align="center">
<b>t</b><br />
<b>thread</b>
</td>
<td>Used to output the name of the thread that generated the logging event.</td>
</tr>
<tr>
<td align="center">
<b>x</b><br />
<b>NDC</b>
</td>
<td>Used to output the Thread Context Stack (also known as the Nested Diagnostic Context or NDC)
associated with the thread that generated the logging event.
</td>
</tr>
<tr>
<td align="center">
<b>X</b>{key}<br />
<b>mdc</b>{key}<br />
<b>MDC</b>{key}
</td>
<td>
<p>Used to output the Thread Context Map (also known as the Mapped Diagnostic Context or MDC)
associated with the thread that generated the logging event. The
<b>X</b>
conversion character can be followed by the key for the
map placed between braces, as in
<b>%X{clientNumber}</b>
where
<code>clientNumber</code>
is the key. The value in the MDC
corresponding to the key will be output. If no additional sub-option
is specified, then the entire contents of the MDC key value pair set
is output using a format {{key1,val1},{key2,val2}}
</p>
<p>See the
<a href="../../log4j2-api/apidocs/org/apache/logging/log4j/ThreadContext.html">ThreadContext</a>
class for more details.
</p>
</td>
</tr>
<tr>
<td align="center">
<b>u</b>{"RANDOM" | "TIME"}<br />
<b>uuid</b>
</td>
<td>Includes either a random or a time-based UUID. The time-based UUID is a Type 1 UUID that can
generate up to 10,000 unique ids per millisecond, will use the MAC address of each host, and to
try to insure uniqueness across multiple JVMs and/or ClassLoaders on the same host a
random number between 0 and 16,384 will be associated with each instance of the UUID generator
Class and included in each time-based UUID generated. Because time-based UUIDs contain
the MAC address and timestamp they should be used with care as they can cause a security
vulnerability.
</td>
</tr>
<tr>
<td align="center">
<b>xEx</b>{depth}<br />
<b>xException</b>{depth}<br />
<b>xThrowable</b>{depth}
</td>
<td>
<p>
The same as the %throwable conversion word but also includes class packaging information.
</p>
<p>
At the end of each stack element of the exception, a string containing the name of the jar file
that contains the class or the directory the class is located in and the "Implementation-Version"
as found in that jar's manifest will be added. If the information is uncertain, then the class
packaging data will be preceded by a tilde, i.e. the '~' character.
</p>
</td>
</tr>
<tr>
<td align="center">
<b>%</b>
</td>
<td>The sequence %% outputs a single percent sign.
</td>
</tr>
</table>
<p>By default the relevant information is output as is. However,
with the aid of format modifiers it is possible to change the
minimum field width, the maximum field width and justification.
</p>
<p>The optional format modifier is placed between the percent sign
and the conversion character.
</p>
<p>The first optional format modifier is the
<em>left justification
flag
</em>
which is just the minus (-) character. Then comes the
optional
<em>minimum field width</em>
modifier. This is a decimal
constant that represents the minimum number of characters to
output. If the data item requires fewer characters, it is padded on
either the left or the right until the minimum width is
reached. The default is to pad on the left (right justify) but you
can specify right padding with the left justification flag. The
padding character is space. If the data item is larger than the
minimum field width, the field is expanded to accommodate the
data. The value is never truncated.
</p>
<p>This behavior can be changed using the
<em>maximum field
width
</em>
modifier which is designated by a period followed by a
decimal constant. If the data item is longer than the maximum
field, then the extra characters are removed from the
<em>beginning</em>
of the data item and not from the end. For
example, it the maximum field width is eight and the data item is
ten characters long, then the first two characters of the data item
are dropped. This behavior deviates from the printf function in C
where truncation is done from the end.
</p>
<p>Below are various format modifier examples for the category
conversion specifier.
</p>
<table BORDER="1" CELLPADDING="8">
<tr>
<th>Format modifier</th>
<th>left justify</th>
<th>minimum width</th>
<th>maximum width</th>
<th>comment</th>
</tr>
<tr>
<td align="center">%20c</td>
<td align="center">false</td>
<td align="center">20</td>
<td align="center">none</td>
<td>Left pad with spaces if the category name is less than 20
characters long.
</td>
</tr>
<tr>
<td align="center">%-20c</td>
<td align="center">true</td>
<td
align="center">20
</td>
<td align="center">none</td>
<td>Right pad with
spaces if the category name is less than 20 characters long.
</td>
</tr>
<tr>
<td align="center">%.30c</td>
<td align="center">NA</td>
<td align="center">none</td>
<td align="center">30</td>
<td>Truncate from the beginning if the category name is longer than 30
characters.
</td>
</tr>
<tr>
<td align="center">%20.30c</td>
<td align="center">false</td>
<td align="center">20</td>
<td align="center">30</td>
<td>Left pad with spaces if the category name is shorter than 20
characters. However, if category name is longer than 30 characters,
then truncate from the beginning.
</td>
</tr>
<tr>
<td align="center">%-20.30c</td>
<td align="center">true</td>
<td align="center">20</td>
<td align="center">30</td>
<td>Right pad with spaces if the category name is shorter than 20
characters. However, if category name is longer than 30 characters,
then truncate from the beginning.
</td>
</tr>
<caption align="top">Pattern Converters</caption>
</table>
</subsection>
<a name="RFC5424Layout"/>
<subsection name="RFC5424Layout">
<p>
As the name implies, the RFC5424Layout formats LogEvents in accordance with
<a href="http://tools.ietf.org/html/rfc5424">RFC 5424</a>, the enhanced Syslog specification. Although the specification
is primarily directed at sending messages via Syslog, this format is quite useful for
other purposes since items are passed in the message as self-describing key/value pairs.
</p>
<table border="1" width="100%">
<tr>
<th>Parameter Name</th>
<th>Type</th>
<th>Description</th>
</tr>
<tr>
<td>appName</td>
<td>String</td>
<td>The value to use as the APP-NAME in the RFC 5424 syslog record.</td>
</tr>
<tr>
<td>charset</td>
<td>String</td>
<td>The character set to use when converting the syslog String to a byte array. The String must be
a valid <a href="http://download.oracle.com/javase/6/docs/api/java/nio/charset/Charset.html">Charset</a>.
If not specified, the default system Charset will be used.</td>
</tr>
<tr>
<td>enterpriseNumber</td>
<td>integer</td>
<td>The IANA enterprise number as described in
<a href="http://tools.ietf.org/html/rfc5424#section-7.2.2">RFC 5424</a></td>
</tr>
<tr>
<td>facility</td>
<td>String</td>
<td>The facility is used to try to classify the message. The facility option must be set to one of
"KERN", "USER", "MAIL", "DAEMON", "AUTH", "SYSLOG", "LPR", "NEWS", "UUCP", "CRON", "AUTHPRIV",
"FTP", "NTP", "AUDIT", "ALERT", "CLOCK", "LOCAL0", "LOCAL1", "LOCAL2", "LOCAL3", "LOCAL4", "LOCAL5",
"LOCAL6", or "LOCAL7". These values may be specified as upper or lower case characters.</td>
</tr>
<tr>
<td>format</td>
<td>String</td>
<td>If set to "RFC5424" the data will be formatted in accordance with RFC 5424. Otherwise, it will
be formatted as a BSD Syslog record. Note that although BSD Syslog records are required to be
1024 bytes or shorter the SyslogLayout does not truncate them. The RFC5424Layout also does not
truncate records since the receiver must accept records of up to 2048 bytes and may accept records
that are longer.</td>
</tr>
<tr>
<td>id</td>
<td>String</td>
<td>The default structured data id to use when formatting according to RFC 5424. If the LogEvent contains
a StructuredDataMessage the id from the Message will be used instead of this value.</td>
</tr>
<tr>
<td>immediateFlush</td>
<td>boolean</td>
<td>When set to true, each write will be followed by a flush. This will guarantee the data is written
to disk but could impact performance.</td>
</tr>
<tr>
<td>includeMDC</td>
<td>boolean</td>
<td>Indicates whether data from the ThreadContextMap will be included in the RFC 5424 Syslog record.
Defaults to true.</td>
</tr>
<tr>
<td>mdcExcludes</td>
<td>String</td>
<td>A comma separated list of mdc keys that should be excluded from the LogEvent. This is mutually
exclusive with the mdcIncludes attribute. This attribute only applies to RFC 5424 syslog records.</td>
</tr>
<tr>
<td>mdcIncludes</td>
<td>String</td>
<td>A comma separated list of mdc keys that should be included in the FlumeEvent. Any keys in the MDC
not found in the list will be excluded. This option is mutually exclusive with the mdcExcludes
attribute. This attribute only applies to RFC 5424 syslog records.</td>
</tr>
<tr>
<td>mdcRequired</td>
<td>String</td>
<td>A comma separated list of mdc keys that must be present in the MDC. If a key is not present a
LoggingException will be thrown. This attribute only applies to RFC 5424 syslog records.</td>
</tr>
<tr>
<td>mdcPrefix</td>
<td>String</td>
<td>A string that should be prepended to each MDC key in order to distinguish it from event attributes.
The default string is "mdc:". This attribute only applies to RFC 5424 syslog records.</td>
</tr>
<tr>
<td>messageId</td>
<td>String</td>
<td>The default value to be used in the MSGID field of RFC 5424 syslog records. </td>
</tr>
<tr>
<td>newLine</td>
<td>boolean</td>
<td>If true, a newline will be appended to the end of the syslog record. The default is false.</td>
</tr>
<caption align="top">RFC5424Layout Parameters</caption>
</table>
</subsection>
<a name="SerializedLayout"/>
<subsection name="SerializedLayout">
<p>
The SerializedLayout simply serializes the LogEvent into a byte array. This is useful when
sending messages via JMS or via a Socket connection. The SerializedLayout accepts no parameters.
</p>
</subsection>
<a name="SyslogLayout"/>
<subsection name="SyslogLayout">
<p>
The SyslogLayout formats the LogEvent as BSD Syslog records matching the same format used by
Log4j 1.2.
</p>
<table border="1" width="100%">
<tr>
<th>Parameter Name</th>
<th>Type</th>
<th>Description</th>
</tr>
<tr>
<td>charset</td>
<td>String</td>
<td>The character set to use when converting the syslog String to a byte array. The String must be
a valid <a href="http://download.oracle.com/javase/6/docs/api/java/nio/charset/Charset.html">Charset</a>.
If not specified, the default system Charset will be used.</td>
</tr>
<tr>
<td>facility</td>
<td>String</td>
<td>The facility is used to try to classify the message. The facility option must be set to one of
"KERN", "USER", "MAIL", "DAEMON", "AUTH", "SYSLOG", "LPR", "NEWS", "UUCP", "CRON", "AUTHPRIV",
"FTP", "NTP", "AUDIT", "ALERT", "CLOCK", "LOCAL0", "LOCAL1", "LOCAL2", "LOCAL3", "LOCAL4", "LOCAL5",
"LOCAL6", or "LOCAL7". These values may be specified as upper or lower case characters.</td>
</tr>
<tr>
<td>newLine</td>
<td>boolean</td>
<td>If true, a newline will be appended to the end of the syslog record. The default is false.</td>
</tr>
<caption align="top">SyslogLayout Parameters</caption>
</table>
</subsection>
<a name="XMLLayout"/>
<subsection name="XMLLayout">
<p>
The output of the XMLLayout consists of a series of log4j:event
elements as defined in the<a href="log4j.dtd">log4j.dtd</a>. If configured to do so it will
output a complete well-formed XML file. The output is designed to be
included as an
<em>external entity</em>
in a separate file to form
a correct XML file.
</p>
<p>For example, if <code>abc</code> is the name of the file where
the XMLLayout ouput goes, then a well-formed XML file would be:
</p>
<pre>
&lt;?xml version="1.0" ?&gt;
*
&lt;!DOCTYPE log4j:eventSet SYSTEM "log4j.dtd" [&lt;!ENTITY data SYSTEM "abc"&gt;]&gt;
*
&lt;log4j:eventSet version="2.0" xmlns:log4j="http://logging.apache.org/log4j/"&gt;
&nbsp;&nbsp;&data;
&lt;/log4j:eventSet&gt;
</pre>
<p>
This approach enforces the independence of the XMLLayout and the appender where it is embedded.
</p>
<p>
The <code>version</code> attribute helps components to correctly intrepret output generated by XMLLayout.
The value of this attribute should be "2.0".
</p>
<p>
Appenders using this layout should have their encoding set to UTF-8 or UTF-16, otherwise events containing
non ASCII characters could result in corrupted log files.
</p>
</subsection>
</section>
</body>
</document>