Top Description Fields Constructors Methods
com.sun.mail.util.logging

public Class CollectorFormatter

extends Formatter
Class Inheritance
Static Imports
com.sun.mail.util.logging.LogManagerProperties.fromLogManager

A LogRecord formatter that takes a sequence of LogRecords and combines them into a single summary result. Formating of the head, LogRecord, and tail are delegated to the wrapped formatter.

By default each CollectorFormatter is initialized using the following LogManager configuration properties where <formatter-name> refers to the fully qualified class name or the fully qualified derived class name of the formatter. If properties are not defined, or contain invalid values, then the specified default values are used.

Author
Jason Mehrens
Since
JavaMail 1.5.2

Field Summary

Modifier and TypeField and Description
private final Comparator<? super LogRecord>
comparator

The comparator used to pick the log record to format.

private long
count

The number of log records that have been formatted.

private final String
fmt

The message format string used as the formatted output.

private final Formatter
formatter

The formatter used to format the chosen log record.

private long
generation

The number of log produced containing at least one log record.

private static final long
INIT_TIME

Avoid depending on JMX runtime bean to get the start time.

private LogRecord
last

The last accepted record.

private long
maxMillis

The newest log record time.

private long
minMillis

The eldest log record time or eldest time possible for this instance.

private long
thrown

The number of log records that have been formatted with a thrown object.

Constructor Summary

AccessConstructor and Description
public
CollectorFormatter()

Creates the formatter using the LogManager defaults.

public
CollectorFormatter(String
the message format or null to use the LogManager default.
format
)

Creates the formatter using the given format.

public
CollectorFormatter(String
the format string or null to use the LogManager default.
format
,
Formatter
the formatter used on the collected log record or null to specify no formatter.
f
,
Comparator<? super LogRecord>
the comparator used to determine which log record to format or null to specify no comparator.
c
)

Creates the formatter using the given values.

Method Summary

Modifier and TypeMethod and Description
private synchronized boolean

Returns:

true if the last record was the expected record.
accept
(final LogRecord
the LogRecord that is expected.
e
,
final LogRecord
the LogRecord used to collect statistics.
u
)

Updates the summary statistics only if the expected record matches the last record.

private synchronized boolean

Returns:

true if the update was performed.
acceptAndUpdate
(LogRecord
the expected record.
e
,
LogRecord
the update record.
u
)

Updates the summary statistics and stores given LogRecord if the expected record matches the current record.

protected LogRecord

Returns:

the greater of the given log records.
apply
(final LogRecord
the current record.
t
,
final LogRecord
the record that could replace the current.
u
)

Used to choose the collected LogRecord.

protected String

Returns:

the transformed string.
finish
(String
the string to transform.
s
)

Applied to the head, format, and tail returned by the target formatter.

public String

Returns:

an empty string.
format
(final LogRecord
the record to store.
record
)

Implements abstract java.util.logging.Formatter.format.

Accumulates log records which will be used to produce the final output.
private String

Returns:

the formatted string.
formatRecord
(final Handler
the Handler or null.
h
,
final boolean
true if the summary statistics and LogRecord should be reset back to initial values.
reset
)

Formats the given record with the head and tail.

public String

Returns:

the output string.
getTail
(final Handler
the handler or null.
h
)

Overrides java.util.logging.Formatter.getTail.

Formats the collected LogRecord and summary statistics.
private Comparator<? super LogRecord>

Returns:

the comparator or null.
initComparator
(final String
the class name prefix.
p
)

Gets and creates the comparator from the LogManager or returns the default comparator.

private String

Returns:

the format string.
initFormat
(final String
the class name prefix.
p
)

Gets the message format string from the LogManager or creates the default message format string.

private Formatter

Returns:

the formatter.
initFormatter
(final String
the class name prefix.
p
)

Gets and creates the formatter from the LogManager or creates the default formatter.

private synchronized LogRecord

Returns:

null or the current log record.
peek
()

Peek at the current log record.

private synchronized void
reset(final long
the current min milliseconds.
min
)

Resets all of the collected summary statistics including the LogRecord.

public String

Returns:

the current record formatted or the default toString.
toString
()

Overrides java.lang.Object.toString.

Formats the collected LogRecord and summary statistics.
Inherited from java.util.logging.Formatter:
formatMessagegetHead

Field Detail

comparatorback to summary
private final Comparator<? super LogRecord> comparator

The comparator used to pick the log record to format.

countback to summary
private long count

The number of log records that have been formatted.

fmtback to summary
private final String fmt

The message format string used as the formatted output.

formatterback to summary
private final Formatter formatter

The formatter used to format the chosen log record.

generationback to summary
private long generation

The number of log produced containing at least one log record. Only incremented when this formatter is reset.

INIT_TIMEback to summary
private static final long INIT_TIME

Avoid depending on JMX runtime bean to get the start time.

lastback to summary
private LogRecord last

The last accepted record. Synchronized access is preferred over volatile for this class.

maxMillisback to summary
private long maxMillis

The newest log record time.

minMillisback to summary
private long minMillis

The eldest log record time or eldest time possible for this instance.

thrownback to summary
private long thrown

The number of log records that have been formatted with a thrown object.

Constructor Detail

CollectorFormatterback to summary
public CollectorFormatter()

Creates the formatter using the LogManager defaults.

Exceptions
SecurityException:
if a security manager exists and the caller does not have LoggingPermission("control").
UndeclaredThrowableException:
if there are problems loading from the LogManager.
CollectorFormatterback to summary
public CollectorFormatter(String format)

Creates the formatter using the given format.

Parameters
format:String

the message format or null to use the LogManager default.

Exceptions
SecurityException:
if a security manager exists and the caller does not have LoggingPermission("control").
UndeclaredThrowableException:
if there are problems loading from the LogManager.
CollectorFormatterback to summary
public CollectorFormatter(String format, Formatter f, Comparator<? super LogRecord> c)

Creates the formatter using the given values.

Parameters
format:String

the format string or null to use the LogManager default.

f:Formatter

the formatter used on the collected log record or null to specify no formatter.

c:Comparator<? super LogRecord>

the comparator used to determine which log record to format or null to specify no comparator.

Exceptions
SecurityException:
if a security manager exists and the caller does not have LoggingPermission("control").
UndeclaredThrowableException:
if there are problems loading from the LogManager.

Method Detail

acceptback to summary
private synchronized boolean accept(final LogRecord e, final LogRecord u)

Updates the summary statistics only if the expected record matches the last record. The update record is not stored.

Parameters
e:LogRecord

the LogRecord that is expected.

u:LogRecord

the LogRecord used to collect statistics.

Returns:boolean

true if the last record was the expected record.

Exceptions
NullPointerException:
if the update record is null.
acceptAndUpdateback to summary
private synchronized boolean acceptAndUpdate(LogRecord e, LogRecord u)

Updates the summary statistics and stores given LogRecord if the expected record matches the current record.

Parameters
e:LogRecord

the expected record.

u:LogRecord

the update record.

Returns:boolean

true if the update was performed.

Exceptions
NullPointerException:
if the update record is null.
applyback to summary
protected LogRecord apply(final LogRecord t, final LogRecord u)

Used to choose the collected LogRecord. This implementation returns the greater of two LogRecords.

Parameters
t:LogRecord

the current record.

u:LogRecord

the record that could replace the current.

Returns:LogRecord

the greater of the given log records.

Exceptions
NullPointerException:
may occur if either record is null.
finishback to summary
protected String finish(String s)

Applied to the head, format, and tail returned by the target formatter. This implementation trims all input strings.

Parameters
s:String

the string to transform.

Returns:String

the transformed string.

Exceptions
NullPointerException:
if the given string is null.
formatback to summary
public String format(final LogRecord record)

Implements abstract java.util.logging.Formatter.format.

Accumulates log records which will be used to produce the final output. The output is generated using the getTail method which also resets this formatter back to its original state.

Parameters
record:LogRecord

the record to store.

Returns:String

an empty string.

Annotations
@Override
Exceptions
NullPointerException:
if the given record is null.
formatRecordback to summary
private String formatRecord(final Handler h, final boolean reset)

Formats the given record with the head and tail.

Parameters
h:Handler

the Handler or null.

reset:boolean

true if the summary statistics and LogRecord should be reset back to initial values.

Returns:String

the formatted string.

See Also
getTail(java.util.logging.Handler)
getTailback to summary
public String getTail(final Handler h)

Overrides java.util.logging.Formatter.getTail.

Formats the collected LogRecord and summary statistics. The collected results are reset after calling this method. The java.text argument indexes are assigned to the following properties:

  1. head the head string returned from the target formatter and finished by this formatter.
  2. formatted the current log record formatted by the target formatter and finished by this formatter. If the formatter is null then record is formatted by this formatter.
  3. tail the tail string returned from the target formatter and finished by this formatter.
  4. count the total number of log records consumed by this formatter.
  5. remaining the count minus one.
  6. thrown the total number of log records consumed by this formatter with an assigned throwable.
  7. normal messages the count minus the thrown.
  8. minMillis the eldest log record event time consumed by this formatter. If the count is zero then this is set to the previous max or approximate start time if there was no previous max. By default this parameter is defined as a number. The format type and format style rules from the java.text.MessageFormat should be used to convert this from milliseconds to a date or time.
  9. maxMillis the most recent log record event time consumed by this formatter. If the count is zero then this is set to the current time. By default this parameter is defined as a number. The format type and format style rules from the java.text.MessageFormat should be used to convert this from milliseconds to a date or time.
  10. elapsed the elapsed time in milliseconds between the maxMillis and minMillis.
  11. startTime the approximate start time in milliseconds. By default this parameter is defined as a number. The format type and format style rules from the java.text.MessageFormat should be used to convert this from milliseconds to a date or time.
  12. currentTime the current time in milliseconds. By default this parameter is defined as a number. The format type and format style rules from the java.text.MessageFormat should be used to convert this from milliseconds to a date or time.
  13. uptime the elapsed time in milliseconds between the currentTime and startTime.
  14. generation the number times this method produced output with at least one consumed log record. This can be used to track the number of complete reports this formatter has produced.

Some example formats:

  • com.sun.mail.util.logging.CollectorFormatter.format={0}{1}{2}{4,choice,-1#|0#|0<... {4,number,integer} more}\n

    This prints the head ({0}), format ({1}), and tail ({2}) from the target formatter followed by the number of remaining ({4}) log records consumed by this formatter if there are any remaining records.

    Encoding failed.|NullPointerException: null String.getBytes(:913)... 3 more
    
  • com.sun.mail.util.logging.CollectorFormatter.format=These {3} messages occurred between\n{7,date,EEE, MMM dd HH:mm:ss:S ZZZ yyyy} and {8,time,EEE, MMM dd HH:mm:ss:S ZZZ yyyy}\n

    This prints the count ({3}) followed by the date and time of the eldest log record ({7}) and the date and time of the most recent log record ({8}).

    These 292 messages occurred between
    Tue, Jul 21 14:11:42:449 -0500 2009 and Fri, Nov 20 07:29:24:0 -0600 2009
    
  • com.sun.mail.util.logging.CollectorFormatter.format=These {3} messages occurred between {9,choice,86400000#{7,date} {7,time} and {8,time}|86400000<{7,date} and {8,date}}\n

    This prints the count ({3}) and then chooses the format based on the elapsed time ({9}). If the elapsed time is less than one day then the eldest log record ({7}) date and time is formatted followed by just the time of the most recent log record ({8}. Otherwise, the just the date of the eldest log record ({7}) and just the date of most recent log record ({8} is formatted.

    These 73 messages occurred between Jul 21, 2009 2:11:42 PM and 2:13:32 PM
    
    These 116 messages occurred between Jul 21, 2009 and Aug 20, 2009
    
  • com.sun.mail.util.logging.CollectorFormatter.format={13} alert reports since {10,date}.\n

    This prints the generation ({13}) followed by the start time ({10}) formatted as a date.

    4,320 alert reports since Jul 21, 2012.
    
Parameters
h:Handler

the handler or null.

Returns:String

the output string.

Annotations
@Override
initComparatorback to summary
private Comparator<? super LogRecord> initComparator(final String p)

Gets and creates the comparator from the LogManager or returns the default comparator.

Parameters
p:String

the class name prefix.

Returns:Comparator<? super LogRecord>

the comparator or null.

Annotations
@SuppressWarnings:unchecked
Exceptions
IllegalArgumentException:
if it was specified that the comparator should be reversed but no initial comparator was specified.
NullPointerException:
if the given argument is null.
UndeclaredThrowableException:
if the comparator can not be created.
initFormatback to summary
private String initFormat(final String p)

Gets the message format string from the LogManager or creates the default message format string.

Parameters
p:String

the class name prefix.

Returns:String

the format string.

Exceptions
NullPointerException:
if the given argument is null.
initFormatterback to summary
private Formatter initFormatter(final String p)

Gets and creates the formatter from the LogManager or creates the default formatter.

Parameters
p:String

the class name prefix.

Returns:Formatter

the formatter.

Exceptions
NullPointerException:
if the given argument is null.
UndeclaredThrowableException:
if the formatter can not be created.
peekback to summary
private synchronized LogRecord peek()

Peek at the current log record.

Returns:LogRecord

null or the current log record.

resetback to summary
private synchronized void reset(final long min)

Resets all of the collected summary statistics including the LogRecord.

Parameters
min:long

the current min milliseconds.

toStringback to summary
public String toString()

Overrides java.lang.Object.toString.

Formats the collected LogRecord and summary statistics. The LogRecord and summary statistics are not changed by calling this method.

Returns:String

the current record formatted or the default toString.

Annotations
@Override
See Also
getTail(java.util.logging.Handler)