📄 patternlayout.java
字号:
/*
* Copyright (C) The Apache Software Foundation. All rights reserved.
*
* This software is published under the terms of the Apache Software License
* version 1.1, a copy of which has been included with this distribution in
* the LICENSE file.
*/
package org.apache.log4j;
import org.apache.log4j.Layout;
import org.apache.log4j.spi.LoggingEvent;
import org.apache.log4j.helpers.PatternParser;
import org.apache.log4j.helpers.PatternConverter;
// Contributors: Nelson Minar <nelson@monkey.org>
// Anders Kristensen <akristensen@dynamicsoft.com>
/**
A flexible layout configurable with pattern string.
<p>The goal of this class is to {@link #format format} a {@link
LoggingEvent} and return the results as a String. The results
depend on the <em>conversion pattern</em>.
<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><i>You are free to insert any literal text within the conversion
pattern.</i>
<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>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>
Category root = Category.getRoot();
root.debug("Message 1");
root.warn("Message 2");
</pre>
would yield the output
<pre>
DEBUG [main]: Message 1
WARN [main]: Message 2
</pre>
<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.
The recognized conversion characters are
<p>
<table border="1" CELLPADDING="8">
<th>Conversion Character</th>
<th>Effect</th>
<tr>
<td align=center><b>c</b></td>
<td>Used to output the category of the logging event. The
category conversion specifier can be optionally followed by
<em>precision specifier</em>, that is a decimal constant in
brackets.
<p>If a precision specifier is given, then only the corresponding
number of right most components of the category name will be
printed. By default the category name is printed in full.
<p>For example, for the category name "a.b.c" the pattern
<b>%c{2}</b> will output "b.c".
</td>
</tr>
<tr>
<td align=center><b>C</b></td>
<td>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
is a decimal constant in brackets.
<p>If a precision specifier is given, then only the corresponding
number of right most components of the class name will be
printed. By default the class name is output in fully qualified form.
<p>For example, for the class name "org.apache.xyz.SomeClass", the
pattern <b>%C{1}</b> will output "SomeClass".
<p><b>WARNING</b> Generating the caller class information is
slow. Thus, it's use should be avoided unless execution speed is
not an issue.
</td>
</tr>
<tr> <td align=center><b>d</b></td> <td>Used to output the date of
the logging event. The date conversion specifier may be
followed by a <em>date format specifier</em> enclosed between
braces. For example, <b>%d{HH:mm:ss,SSS}</b> or
<b>%d{dd MMM yyyy HH:mm:ss,SSS}</b>. If no
date format specifier is given then ISO8601 format is
assumed.
<p>The date format specifier admits the same syntax as the
time pattern string of the {@link
java.text.SimpleDateFormat}. Although part of the standard
JDK, the performance of <code>SimpleDateFormat</code> is
quite poor.
<p>For better results it is recommended to use the log4j date
formatters. These can be specified using one of the strings
"ABSOLUTE", "DATE" and "ISO8601" for specifying {@link
org.apache.log4j.helpers.AbsoluteTimeDateFormat
AbsoluteTimeDateFormat}, {@link
org.apache.log4j.helpers.DateTimeDateFormat DateTimeDateFormat}
and respectively {@link
org.apache.log4j.helpers.ISO8601DateFormat
ISO8601DateFormat}. For example, <b>%d{ISO8601}</b> or
<b>%d{ABSOLUTE}</b>.
<p>These dedicated date formatters perform significantly
better than {@link java.text.SimpleDateFormat}.
</td>
</tr>
<tr>
<td align=center><b>F</b></td>
<td>Used to output the file name where the logging request was
issued.
<p><b>WARNING</b> Generating caller location information is
extremely slow. It's use should be avoided unless execution speed
is not an issue.
</tr>
<tr>
<td align=center><b>l</b></td>
<td>Used to output location information of the caller which generated
the logging event.
<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>The location information can be very useful. However, it's
generation is <em>extremely</em> slow. It's use should be avoided
unless execution speed is not an issue.
</td>
</tr>
<tr>
<td align=center><b>L</b></td>
<td>Used to output the line number from where the logging request
was issued.
<p><b>WARNING</b> Generating caller location information is
extremely slow. It's use should be avoided unless execution speed
is not an issue.
</tr>
<tr>
<td align=center><b>m</b></td>
<td>Used to output the application supplied message associated with
the logging event.</td>
</tr>
<tr>
<td align=center><b>M</b></td>
<td>Used to output the method name where the logging request was
issued.
<p><b>WARNING</b> Generating caller location information is
extremely slow. It's use should be avoided unless execution speed
is not an issue.
</tr>
<tr>
<td align=center><b>n</b></td>
<td>Outputs the platform dependent line separator character or
characters.
<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.
</tr>
<tr>
<td align=center><b>p</b></td>
<td>Used to output the priority of the logging event.</td>
</tr>
<tr>
<td align=center><b>r</b></td>
<td>Used to output the number of milliseconds elapsed since the start
of the application until the creation of the logging event.</td>
</tr>
<tr>
<td align=center><b>t</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></td>
<td>Used to output the NDC (nested diagnostic context) associated
with the thread that generated the logging event.
</td>
</tr>
<tr>
<td align=center><b>X</b></td>
<td>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -