The Toothpick Logo

PatternLoggingFormatter

PatternLoggingFormatter provides a printf()-like was of formatting the output of a LoggingEvent. This is useful for e.g. outputting events to a comma-delimited file. The goal of this class is to format a LoggingEvent and return the results as a String. The results depend on the conversion pattern.

[top]  [home]

Conversion Patterns

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 conversion specifiers.

You are free to insert any literal text within the conversion pattern.

Each conversion specifier starts with a percent sign (%) and is followed by optional format modifiers and a conversion character. 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.

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 %-5p means the priority of the logging event should be left justified to a width of five characters. The following conversion specifiers are currently supported:

%cUsed to output the category of the logging event.
%d Used to output the timestamp of the logging event. Uses the ISO8601TimestampFormatter.
%lUsed to output the name of the Logger associated with the event.
%mUsed to output the message associated with the logging event.
%nOutputs the platform dependent line separator character or characters.
%pUsed to output the priority of the logging event.
%tOutputs a tab character.
%xUsed to output the context of the logging event.
%%The sequence %% outputs a single percent sign.

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.

[top]  [home]

Format Modifiers

The optional format modifier is placed between the percent sign and the conversion character.

The first optional format modifier is the left justification flag which is just the minus (-) character. Then comes the optional minimum field width 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.

This behavior can be changed using the maximum field width 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 beginning 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.

Below are various format modifier examples for the category conversion specifier.

Format modifierleft justifymin. widthmax. widthcomment
%20cfalse20noneLeft pad with spaces if the category name is less than 20 Characters long.
%-20ctrue20noneRight pad with spaces if the category name is less than 20 characters long.
%.30cNAnone30Truncate from the beginning if the category name is longer than 30 characters.
%20.30cfalse2030Left 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.
%-20.30ctrue2030Right 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.



Published Thursday, October 19, 2006
supported dialects