📄 patternlayout.java
字号:
<p>Used to output the MDC (mapped diagnostic context) associated
with the thread that generated the logging event. The <b>X</b>
conversion character <em>must</em> 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.</p>
<p>See {@link MDC} class for more details.
</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>The optional format modifier is placed between the percent sign
and the conversion character.
<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>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>Below are various format modifier examples for the category
conversion specifier.
<p>
<TABLE BORDER=1 CELLPADDING=8>
<th>Format modifier
<th>left justify
<th>minimum width
<th>maximum width
<th>comment
<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.
<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.
<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.
<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.
<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.
</table>
<p>Below are some examples of conversion patterns.
<dl>
<p><dt><b>%r [%t] %-5p %c %x - %m\n</b>
<p><dd>This is essentially the TTCC layout.
<p><dt><b>%-6r [%15.15t] %-5p %30.30c %x - %m\n</b>
<p><dd>Similar to the TTCC layout except that the relative time is
right padded if less than 6 digits, thread name is right padded if
less than 15 characters and truncated if longer and the category
name is left padded if shorter than 30 characters and truncated if
longer.
</dl>
<p>The above text is largely inspired from Peter A. Darnell and
Philip E. Margolis' highly recommended book "C -- a Software
Engineering Approach", ISBN 0-387-97389-3.
@author <a href="mailto:cakalijp@Maritz.com">James P. Cakalic</a>
@author Ceki Gülcü
@since 0.8.2 */
public class PatternLayout extends Layout {
/** Default pattern string for log output. Currently set to the
string <b>"%m%n"</b> which just prints the application supplied
message. */
public final static String DEFAULT_CONVERSION_PATTERN ="%m%n";
/** A conversion pattern equivalent to the TTCCCLayout.
Current value is <b>%r [%t] %p %c %x - %m%n</b>. */
public final static String TTCC_CONVERSION_PATTERN
= "%r [%t] %p %c %x - %m%n";
protected final int BUF_SIZE = 256;
protected final int MAX_CAPACITY = 1024;
// output buffer appended to when format() is invoked
private StringBuffer sbuf = new StringBuffer(BUF_SIZE);
private String pattern;
private PatternConverter head;
private String timezone;
/**
Constructs a PatternLayout using the DEFAULT_LAYOUT_PATTERN.
The default pattern just produces the application supplied message.
*/
public PatternLayout() {
this(DEFAULT_CONVERSION_PATTERN);
}
/**
Constructs a PatternLayout using the supplied conversion pattern.
*/
public PatternLayout(String pattern) {
this.pattern = pattern;
head = createPatternParser((pattern == null) ? DEFAULT_CONVERSION_PATTERN :
pattern).parse();
}
/**
Set the <b>ConversionPattern</b> option. This is the string which
controls formatting and consists of a mix of literal content and
conversion specifiers.
*/
public
void setConversionPattern(String conversionPattern) {
pattern = conversionPattern;
head = createPatternParser(conversionPattern).parse();
}
/**
Returns the value of the <b>ConversionPattern</b> option.
*/
public
String getConversionPattern() {
return pattern;
}
/**
Does not do anything as options become effective
*/
public
void activateOptions() {
// nothing to do.
}
/**
The PatternLayout does not handle the throwable contained within
{@link LoggingEvent LoggingEvents}. Thus, it returns
<code>true</code>.
@since 0.8.4 */
public
boolean ignoresThrowable() {
return true;
}
/**
Returns PatternParser used to parse the conversion string. Subclasses
may override this to return a subclass of PatternParser which recognize
custom conversion characters.
@since 0.9.0
*/
protected PatternParser createPatternParser(String pattern) {
return new PatternParser(pattern);
}
/**
Produces a formatted string as specified by the conversion pattern.
*/
public String format(LoggingEvent event) {
// Reset working stringbuffer
if(sbuf.capacity() > MAX_CAPACITY) {
sbuf = new StringBuffer(BUF_SIZE);
} else {
sbuf.setLength(0);
}
PatternConverter c = head;
while(c != null) {
c.format(sbuf, event);
c = c.next;
}
return sbuf.toString();
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -