Log4j Version 1.2.14: Class PatternLayout

Log4j 1.2

Log4j 1.2.14

org.apache.log4j Class PatternLayout

java.lang.Object
  |
  +--org.apache.log4j.Layout
        |
        +--org.apache.log4j.PatternLayout
All Implemented Interfaces:
OptionHandler

public class PatternLayout
extends Layout

A flexible layout configurable with pattern string.

The goal of this class is to format a LoggingEvent and return the results as a String. The results depend on the conversion pattern.

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. The following is a simple example.

Let the conversion pattern be "%-5p [%t]: %m%n" and assume that the log4j environment was set to use a PatternLayout. Then the statements

Category root = Category.getRoot();
root.debug("Message 1");
root.warn("Message 2");
would yield the output
DEBUG [main]: Message 1
WARN  [main]: Message 2

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 recognized conversion characters are

Conversion Character Effect
c
C
d
F
l
L
m
M
n
p
r
t
x
X
%

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.

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 modifier left justify minimum width maximum width comment
%20c false 20 none
%-20c true 20 none
%.30c NA none 30
%20.30c false 20 30
%-20.30c true 20 30

Below are some examples of conversion patterns.

%r [%t] %-5p %c %x - %m\n

This is essentially the TTCC layout.

%-6r [%15.15t] %-5p %30.30c %x - %m\n

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.

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.

Since:
0.8.2
Author:
James P. Cakalic, Ceki Gülcü

protected  int
static String
protected  int
static String
 
 
 
 void
protected  PatternParser
 String
 String
 boolean
 void
 
 
 

DEFAULT_CONVERSION_PATTERN

public static final String DEFAULT_CONVERSION_PATTERN
Default pattern string for log output. Currently set to the string "%m%n" which just prints the application supplied message.

TTCC_CONVERSION_PATTERN

public static final String TTCC_CONVERSION_PATTERN
A conversion pattern equivalent to the TTCCCLayout. Current value is %r [%t] %p %c %x - %m%n.

BUF_SIZE

protected final int BUF_SIZE

MAX_CAPACITY

protected final int MAX_CAPACITY

PatternLayout

public PatternLayout()
Constructs a PatternLayout using the DEFAULT_LAYOUT_PATTERN. The default pattern just produces the application supplied message.

PatternLayout

public PatternLayout(String pattern)
Constructs a PatternLayout using the supplied conversion pattern.

setConversionPattern

public void setConversionPattern(String conversionPattern)
Set the ConversionPattern option. This is the string which controls formatting and consists of a mix of literal content and conversion specifiers.

getConversionPattern

public String getConversionPattern()
Returns the value of the ConversionPattern option.

activateOptions

public void activateOptions()
Does not do anything as options become effective

ignoresThrowable

public boolean ignoresThrowable()
The PatternLayout does not handle the throwable contained within LoggingEvents. Thus, it returns true.
Overrides:
ignoresThrowable in class Layout
Since:
0.8.4

createPatternParser

protected PatternParser createPatternParser(String pattern)
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

format

public String format(LoggingEvent event)
Produces a formatted string as specified by the conversion pattern.
Overrides:
format in class Layout

Log4j 1.2.14

Copyright 2000-2005 Apache Software Foundation.