Module jakarta.mail

Class CompactFormatter


  • public class CompactFormatter
    extends java.util.logging.Formatter
    A plain text formatter that can produce fixed width output. By default this formatter will produce output no greater than 160 characters (Unicode code points) 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.

    • <formatter-name>.format - the format string used to transform the output. The arguments are explained in detail in the format documentation. The format string can be used to fix the output size. (defaults to %7$#.160s%n)
    Since:
    JavaMail 1.5.2
    Author:
    Jason Mehrens
    • Constructor Summary

      Constructors 
      Constructor Description
      CompactFormatter()
      Creates an instance with a default format pattern.
      CompactFormatter​(java.lang.String format)
      Creates an instance with the given format pattern.
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      protected java.lang.Throwable apply​(java.lang.Throwable t)
      Chooses a single throwable from the cause chain that will be formatted.
      java.lang.String format​(java.util.logging.LogRecord record)
      Format the given log record and returns the formatted string.
      java.lang.String formatBackTrace​(java.util.logging.LogRecord record)
      Formats the back trace for the given log record.
      java.lang.String formatError​(java.util.logging.LogRecord record)
      Formats the thrown property of a LogRecord as an error message.
      java.lang.String formatLevel​(java.util.logging.LogRecord record)
      Formats the level property of the given log record.
      java.lang.String formatLoggerName​(java.util.logging.LogRecord record)
      Formats the logger name property of the given log record.
      java.lang.String formatMessage​(java.lang.Throwable t)
      Formats the message from the thrown property of the log record.
      java.lang.String formatMessage​(java.util.logging.LogRecord record)
      Formats message for the log record.
      java.lang.String formatSource​(java.util.logging.LogRecord record)
      Formats the source from the given log record.
      java.lang.Number formatThreadID​(java.util.logging.LogRecord record)
      Formats the thread id property of the given log record.
      java.lang.String formatThrown​(java.util.logging.LogRecord record)
      Formats the thrown property of a LogRecord.
      protected boolean ignore​(java.lang.StackTraceElement s)
      Determines if a stack frame should be ignored as the cause of an error.
      protected java.lang.String toAlternate​(java.lang.String s)
      Defines the alternate format.
      • Methods inherited from class java.util.logging.Formatter

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

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

      • CompactFormatter

        public CompactFormatter()
        Creates an instance with a default format pattern.
      • CompactFormatter

        public CompactFormatter​(java.lang.String format)
        Creates an instance with the given format pattern.
        Parameters:
        format - the pattern or null to use the LogManager default. The arguments are described in the format method.
    • Method Detail

      • format

        public java.lang.String format​(java.util.logging.LogRecord record)
        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. Width and precision are by Unicode code points. This parameter supports alternate form.
        9. thrown|message The thrown and message properties joined as one parameter. Width and precision are by Unicode code points. 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. Width and precision are by Unicode code points. This parameter supports alternate form.
        14. error|message The error and message properties joined as one parameter. Width and precision are by Unicode code points. 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 (Unicode code points) 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 (Unicode code points) 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 (Unicode code points) 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)
           
        Specified by:
        format in class java.util.logging.Formatter
        Parameters:
        record - the log record to format.
        Returns:
        the formatted record.
        Throws:
        java.lang.NullPointerException - if the given record is null.
      • formatMessage

        public java.lang.String formatMessage​(java.util.logging.LogRecord record)
        Formats message for the log record. This method removes any fully qualified throwable class names from the message.
        Overrides:
        formatMessage in class java.util.logging.Formatter
        Parameters:
        record - the log record.
        Returns:
        the formatted message string.
      • formatMessage

        public java.lang.String formatMessage​(java.lang.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 - the throwable to format or null.
        Returns:
        the empty string if null was given or the formatted message string from the throwable which may be null.
      • formatLevel

        public java.lang.String formatLevel​(java.util.logging.LogRecord record)
        Formats the level property of the given log record.
        Parameters:
        record - the record.
        Returns:
        the formatted logger name.
        Throws:
        java.lang.NullPointerException - if the given record is null.
      • formatSource

        public java.lang.String formatSource​(java.util.logging.LogRecord record)
        Formats the source from the given log record.
        Parameters:
        record - the record.
        Returns:
        the formatted source of the log record.
        Throws:
        java.lang.NullPointerException - if the given record is null.
      • formatLoggerName

        public java.lang.String formatLoggerName​(java.util.logging.LogRecord record)
        Formats the logger name property of the given log record.
        Parameters:
        record - the record.
        Returns:
        the formatted logger name.
        Throws:
        java.lang.NullPointerException - if the given record is null.
      • formatThreadID

        public java.lang.Number formatThreadID​(java.util.logging.LogRecord record)
        Formats the thread id property of the given log record. Long thread ids are preferred if supported. Otherwise, the integer thread id is formatted as a long by an unsigned conversion.
        Parameters:
        record - the record.
        Returns:
        the formatted thread id as a number.
        Throws:
        java.lang.NullPointerException - if the given record is null.
        Since:
        JavaMail 1.5.4
      • formatThrown

        public java.lang.String formatThrown​(java.util.logging.LogRecord record)
        Formats the thrown property of a LogRecord. The returned string will contain a throwable message with a back trace.
        Parameters:
        record - the record.
        Returns:
        empty string if nothing was thrown or formatted string.
        Throws:
        java.lang.NullPointerException - if the given record is null.
        See Also:
        apply(java.lang.Throwable), formatBackTrace(java.util.logging.LogRecord)
      • formatError

        public java.lang.String formatError​(java.util.logging.LogRecord record)
        Formats the thrown property of a LogRecord as an error message. The returned string will not contain a back trace.
        Parameters:
        record - the record.
        Returns:
        empty string if nothing was thrown or formatted string.
        Throws:
        java.lang.NullPointerException - if the given record is null.
        Since:
        JavaMail 1.5.4
        See Also:
        Throwable.toString(), apply(java.lang.Throwable), formatMessage(java.lang.Throwable)
      • apply

        protected java.lang.Throwable apply​(java.lang.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 - the throwable from the log record.
        Returns:
        the chosen throwable or null only if the given argument is null.
        See Also:
        formatThrown(java.util.logging.LogRecord)
      • ignore

        protected boolean ignore​(java.lang.StackTraceElement s)
        Determines if a stack frame should be ignored as the cause of an error.
        Parameters:
        s - the stack trace element.
        Returns:
        true if this frame should be ignored.
        See Also:
        formatThrown(java.util.logging.LogRecord)
      • toAlternate

        protected java.lang.String toAlternate​(java.lang.String s)
        Defines the alternate format. This implementation removes all control characters from the given string.
        Parameters:
        s - any string or null.
        Returns:
        null if the argument was null otherwise, an alternate string.