Class CollectorFormatter
- java.lang.Object
-
- java.util.logging.Formatter
-
- com.sun.mail.util.logging.CollectorFormatter
-
public class CollectorFormatter extends java.util.logging.Formatter
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.
- <formatter-name>.comparator name of a Comparator class used to choose the collected LogRecord. If a comparator is specified then the max LogRecord is chosen. If comparator is set to the string literal null, then the last record is chosen. (defaults to SeverityComparator)
- <formatter-name>.comparator.reverse a boolean true to collect the min LogRecord or false to collect the max LogRecord. (defaults to false)
- <formatter-name>.format the MessageFormat string used to format the collected summary statistics. The arguments are explained in detail in the getTail documentation. (defaults to {0}{1}{2}{4,choice,-1#|0#|0<... {4,number,integer} more}\n)
- <formatter-name>.formatter name of a Formatter class used to format the collected LogRecord. (defaults to CompactFormatter)
- Since:
- JavaMail 1.5.2
-
-
Field Summary
Fields Modifier and Type Field Description private java.util.Comparator<? super java.util.logging.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 java.lang.String
fmt
The message format string used as the formatted output.private java.util.logging.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 long
INIT_TIME
Avoid depending on JMX runtime bean to get the start time.private java.util.logging.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
Constructors Constructor Description CollectorFormatter()
Creates the formatter using the LogManager defaults.CollectorFormatter(java.lang.String format)
Creates the formatter using the given format.CollectorFormatter(java.lang.String format, java.util.logging.Formatter f, java.util.Comparator<? super java.util.logging.LogRecord> c)
Creates the formatter using the given values.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description private boolean
accept(java.util.logging.LogRecord e, java.util.logging.LogRecord u)
Updates the summary statistics only if the expected record matches the last record.private boolean
acceptAndUpdate(java.util.logging.LogRecord e, java.util.logging.LogRecord u)
Updates the summary statistics and stores given LogRecord if the expected record matches the current record.protected java.util.logging.LogRecord
apply(java.util.logging.LogRecord t, java.util.logging.LogRecord u)
Used to choose the collected LogRecord.protected java.lang.String
finish(java.lang.String s)
Applied to the head, format, and tail returned by the target formatter.java.lang.String
format(java.util.logging.LogRecord record)
Accumulates log records which will be used to produce the final output.private java.lang.String
formatRecord(java.util.logging.Handler h, boolean reset)
Formats the given record with the head and tail.java.lang.String
getTail(java.util.logging.Handler h)
Formats the collected LogRecord and summary statistics.private java.util.Comparator<? super java.util.logging.LogRecord>
initComparator(java.lang.String p)
Gets and creates the comparator from the LogManager or returns the default comparator.private java.lang.String
initFormat(java.lang.String p)
Gets the message format string from the LogManager or creates the default message format string.private java.util.logging.Formatter
initFormatter(java.lang.String p)
Gets and creates the formatter from the LogManager or creates the default formatter.private java.util.logging.LogRecord
peek()
Peek at the current log record.private void
reset(long min)
Resets all of the collected summary statistics including the LogRecord.java.lang.String
toString()
Formats the collected LogRecord and summary statistics.
-
-
-
Field Detail
-
INIT_TIME
private static final long INIT_TIME
Avoid depending on JMX runtime bean to get the start time.
-
fmt
private final java.lang.String fmt
The message format string used as the formatted output.
-
formatter
private final java.util.logging.Formatter formatter
The formatter used to format the chosen log record.
-
comparator
private final java.util.Comparator<? super java.util.logging.LogRecord> comparator
The comparator used to pick the log record to format.
-
last
private java.util.logging.LogRecord last
The last accepted record. Synchronized access is preferred over volatile for this class.
-
count
private long count
The number of log records that have been formatted.
-
generation
private long generation
The number of log produced containing at least one log record. Only incremented when this formatter is reset.
-
thrown
private long thrown
The number of log records that have been formatted with a thrown object.
-
minMillis
private long minMillis
The eldest log record time or eldest time possible for this instance.
-
maxMillis
private long maxMillis
The newest log record time.
-
-
Constructor Detail
-
CollectorFormatter
public CollectorFormatter()
Creates the formatter using the LogManager defaults.- Throws:
java.lang.SecurityException
- if a security manager exists and the caller does not have LoggingPermission("control").java.lang.reflect.UndeclaredThrowableException
- if there are problems loading from the LogManager.
-
CollectorFormatter
public CollectorFormatter(java.lang.String format)
Creates the formatter using the given format.- Parameters:
format
- the message format or null to use the LogManager default.- Throws:
java.lang.SecurityException
- if a security manager exists and the caller does not have LoggingPermission("control").java.lang.reflect.UndeclaredThrowableException
- if there are problems loading from the LogManager.
-
CollectorFormatter
public CollectorFormatter(java.lang.String format, java.util.logging.Formatter f, java.util.Comparator<? super java.util.logging.LogRecord> c)
Creates the formatter using the given values.- Parameters:
format
- the format string or null to use the LogManager default.f
- the formatter used on the collected log record or null to specify no formatter.c
- the comparator used to determine which log record to format or null to specify no comparator.- Throws:
java.lang.SecurityException
- if a security manager exists and the caller does not have LoggingPermission("control").java.lang.reflect.UndeclaredThrowableException
- if there are problems loading from the LogManager.
-
-
Method Detail
-
format
public java.lang.String format(java.util.logging.LogRecord record)
Accumulates log records which will be used to produce the final output. The output is generated using thegetTail(java.util.logging.Handler)
method which also resets this formatter back to its original state.- Specified by:
format
in classjava.util.logging.Formatter
- Parameters:
record
- the record to store.- Returns:
- an empty string.
- Throws:
java.lang.NullPointerException
- if the given record is null.
-
getTail
public java.lang.String getTail(java.util.logging.Handler h)
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:head
the head string returned from the target formatter and finished by this formatter.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.tail
the tail string returned from the target formatter and finished by this formatter.count
the total number of log records consumed by this formatter.remaining
the count minus one.thrown
the total number of log records consumed by this formatter with an assigned throwable.normal messages
the count minus the thrown.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 MessageFormat should be used to convert this from milliseconds to a date or time.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 MessageFormat should be used to convert this from milliseconds to a date or time.elapsed
the elapsed time in milliseconds between themaxMillis
andminMillis
.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 MessageFormat should be used to convert this from milliseconds to a date or time.currentTime
the current time in milliseconds. By default this parameter is defined as a number. The format type and format style rules from the MessageFormat should be used to convert this from milliseconds to a date or time.uptime
the elapsed time in milliseconds between thecurrentTime
andstartTime
.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.
- Overrides:
getTail
in classjava.util.logging.Formatter
- Parameters:
h
- the handler or null.- Returns:
- the output string.
-
toString
public java.lang.String toString()
Formats the collected LogRecord and summary statistics. The LogRecord and summary statistics are not changed by calling this method.- Overrides:
toString
in classjava.lang.Object
- Returns:
- the current record formatted or the default toString.
- See Also:
getTail(java.util.logging.Handler)
-
apply
protected java.util.logging.LogRecord apply(java.util.logging.LogRecord t, java.util.logging.LogRecord u)
Used to choose the collected LogRecord. This implementation returns the greater of two LogRecords.- Parameters:
t
- the current record.u
- the record that could replace the current.- Returns:
- the greater of the given log records.
- Throws:
java.lang.NullPointerException
- may occur if either record is null.
-
accept
private boolean accept(java.util.logging.LogRecord e, java.util.logging.LogRecord u)
Updates the summary statistics only if the expected record matches the last record. The update record is not stored.- Parameters:
e
- the LogRecord that is expected.u
- the LogRecord used to collect statistics.- Returns:
- true if the last record was the expected record.
- Throws:
java.lang.NullPointerException
- if the update record is null.
-
reset
private void reset(long min)
Resets all of the collected summary statistics including the LogRecord.- Parameters:
min
- the current min milliseconds.
-
formatRecord
private java.lang.String formatRecord(java.util.logging.Handler h, boolean reset)
Formats the given record with the head and tail.- Parameters:
h
- the Handler or null.reset
- true if the summary statistics and LogRecord should be reset back to initial values.- Returns:
- the formatted string.
- See Also:
getTail(java.util.logging.Handler)
-
finish
protected java.lang.String finish(java.lang.String s)
Applied to the head, format, and tail returned by the target formatter. This implementation trims all input strings.- Parameters:
s
- the string to transform.- Returns:
- the transformed string.
- Throws:
java.lang.NullPointerException
- if the given string is null.
-
peek
private java.util.logging.LogRecord peek()
Peek at the current log record.- Returns:
- null or the current log record.
-
acceptAndUpdate
private boolean acceptAndUpdate(java.util.logging.LogRecord e, java.util.logging.LogRecord u)
Updates the summary statistics and stores given LogRecord if the expected record matches the current record.- Parameters:
e
- the expected record.u
- the update record.- Returns:
- true if the update was performed.
- Throws:
java.lang.NullPointerException
- if the update record is null.
-
initFormat
private java.lang.String initFormat(java.lang.String p)
Gets the message format string from the LogManager or creates the default message format string.- Parameters:
p
- the class name prefix.- Returns:
- the format string.
- Throws:
java.lang.NullPointerException
- if the given argument is null.
-
initFormatter
private java.util.logging.Formatter initFormatter(java.lang.String p)
Gets and creates the formatter from the LogManager or creates the default formatter.- Parameters:
p
- the class name prefix.- Returns:
- the formatter.
- Throws:
java.lang.NullPointerException
- if the given argument is null.java.lang.reflect.UndeclaredThrowableException
- if the formatter can not be created.
-
initComparator
private java.util.Comparator<? super java.util.logging.LogRecord> initComparator(java.lang.String p)
Gets and creates the comparator from the LogManager or returns the default comparator.- Parameters:
p
- the class name prefix.- Returns:
- the comparator or null.
- Throws:
java.lang.IllegalArgumentException
- if it was specified that the comparator should be reversed but no initial comparator was specified.java.lang.NullPointerException
- if the given argument is null.java.lang.reflect.UndeclaredThrowableException
- if the comparator can not be created.
-
-