Top Description Inners Fields Constructors Methods
org.apache.log.format

public Class PatternFormatter

extends Object
implements Formatter
Class Inheritance
All Implemented Interfaces
org.apache.log.format.Formatter
Known Direct Subclasses
org.apache.log.format.ExtendedPatternFormatter
Imports
java.io.StringWriter, java.text.SimpleDateFormat, java.util.Date, .Stack, org.apache.log.ContextMap, .LogEvent, .Priority

This formater formats the LogEvents according to a input pattern string.

The format of each pattern element can be %[+|-][#[.#]]{field:subformat}.

A simple example of a typical PatternFormatter format would be:


  %{time} %5.5{priority}[%-10.10{category}]: %{message}

This would produce a line like:


  1000928827905 DEBUG [     junit]: Sample message

The format string specifies that the logger should first print the time value of the log event without size restriction, then the priority of the log event with a minimum and maximum size of 5, then the category of the log event right justified with a minimum and maximum size of 10, followed by the message of the log event without any size restriction.

Authors
Avalon Development Team, Peter Donald, Sylvain Wallez, Leif Mortenson
Version
$Id: PatternFormatter.java 506267 2007-02-12 04:06:06Z crossley $

Nested and Inner Type Summary

Modifier and TypeClass and Description
protected static class

Field Summary

Modifier and TypeField and Description
private static final String
private final Date
private PatternFormatter.PatternRun[]
private SimpleDateFormat
protected static final int
MAX_TYPE

The maximum value used for TYPEs.

private static final String
private static final String
private static final String
private static final String
private static final String
private static final int
private static final String
private static final int
private static final String
private static final int
private static final String
private static final int
private static final String
private static final int
private static final String
private static final int
private static final int
private static final String
private static final int
private static final String
private static final int
private static final String

Constructor Summary

AccessConstructor and Description
public
PatternFormatter(final String
the patter
pattern
)

Creation of a new patter formatter baseed on a supplied pattern.

Method Summary

Modifier and TypeMethod and Description
private int

Returns:

the number of characters in pattern run
addPatternRun
(final Stack<E>
the stack on which to place patterns
stack
,
final char[]
the input string
pattern
,
int
the start of pattern run
index
)

Extract and build a pattern from input string.

private int

Returns:

the number of characters in run
addTextRun
(final Stack<E>
the stack on which to place runs
stack
,
final char[]
the input string
pattern
,
int
the start of the text run
index
)

Extract and build a text run from input string.

private void
append(final StringBuffer
the StringBuffer
sb
,
final int
the minimum size of output (0 to ignore)
minSize
,
final int
the maximum size of output (0 to ignore)
maxSize
,
final boolean
true if the string is to be right justified in it's box.
rightJustify
,
final String
the input string
output
)

Utility to append a string to buffer given certain constraints.

private void
appendWhiteSpace(final StringBuffer
the StringBuffer
sb
,
int
the number of spaces to append
length
)

Append a certain number of whitespace characters to a StringBuffer.

public String

Returns:

the formatted output
format
(final LogEvent
the event
event
)

Implements org.apache.log.format.Formatter.format.

Format the event according to the pattern.
protected String

Returns:

the formatted result.
formatPatternRun
(final LogEvent event, final PatternFormatter.PatternRun
the pattern run to format.
run
)

Formats a single pattern run (can be extended in subclasses).

protected String

Returns:

the formatted string
getCategory
(final String
the category string
category
,
final String
ancilliary format parameter - allowed to be null
format
)

Utility method to format category.

protected String

Returns:

the formatted string
getContextMap
(final ContextMap
the context map
map
,
final String
ancilliary format parameter - allowed to be null
format
)

Utility method to format context map.

protected String

Returns:

the formatted string
getMessage
(final String
the message string
message
,
final String
ancilliary format parameter - allowed to be null
format
)

Utility method to format message.

protected String
getPriority(final Priority priority, final String format)

Get formatted priority string.

protected String

Returns:

the formatted string
getRTime
(final long
the time
time
,
final String
ancilliary format parameter - allowed to be null
format
)

Utility method to format relative time.

protected String

Returns:

the formatted string
getStackTrace
(final Throwable
the throwable instance
throwable
,
final String
ancilliary format parameter - allowed to be null
format
)

Utility method to format stack trace.

protected String
getThread(final String format)

Get formatted thread string.

protected String

Returns:

the formatted string
getTime
(final long
the time
time
,
final String
ancilliary format parameter - allowed to be null
format
)

Utility method to format time.

protected int

Returns:

the type-id
getTypeIdFor
(final String
the string
type
)

Retrieve the type-id for a particular string.

protected final void
parse(final String
the pattern
patternString
)

Parse the input pattern and build internal data structures.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

EOLback to summary
private static final String EOL
m_dateback to summary
private final Date m_date
m_formatSpecificationback to summary
private PatternFormatter.PatternRun[] m_formatSpecification
m_simpleDateFormatback to summary
private SimpleDateFormat m_simpleDateFormat
MAX_TYPEback to summary
protected static final int MAX_TYPE

The maximum value used for TYPEs. Subclasses can define their own TYPEs starting at MAX_TYPE + 1.

SPACE_1back to summary
private static final String SPACE_1
SPACE_16back to summary
private static final String SPACE_16
SPACE_2back to summary
private static final String SPACE_2
SPACE_4back to summary
private static final String SPACE_4
SPACE_8back to summary
private static final String SPACE_8
TYPE_CATEGORYback to summary
private static final int TYPE_CATEGORY
TYPE_CATEGORY_STRback to summary
private static final String TYPE_CATEGORY_STR
TYPE_CONTEXTback to summary
private static final int TYPE_CONTEXT
TYPE_CONTEXT_STRback to summary
private static final String TYPE_CONTEXT_STR
TYPE_MESSAGEback to summary
private static final int TYPE_MESSAGE
TYPE_MESSAGE_STRback to summary
private static final String TYPE_MESSAGE_STR
TYPE_PRIORITYback to summary
private static final int TYPE_PRIORITY
TYPE_PRIORITY_STRback to summary
private static final String TYPE_PRIORITY_STR
TYPE_RELATIVE_TIMEback to summary
private static final int TYPE_RELATIVE_TIME
TYPE_RELATIVE_TIME_STRback to summary
private static final String TYPE_RELATIVE_TIME_STR
TYPE_TEXTback to summary
private static final int TYPE_TEXT
TYPE_THREADback to summary
private static final int TYPE_THREAD
TYPE_THREAD_STRback to summary
private static final String TYPE_THREAD_STR
TYPE_THROWABLEback to summary
private static final int TYPE_THROWABLE
TYPE_THROWABLE_STRback to summary
private static final String TYPE_THROWABLE_STR
TYPE_TIMEback to summary
private static final int TYPE_TIME
TYPE_TIME_STRback to summary
private static final String TYPE_TIME_STR

Constructor Detail

PatternFormatterback to summary
public PatternFormatter(final String pattern)

Creation of a new patter formatter baseed on a supplied pattern.

Parameters
pattern:String

the patter

Method Detail

addPatternRunback to summary
private int addPatternRun(final Stack<E> stack, final char[] pattern, int index)

Extract and build a pattern from input string.

Parameters
stack:Stack<E>

the stack on which to place patterns

pattern:char[]

the input string

index:int

the start of pattern run

Returns:int

the number of characters in pattern run

addTextRunback to summary
private int addTextRun(final Stack<E> stack, final char[] pattern, int index)

Extract and build a text run from input string. It does special handling of '\n' and '\t' replaceing them with newline and tab.

Parameters
stack:Stack<E>

the stack on which to place runs

pattern:char[]

the input string

index:int

the start of the text run

Returns:int

the number of characters in run

appendback to summary
private void append(final StringBuffer sb, final int minSize, final int maxSize, final boolean rightJustify, final String output)

Utility to append a string to buffer given certain constraints.

Parameters
sb:StringBuffer

the StringBuffer

minSize:int

the minimum size of output (0 to ignore)

maxSize:int

the maximum size of output (0 to ignore)

rightJustify:boolean

true if the string is to be right justified in it's box.

output:String

the input string

appendWhiteSpaceback to summary
private void appendWhiteSpace(final StringBuffer sb, int length)

Append a certain number of whitespace characters to a StringBuffer.

Parameters
sb:StringBuffer

the StringBuffer

length:int

the number of spaces to append

formatback to summary
public String format(final LogEvent event)

Implements org.apache.log.format.Formatter.format.

Format the event according to the pattern.

Parameters
event:LogEvent

the event

Returns:String

the formatted output

formatPatternRunback to summary
protected String formatPatternRun(final LogEvent event, final PatternFormatter.PatternRun run)

Formats a single pattern run (can be extended in subclasses).

Parameters
run:PatternFormatter.PatternRun

the pattern run to format.

Returns:String

the formatted result.

getCategoryback to summary
protected String getCategory(final String category, final String format)

Utility method to format category.

Parameters
category:String

the category string

format:String

ancilliary format parameter - allowed to be null

Returns:String

the formatted string

getContextMapback to summary
protected String getContextMap(final ContextMap map, final String format)

Utility method to format context map.

Parameters
map:ContextMap

the context map

format:String

ancilliary format parameter - allowed to be null

Returns:String

the formatted string

getMessageback to summary
protected String getMessage(final String message, final String format)

Utility method to format message.

Parameters
message:String

the message string

format:String

ancilliary format parameter - allowed to be null

Returns:String

the formatted string

getPriorityback to summary
protected String getPriority(final Priority priority, final String format)

Get formatted priority string.

getRTimeback to summary
protected String getRTime(final long time, final String format)

Utility method to format relative time.

Parameters
time:long

the time

format:String

ancilliary format parameter - allowed to be null

Returns:String

the formatted string

getStackTraceback to summary
protected String getStackTrace(final Throwable throwable, final String format)

Utility method to format stack trace.

Parameters
throwable:Throwable

the throwable instance

format:String

ancilliary format parameter - allowed to be null

Returns:String

the formatted string

getThreadback to summary
protected String getThread(final String format)

Get formatted thread string.

getTimeback to summary
protected String getTime(final long time, final String format)

Utility method to format time.

Parameters
time:long

the time

format:String

ancilliary format parameter - allowed to be null

Returns:String

the formatted string

getTypeIdForback to summary
protected int getTypeIdFor(final String type)

Retrieve the type-id for a particular string.

Parameters
type:String

the string

Returns:int

the type-id

parseback to summary
protected final void parse(final String patternString)

Parse the input pattern and build internal data structures.

Parameters
patternString:String

the pattern

org.apache.log.format back to summary

protected Class PatternFormatter.PatternRun

extends Object
Class Inheritance

Field Summary

Modifier and TypeField and Description
public String
public String
public int
public int
public boolean
public int

Constructor Summary

AccessConstructor and Description
protected

Method Summary

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

m_databack to summary
public String m_data
m_formatback to summary
public String m_format
m_maxSizeback to summary
public int m_maxSize
m_minSizeback to summary
public int m_minSize
m_rightJustifyback to summary
public boolean m_rightJustify
m_typeback to summary
public int m_type

Constructor Detail

PatternRunback to summary
protected PatternRun()