momelog.formatter
Class PatternFormatter

java.lang.Object
  extended by momelog.Formatter
      extended by momelog.formatter.PatternFormatter
All Implemented Interfaces:
Configurable

public class PatternFormatter
extends Formatter
implements Configurable

Formatter intended to convert logging events to strings based on conversion pattern. It's functionality and format of conversion pattern resemble very much that of the log4J framework.

Conversion pattern is a string that specifies the way, logging events are converted. It's format resembles very much format of conversion pattern in log4J framework.

Conversion pattern consists of literal text and conversion specifiers. Literal text of pattern is inserted in result as is. Users are free to specify any literal text within the conversion pattern. Conversion specifiers designate where and how properties of logging event should be inserted.

Each conversion specifier starts with a percent sign (%) followed by the optional format modifiers and conversion character. Conversion character specifies property of logging event to be inserted (e.g. category, message, timestamp etc.). The format modifiers define minimum and/or maximum field's width and left or right justification.

PatternFormatter recognizes following conversion characters.

Conversion character

Description

c

Designates the place where category of logger generated this logging event should be inserted.

Note: Unlike in log4J framework, category conversion specifier can not be followed by the optional precision specifier,

C

Designates the place where last part of category of logger generated this logging event should be inserted. Category is separated on parts by dots (.). For example the last part of category foo.bar.SomeCategory is SomeCategory.

Note: Unlike in log4J framework, the last part category conversion specifier can not be followed by optional precision specifier,

m

Designates the place where message of logging event should be inserted. If message is null an empty string is inserted.

e

Designates the place where error of logging event should be inserted. Error is converted to the string by Throwable.toString() method. If error is null an empty string is inserted.

t

Designates the place where timestamp of logging event should be inserted. Timestamp is a long integer specifying number of milliseconds from some base time point till logging event generation. By default, base time point is the time of Logger class loading. It can be set to now by Logger.resetBaseTime() method.

h

Designates the place where the name of thread, in which logging event was issued, should be inserted.

%%

Sequence %% outputs a single percent sign (%).

By default the relevant information is output as is. However, by use of an optional format modifiers it is possible to specify minimum, maximum field's width and justification. Format modifiers are placed between the percent sign and the conversion character.

Format modifiers string is of the format [-][min][.max]

The first optional minus (-) character designates left justification. If it is specified, field is left justified, right justified otherwise (by default).

Optional min modifier is the decimal constant, that specifies minimum width of the field. If field's width is less than minimum, field is padded by spaces from left in a case of right justification or from right in a case of left justification.

Optional max modifier is the decimal constant, that specifies maximum width of the field. It is designated by preceding dot (.). If field's width is greater than maximum, then the extra characters (as in log4J framework) are deleted from the beginning of the field not from the end.

For example. The conversion pattern is set to "Logging of '%7.7c' in thread %h is %m %e %%at %10t". Then the following code snippet

  public class SomeClass
  {
    ...
          
    private static final Logger log = Logger.getLogger(SomeClass.class);
          
    ...
          
    public void someMethod()
    {
      ...
          
      log.log( "Connection with " + url + " established");
          
      ...
          
    }
          
    ...
  }
 

produces the logging event that is converted to

  Logging of 'meClass' in thread Thread-7 is Connection with http://somedomain.org/somepage/index.html established  %at      17785
 

PatternFormatter can be instantiated by using one of the following constructors PatternFormatter(String) or PatternFormatter(). In the first case it is initialized with specified conversion pattern and in second - with default one. MoMELog logging framework is preset with PatternFormatter initialized with default pattern ("%C> %m %e (%t)"). That's why, if another formatter is not specified declaratively or programmatically, PatternFormatter instance can be obtained by calling Logger.getFormatter() static method.

Conversion pattern can be set by using setPattern(String) setter method. After this method returns, all new logging events are formatted using new conversion pattern.

PatternFormatter implements Configurable interface. It can be configured programmatically by invoking respective setter method or declaratively from configuration file. See PatternFormatter Guide for more details.

Version:
1.0
Author:
Sergio Morozov

Field Summary
static String DEFAULT_PATTERN
          Default conversion pattern.
 
Constructor Summary
PatternFormatter()
          Instantiates PatternFormatter initialized with DEFAULT_PATTERN ("%C> %m %e (%t)").
PatternFormatter(String pattern)
          Instantiates PatternFormatter initialized with the specified conversion pattern.
 
Method Summary
 void configure(char[] lines, int offset, int length)
          Configures PatternFormatter instance from given character sequence.
 void format(LogEvent event, StringBuffer buffer)
          Converts specified logging event to the string based on conversion pattern and appends it to the given StringBuffer.
 void setPattern(String pattern)
          Sets conversion pattern to the specified value.
 
Methods inherited from class momelog.Formatter
format, formatAsObject
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

DEFAULT_PATTERN

public static final String DEFAULT_PATTERN
Default conversion pattern. The value is "%C> %m %e (%t)".

See Also:
Constant Field Values
Constructor Detail

PatternFormatter

public PatternFormatter(String pattern)
Instantiates PatternFormatter initialized with the specified conversion pattern.

Parameters:
pattern - conversion pattern.
Throws:
IllegalArgumentException - if specified pattern is of invalid format.

PatternFormatter

public PatternFormatter()
Instantiates PatternFormatter initialized with DEFAULT_PATTERN ("%C> %m %e (%t)").

Method Detail

format

public void format(LogEvent event,
                   StringBuffer buffer)
Converts specified logging event to the string based on conversion pattern and appends it to the given StringBuffer.

Specified by:
format in class Formatter
Parameters:
event - LogEvent instance to convert.
buffer - StringBuffer where to append formatted string.
See Also:
Formatter.format(momelog.LogEvent, java.lang.StringBuffer)

setPattern

public void setPattern(String pattern)
Sets conversion pattern to the specified value. After this method returns, all new logging events are formatted using given conversion pattern.

Parameters:
pattern - conversion pattern to set.
Throws:
IllegalArgumentException - if given conversion pattern is of invalid format.

configure

public void configure(char[] lines,
                      int offset,
                      int length)
Configures PatternFormatter instance from given character sequence. Character sequence should consist of lines separated by new-line character. Each line should contain name-value pair separated by equal sign (=). Spaces around property name are allowed and ignored. Spaces around property value are allowed and significant.

PatternFormatter supports the only property - pattern. If this property is specified more than one time, the last occurrence prevails.

Property Name

Description

Default Value

"pattern"

Designates conversion pattern to be used. Spaces around property value are significant (property value is not trimmed.).

Since version 1.0

"%C> %m %e (%t)"

For example following configuration file snippet

  #marker <EOL> is just the end of line. It is shown to emphasize trailing spaces.
  formatter.pattern =  %.5C:%m %e (%7t) [%h] <EOL>
 

sets pattern to " %.5C:%m %e (%7t) [%h] " (spaces around property value are significant).

Specified by:
configure in interface Configurable
Parameters:
lines - char array containing character sequence.
offset - position of the first character of sequence in given array.
length - length of the sequence in characters.