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

public Class CompactFormatter

extends Formatter
Class Inheritance
Imports
java.util.*, java.util.logging.LogRecord

A plain text formatter that can produce fixed width output. By default this formatter will produce output no greater than 160 characters wide plus the separator and newline characters. Only specified fields support an alternate fixed width format.

By default each CompactFormatter 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

Nested and Inner Type Summary

Modifier and TypeClass and Description
private class
CompactFormatter.Alternate

Used to format two arguments as fixed length message.

Field Summary

Modifier and TypeField and Description
private final String
fmt

Holds the java.util.Formatter pattern.

Constructor Summary

AccessConstructor and Description
public
CompactFormatter()

Creates an instance with a default format pattern.

public
CompactFormatter(final String
the pattern or null to use the LogManager default. The arguments are described in the format method.
format
)

Creates an instance with the given format pattern.

Method Summary

Modifier and TypeMethod and Description
protected Throwable

Returns:

the chosen throwable or null only if the given argument is null.
apply
(final Throwable
the throwable from the log record.
t
)

Chooses a single throwable from the cause chain that will be formatted.

private boolean

Returns:

true if this frame should be ignored.
defaultIgnore
(final StackTraceElement
the stack trace element.
s
)

Determines if a stack frame should be ignored as the cause of an error.

private String

Returns:

a String that best describes the call site.
findAndFormat
(final StackTraceElement[]
the fill stack to examine.
trace
)

Finds and formats the first stack frame of interest.

public String

Returns:

the formatted record.
format
(final LogRecord
the log record to format.
record
)

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

Format the given log record and returns the formatted string.
public String

Returns:

the formatted back trace.
formatBackTrace
(final LogRecord
the log record to format.
record
)

Formats the back trace for the given log record.

public String

Returns:

empty string if nothing was thrown or formatted string.
formatError
(final LogRecord
the record.
record
)

Formats the thrown property of a LogRecord as an error message.

public String

Returns:

the formatted logger name.
formatLevel
(final LogRecord
the record.
record
)

Formats the level property of the given log record.

public String

Returns:

the formatted logger name.
formatLoggerName
(final LogRecord
the record.
record
)

Formats the logger name property of the given log record.

public String

Returns:

the formatted message string.
formatMessage
(final LogRecord
the log record.
record
)

Overrides java.util.logging.Formatter.formatMessage.

Formats message for the log record.
public String

Returns:

the empty string if null was given or the formatted message string from the throwable which may be null.
formatMessage
(final Throwable
the throwable to format or null.
t
)

Formats the message from the thrown property of the log record.

public String

Returns:

the formatted source of the log record.
formatSource
(final LogRecord
the record.
record
)

Formats the source from the given log record.

private String

Returns:

the formatted stack trace element.
formatStackTraceElement
(final StackTraceElement
the stack trace element to format.
s
)

Formats a stack trace element into a simple call site.

public Number

Returns:

the formatted thread id as a number.
formatThreadID
(final LogRecord
the record.
record
)

Formats the thread id property of the given log record.

public String

Returns:

empty string if nothing was thrown or formatted string.
formatThrown
(final LogRecord
the record.
record
)

Formats the thrown property of a LogRecord.

private Comparable<?>

Returns:

a zoned date time or a legacy date object.
formatZonedDateTime
(final LogRecord
the current log record.
record
)

Gets the zoned date time from the given log record.

protected boolean

Returns:

true if this frame should be ignored.
ignore
(final StackTraceElement
the stack trace element.
s
)

Determines if a stack frame should be ignored as the cause of an error.

private String

Returns:

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

Creates the format pattern for this formatter.

private static boolean

Returns:

true if null or spaces.
isNullOrSpaces
(final String
the string or null.
s
)

Determines is the given string is null or spaces.

private boolean

Returns:

true if the line number is unknown.
isReflection
(final StackTraceElement
the stack trace element.
s
)

Determines if a stack trace element represents a reflection frame.

private boolean

Returns:

true if this frame should be ignored.
isStaticUtility
(final StackTraceElement
the stack trace element.
s
)

Determines if a stack frame is for a static utility class.

private boolean

Returns:

true if synthetic.
isSynthetic
(final StackTraceElement
the stack trace element.
s
)

Determines if a stack trace element is for a synthetic method.

private boolean

Returns:

true if the line number is unknown.
isUnknown
(final StackTraceElement
the stack trace element.
s
)

Determines if a stack trace element has an unknown line number or a native line number.

private static Class<?>[]

Returns:

an array of classes never null.
loadDeclaredClasses
()

Used to load declared classes encase class loader doesn't allow loading during JVM termination.

private static String

Returns:

the modified message string.
replaceClassName
(String
the message.
msg
,
Throwable
the throwable cause chain to search or null.
t
)

Searches the given message for all instances fully qualified class name with simple class name based off of the types contained in the given parameter array.

private static String

Returns:

the modified message string.
replaceClassName
(String
the message or null.
msg
,
Object[]
the parameter array or null.
p
)

Searches the given message for all instances fully qualified class name with simple class name based off of the types contained in the given parameter array.

private static String

Returns:

the simple class name or null.
simpleClassName
(final Class<?>
the class object.
k
)

Gets the simple class name from the given class.

private static String

Returns:

the simple class name or given input.
simpleClassName
(String
the fully qualified class name prefix or null.
name
)

Converts a fully qualified class name to a simple class name.

private static String

Returns:

the simple file name or null.
simpleFileName
(String
the full file name or null.
name
)

Converts a file name with an extension to a file name without an extension.

protected String

Returns:

null if the argument was null otherwise, an alternate string.
toAlternate
(final String
any string or null.
s
)

Defines the alternate format.

Inherited from java.util.logging.Formatter:
getHeadgetTail

Field Detail

fmtback to summary
private final String fmt

Holds the java.util.Formatter pattern.

Constructor Detail

CompactFormatterback to summary
public CompactFormatter()

Creates an instance with a default format pattern.

CompactFormatterback to summary
public CompactFormatter(final String format)

Creates an instance with the given format pattern.

Parameters
format:String

the pattern or null to use the LogManager default. The arguments are described in the format method.

Method Detail

applyback to summary
protected Throwable apply(final Throwable t)

Chooses a single throwable from the cause chain that will be formatted. This implementation chooses the throwable that best describes the chain. Subclasses can override this method to choose an alternate throwable for formatting.

Parameters
t:Throwable

the throwable from the log record.

Returns:Throwable

the chosen throwable or null only if the given argument is null.

See Also
formatThrown(java.util.logging.LogRecord)
defaultIgnoreback to summary
private boolean defaultIgnore(final StackTraceElement s)

Determines if a stack frame should be ignored as the cause of an error. This does not check for unknown line numbers because code can be compiled without debugging info.

Parameters
s:StackTraceElement

the stack trace element.

Returns:boolean

true if this frame should be ignored.

findAndFormatback to summary
private String findAndFormat(final StackTraceElement[] trace)

Finds and formats the first stack frame of interest.

Parameters
trace:StackTraceElement[]

the fill stack to examine.

Returns:String

a String that best describes the call site.

Exceptions
NullPointerException:
if stack trace element array is null.
formatback to summary
public String format(final LogRecord record)

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

Format the given log record and returns the formatted string. The java.util argument indexes are assigned to the following properties:

  1. format - the java.util.Formatter format string specified in the <formatter-name>.format property or the format that was given when this formatter was created.
  2. date - if the log record supports nanoseconds then a ZonedDateTime object representing the event time of the log record in the system time zone. Otherwise, a Date object representing event time of the log record.
  3. source - a string representing the caller, if available; otherwise, the logger's name.
  4. logger - the logger's simple name.
  5. level - the log level.
  6. message - the formatted log message returned from the formatMessage(LogRecord) method.
  7. thrown - a string representing the throwable associated with the log record and a relevant stack trace element if available; otherwise, an empty string is used.
  8. message|thrown The message and the thrown properties joined as one parameter. This parameter supports alternate form.
  9. thrown|message The thrown and message properties joined as one parameter. This parameter supports alternate form.
  10. sequence the sequence number if the given log record.
  11. thread id the thread id of the given log record. By default this is formatted as a long by an unsigned conversion.
  12. error the throwable simple class name and error message without any stack trace.
  13. message|error The message and error properties joined as one parameter. This parameter supports alternate form.
  14. error|message The error and message properties joined as one parameter. This parameter supports alternate form.
  15. backtrace only the stack trace of the given throwable.
  16. bundlename the resource bundle name of the given log record.
  17. key the raw message before localization or formatting.

Some example formats:

  • com.sun.mail.util.logging.CompactFormatter.format=%7$#.160s%n

    This prints only 160 characters of the message|thrown (7$) using the alternate form. The separator is not included as part of the total width.

    Encoding failed.|NullPointerException: null String.getBytes(:913)
    
  • com.sun.mail.util.logging.CompactFormatter.format=%7$#.20s%n

    This prints only 20 characters of the message|thrown (7$) using the alternate form. This will perform a weighted truncation of both the message and thrown properties of the log record. The separator is not included as part of the total width.

    Encoding|NullPointerE
    
  • com.sun.mail.util.logging.CompactFormatter.format=%1$tc %2$s%n%4$s: %5$s%6$s%n

    This prints the timestamp (1$) and the source (2$) on the first line. The second line is the log level (4$), log message (5$), and the throwable with a relevant stack trace element (6$) if one is available.

    Fri Nov 20 07:29:24 CST 2009 MyClass fatal
    SEVERE: Encoding failed.NullPointerException: null String.getBytes(:913)
    
  • com.sun.mail.util.logging.CompactFormatter.format=%4$s: %12$#.160s%n

    This prints the log level (4$) and only 160 characters of the message|error (12$) using the alternate form.

    SEVERE: Unable to send notification.|SocketException: Permission denied: connect
    
  • com.sun.mail.util.logging.CompactFormatter.format=[%9$d][%1$tT][%10$d][%2$s] %5$s%n%6$s%n

    This prints the sequence (9$), event time (1$) as 24 hour time, thread id (10$), source (2$), log message (5$), and the throwable with back trace (6$).

    [125][14:11:42][38][MyClass fatal] Unable to send notification.
    SocketException: Permission denied: connect SMTPTransport.openServer(:1949)
    
Parameters
record:LogRecord

the log record to format.

Returns:String

the formatted record.

Annotations
@Override
Exceptions
NullPointerException:
if the given record is null.
formatBackTraceback to summary
public String formatBackTrace(final LogRecord record)

Formats the back trace for the given log record.

Parameters
record:LogRecord

the log record to format.

Returns:String

the formatted back trace.

Exceptions
NullPointerException:
if the given record is null.
See Also
apply(java.lang.Throwable), formatThrown(java.util.logging.LogRecord), ignore(java.lang.StackTraceElement)
formatErrorback to summary
public String formatError(final LogRecord record)

Formats the thrown property of a LogRecord as an error message. The returned string will not contain a back trace.

Parameters
record:LogRecord

the record.

Returns:String

empty string if nothing was thrown or formatted string.

Exceptions
NullPointerException:
if the given record is null.
Since
JavaMail 1.5.4
See Also
Throwable#toString(), apply(java.lang.Throwable), formatMessage(java.lang.Throwable)
formatLevelback to summary
public String formatLevel(final LogRecord record)

Formats the level property of the given log record.

Parameters
record:LogRecord

the record.

Returns:String

the formatted logger name.

Exceptions
NullPointerException:
if the given record is null.
formatLoggerNameback to summary
public String formatLoggerName(final LogRecord record)

Formats the logger name property of the given log record.

Parameters
record:LogRecord

the record.

Returns:String

the formatted logger name.

Exceptions
NullPointerException:
if the given record is null.
formatMessageback to summary
public String formatMessage(final LogRecord record)

Overrides java.util.logging.Formatter.formatMessage.

Formats message for the log record. This method removes any fully qualified throwable class names from the message.

Parameters
record:LogRecord

the log record.

Returns:String

the formatted message string.

Annotations
@Override
formatMessageback to summary
public String formatMessage(final Throwable t)

Formats the message from the thrown property of the log record. This method replaces fully qualified throwable class names from the message cause chain with simple class names.

Parameters
t:Throwable

the throwable to format or null.

Returns:String

the empty string if null was given or the formatted message string from the throwable which may be null.

formatSourceback to summary
public String formatSource(final LogRecord record)

Formats the source from the given log record.

Parameters
record:LogRecord

the record.

Returns:String

the formatted source of the log record.

Exceptions
NullPointerException:
if the given record is null.
formatStackTraceElementback to summary
private String formatStackTraceElement(final StackTraceElement s)

Formats a stack trace element into a simple call site.

Parameters
s:StackTraceElement

the stack trace element to format.

Returns:String

the formatted stack trace element.

Exceptions
NullPointerException:
if stack trace element is null.
See Also
formatThrown(java.util.logging.LogRecord)
formatThreadIDback to summary
public Number formatThreadID(final LogRecord record)

Formats the thread id property of the given log record. By default this is formatted as a long by an unsigned conversion.

Parameters
record:LogRecord

the record.

Returns:Number

the formatted thread id as a number.

Exceptions
NullPointerException:
if the given record is null.
Since
JavaMail 1.5.4
formatThrownback to summary
public String formatThrown(final LogRecord record)

Formats the thrown property of a LogRecord. The returned string will contain a throwable message with a back trace.

Parameters
record:LogRecord

the record.

Returns:String

empty string if nothing was thrown or formatted string.

Exceptions
NullPointerException:
if the given record is null.
See Also
apply(java.lang.Throwable), formatBackTrace(java.util.logging.LogRecord)
formatZonedDateTimeback to summary
private Comparable<?> formatZonedDateTime(final LogRecord record)

Gets the zoned date time from the given log record.

Parameters
record:LogRecord

the current log record.

Returns:Comparable<?>

a zoned date time or a legacy date object.

Exceptions
NullPointerException:
if the given record is null.
Since
JavaMail 1.5.6
ignoreback to summary
protected boolean ignore(final StackTraceElement s)

Determines if a stack frame should be ignored as the cause of an error.

Parameters
s:StackTraceElement

the stack trace element.

Returns:boolean

true if this frame should be ignored.

See Also
formatThrown(java.util.logging.LogRecord)
initFormatback to summary
private String initFormat(final String p)

Creates the format pattern for this formatter.

Parameters
p:String

the class name prefix.

Returns:String

the java.util.Formatter format string.

Exceptions
NullPointerException:
if the given class name is null.
isNullOrSpacesback to summary
private static boolean isNullOrSpaces(final String s)

Determines is the given string is null or spaces.

Parameters
s:String

the string or null.

Returns:boolean

true if null or spaces.

isReflectionback to summary
private boolean isReflection(final StackTraceElement s)

Determines if a stack trace element represents a reflection frame.

Parameters
s:StackTraceElement

the stack trace element.

Returns:boolean

true if the line number is unknown.

Exceptions
NullPointerException:
if stack trace element is null.
isStaticUtilityback to summary
private boolean isStaticUtility(final StackTraceElement s)

Determines if a stack frame is for a static utility class.

Parameters
s:StackTraceElement

the stack trace element.

Returns:boolean

true if this frame should be ignored.

isSyntheticback to summary
private boolean isSynthetic(final StackTraceElement s)

Determines if a stack trace element is for a synthetic method.

Parameters
s:StackTraceElement

the stack trace element.

Returns:boolean

true if synthetic.

Exceptions
NullPointerException:
if stack trace element is null.
isUnknownback to summary
private boolean isUnknown(final StackTraceElement s)

Determines if a stack trace element has an unknown line number or a native line number.

Parameters
s:StackTraceElement

the stack trace element.

Returns:boolean

true if the line number is unknown.

Exceptions
NullPointerException:
if stack trace element is null.
loadDeclaredClassesback to summary
private static Class<?>[] loadDeclaredClasses()

Used to load declared classes encase class loader doesn't allow loading during JVM termination. This method is used with unit testing.

Returns:Class<?>[]

an array of classes never null.

replaceClassNameback to summary
private static String replaceClassName(String msg, Throwable t)

Searches the given message for all instances fully qualified class name with simple class name based off of the types contained in the given parameter array.

Parameters
msg:String

the message.

t:Throwable

the throwable cause chain to search or null.

Returns:String

the modified message string.

replaceClassNameback to summary
private static String replaceClassName(String msg, Object[] p)

Searches the given message for all instances fully qualified class name with simple class name based off of the types contained in the given parameter array.

Parameters
msg:String

the message or null.

p:Object[]

the parameter array or null.

Returns:String

the modified message string.

simpleClassNameback to summary
private static String simpleClassName(final Class<?> k)

Gets the simple class name from the given class. This is a workaround for BUG ID JDK-8057919.

Parameters
k:Class<?>

the class object.

Returns:String

the simple class name or null.

Since
JavaMail 1.5.3
simpleClassNameback to summary
private static String simpleClassName(String name)

Converts a fully qualified class name to a simple class name. If the leading part of the given string is not a legal class name then the given string is returned.

Parameters
name:String

the fully qualified class name prefix or null.

Returns:String

the simple class name or given input.

simpleFileNameback to summary
private static String simpleFileName(String name)

Converts a file name with an extension to a file name without an extension.

Parameters
name:String

the full file name or null.

Returns:String

the simple file name or null.

toAlternateback to summary
protected String toAlternate(final String s)

Defines the alternate format. This implementation removes all control characters from the given string.

Parameters
s:String

any string or null.

Returns:String

null if the argument was null otherwise, an alternate string.

com.sun.mail.util.logging back to summary

private Class CompactFormatter.Alternate

extends Object
implements Formattable
Class Inheritance
All Implemented Interfaces
java.util.Formattable

Used to format two arguments as fixed length message.

Field Summary

Modifier and TypeField and Description
private final String
left

The left side of the output.

private final String
right

The right side of the output.

Constructor Summary

AccessConstructor and Description
pack-priv
Alternate(final String
the left side or null.
left
,
final String
the right side or null.
right
)

Creates an alternate output.

Method Summary

Modifier and TypeMethod and Description
public void
formatTo(Formatter
The formatter. Implementing classes may call formatter.out() or formatter.locale() to obtain the Appendable or Locale used by this formatter respectively.
formatter
,
int
The flags modify the output format. The value is interpreted as a bitmask. Any combination of the following flags may be set: FormattableFlags#LEFT_JUSTIFY, FormattableFlags#UPPERCASE, and FormattableFlags#ALTERNATE. If no flags are set, the default formatting of the implementing class will apply.
flags
,
int
The minimum number of characters to be written to the output. If the length of the converted value is less than the width then the output will be padded by '  ' until the total number of characters equals width. The padding is at the beginning by default. If the FormattableFlags#LEFT_JUSTIFY flag is set then the padding will be at the end. If width is -1 then there is no minimum.
width
,
int
The maximum number of characters to be written to the output. The precision is applied before the width, thus the output will be truncated to precision characters even if the width is greater than the precision. If precision is -1 then there is no explicit limit on the number of characters.
precision
)

Implements java.util.Formattable.formatTo.

Formats the object using the provided formatter.
private String

Returns:

the padded string.
pad
(int
the formatter flags.
flags
,
String
the string to pad.
s
,
int
the final string length.
length
)

Pad the given input string.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

leftback to summary
private final String left

The left side of the output.

rightback to summary
private final String right

The right side of the output.

Constructor Detail

Alternateback to summary
pack-priv Alternate(final String left, final String right)

Creates an alternate output.

Parameters
left:String

the left side or null.

right:String

the right side or null.

Method Detail

formatToback to summary
public void formatTo(Formatter formatter, int flags, int width, int precision)

Implements java.util.Formattable.formatTo.

Doc from java.util.Formattable.formatTo.

Formats the object using the provided formatter.

Parameters
formatter:Formatter

The formatter. Implementing classes may call formatter.out() or formatter.locale() to obtain the Appendable or Locale used by this formatter respectively.

flags:int

The flags modify the output format. The value is interpreted as a bitmask. Any combination of the following flags may be set: FormattableFlags#LEFT_JUSTIFY, FormattableFlags#UPPERCASE, and FormattableFlags#ALTERNATE. If no flags are set, the default formatting of the implementing class will apply.

width:int

The minimum number of characters to be written to the output. If the length of the converted value is less than the width then the output will be padded by '  ' until the total number of characters equals width. The padding is at the beginning by default. If the FormattableFlags#LEFT_JUSTIFY flag is set then the padding will be at the end. If width is -1 then there is no minimum.

precision:int

The maximum number of characters to be written to the output. The precision is applied before the width, thus the output will be truncated to precision characters even if the width is greater than the precision. If precision is -1 then there is no explicit limit on the number of characters.

Annotations
@SuppressWarnings:override
padback to summary
private String pad(int flags, String s, int length)

Pad the given input string.

Parameters
flags:int

the formatter flags.

s:String

the string to pad.

length:int

the final string length.

Returns:String

the padded string.