public final class PatternFormatter extends Formatter
LogRecords using printf-like
format specifiers.
The pattern string contains either plain characters and format
specifiers. Plain characters are copied to the log message
verbatim. The format string uses format specifiers
similar to the C standard library printf
subroutine.
Format specifiers are of the form "%<letter>". The table below lists the supported specifier letters:
| Specifier Letter | Takes Modifiers? | Description |
|---|---|---|
| C | Yes. | Place the log record's source class name in the location. |
| d | Yes |
Place the current date and time in the location using
the default Java format
java.text.DateFormat.DEFAULT.
The default may be overridden using the extended %d
specifier |
| e | Yes. |
Place the LogRecord's throwable message in the location.
If the record's throwable is null, then
nothing is output.
|
| E | No. |
Place the LogRecord's throwable stack trace in the
location but on the next line. If the record's throwable
is null, then nothing is output.
Note: the stack trace is written on a new line. It is not necessary to have "%n%E" in your pattern. |
| l | Yes. | Place the log record's log level in the location. |
| m | Yes. | Place the log record's message in the location. |
| M | Yes. | Place the log record's source method name in the location. |
| n | No. | Places the platform-dependent end-of-line marker at the location. |
| N | Yes. | Place the log record's source Logger name in the location. |
| s | Yes. | Place the log record's sequence number in the location. |
| t | Yes. | Place the log record's thread identifier in the location. |
| % | No. | Place the '%' character in the message at this location. |
These specifiers may be further modified with a justification marker, minimum width and maximum width. These modifiers are placed between the '%' and the specifier letter. The format specifier syntax is:
%[-][min width][.max width]<letter>.
where a "-" denotes left justification. If "-" is used, then
"min width" must also be used.
The date specifier also allows for the date format suffix:
%<modifiers>d{<date format>}
The table below describes how these modifiers can be used in different combinations for different affects:
| Format Modifier | Justification | Minimum Width | Maximum Width | Description |
|---|---|---|---|---|
| %m | None | None | None | Output the localized log message in its entirity. |
| %20m | Right | 20 | None | Generates the localized log message. If the result is less than the minimum width, prepends spaces until the minimum width is reached. |
| %-20m | Left | 20 | None | Generates the localized log message. If the result is less than the minimum width, appends spaces until the minimum width is reached. |
| %.40m | None | None | 40 | Generates the localized log message. If the result is greater than the maximum width, truncates the characters beyond the maximum width. |
| %20.40m | Right | 20 | 40 | Generates the localized log message. If the result is less than the minimum width, then prepends spaces until the minimum width is reached. If the result is greater than the maximum width, truncates the characters beyond the maximum width. |
| %-20.40m | Left | 20 | 40 | Generates the localized log message. If the result is less than the minimum width, then appends spaces until the minimum width is reached. If the result is greater than the maximum width, truncates the characters beyond the maximum width. |
Configuration: PatternFormatter
default configuration uses the following LogManager
property. If the named property is either not defined or is
invalid, then the default setting is used.
The idea for this class came from the Apache Jakarta's
Log4j project class
org.apache.log4j.PatternLayout but is in no
way based on it.
| Modifier and Type | Field and Description |
|---|---|
static String |
DEFAULT_PATTERN
The default pattern is "%d{MM/dd/yyyy HH:mm:ss} %m%E".
|
| Constructor and Description |
|---|
PatternFormatter()
Creates a new
PatternFormatter and configures
it according to LogManager configuration
properties. |
PatternFormatter(String pattern)
Constructs a
PatternFormatter instance for
the given pattern. |
| Modifier and Type | Method and Description |
|---|---|
String |
format(LogRecord record)
Returns a localized
String resulting from
formatting the LogRecord. |
formatMessage, getHead, getTailpublic static final String DEFAULT_PATTERN
Example log record:
07/04/1776 10:04:32 John Hancock signed on.
java.lang.IllegalStateException: cannot declare independence
at net.sf.eBus.util.logging.PatternFormatterTest.setUpClass(PatternFormatterTest.java:66)
public PatternFormatter()
PatternFormatter and configures
it according to LogManager configuration
properties.public PatternFormatter(String pattern) throws IllegalArgumentException
PatternFormatter instance for
the given pattern.pattern - the log record pattern string.IllegalArgumentException - if pattern is not a valid pattern string.Copyright © 2019. All rights reserved.