Class PatternFormatter


  • public final class PatternFormatter
    extends java.util.logging.Formatter
    Formats 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:

    Regular Expression 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 %d{<date pattern>}. <date pattern> will be passed to SimpleDateFormat(String) constructor (which see). If SimpleDateFormat rejects the date pattern by throwing an exception, the exception will pass through to your application.

    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:

    Pattern Modifiers
    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.

    • net.sf.eBus.util.logging.PatternFormatter.pattern (defaults to "%d{MM/dd/yyyy HH:mm:ss} %m%E")

    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.

    Version:
    $Id: PatternFormatter.java,v 1.4 2005/07/22 01:52:33 charlesr Exp $
    Author:
    Charles Rapp
    • Field Summary

      Fields 
      Modifier and Type Field Description
      static java.lang.String DEFAULT_PATTERN
      The default pattern is "%d{MM/dd/yyyy HH:mm:ss} %m%E".
    • Constructor Summary

      Constructors 
      Constructor Description
      PatternFormatter()
      Creates a new PatternFormatter and configures it according to LogManager configuration properties.
      PatternFormatter​(java.lang.String pattern)
      Constructs a PatternFormatter instance for the given pattern.
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      java.lang.String format​(java.util.logging.LogRecord logRecord)
      Returns a localized String resulting from formatting the LogRecord.
      • Methods inherited from class java.util.logging.Formatter

        formatMessage, getHead, getTail
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Field Detail

      • DEFAULT_PATTERN

        public static final java.lang.String DEFAULT_PATTERN
        The default pattern is "%d{MM/dd/yyyy HH:mm:ss} %m%E".

        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)
         
        See Also:
        Constant Field Values
    • Constructor Detail

      • PatternFormatter

        public PatternFormatter()
        Creates a new PatternFormatter and configures it according to LogManager configuration properties.
      • PatternFormatter

        public PatternFormatter​(java.lang.String pattern)
        Constructs a PatternFormatter instance for the given pattern.
        Parameters:
        pattern - the log record pattern string.
        Throws:
        java.lang.IllegalArgumentException - if pattern is not a valid pattern string.
    • Method Detail

      • format

        public java.lang.String format​(java.util.logging.LogRecord logRecord)
        Returns a localized String resulting from formatting the LogRecord.
        Specified by:
        format in class java.util.logging.Formatter
        Parameters:
        logRecord - Output this record to the log.
        Returns:
        a localized String resulting from formatting the LogRecord.