Module jakarta.mail
Package jakarta.mail

Class MailLogger


  • final class MailLogger
    extends java.lang.Object
    A simplified logger used by Jakarta Mail to handle logging to a PrintStream and logging through a java.util.logging.Logger. If debug is set, messages are written to the PrintStream and prefixed by the specified prefix (which is not included in Logger messages). Messages are logged by the Logger based on the configuration of the logging system.
    • Field Summary

      Fields 
      Modifier and Type Field Description
      private boolean debug
      Produce debug output?
      private java.util.logging.Logger logger
      For log messages.
      private java.io.PrintStream out
      Stream for debug output.
      private java.lang.String prefix
      For debug output.
    • Constructor Summary

      Constructors 
      Constructor Description
      MailLogger​(java.lang.Class<?> clazz, java.lang.String prefix, boolean debug, java.io.PrintStream out)
      Construct a new MailLogger using the specified class' package name as the Logger name, debug prefix (e.g., "DEBUG"), debug flag, and PrintStream.
      MailLogger​(java.lang.Class<?> clazz, java.lang.String prefix, Session session)
      Deprecated.
      MailLogger​(java.lang.Class<?> clazz, java.lang.String subname, java.lang.String prefix, boolean debug, java.io.PrintStream out)
      Construct a new MailLogger using the specified class' package name combined with the specified subname as the Logger name, debug prefix (e.g., "DEBUG"), debug flag, and PrintStream.
      MailLogger​(java.lang.String name, java.lang.String prefix, boolean debug, java.io.PrintStream out)
      Construct a new MailLogger using the specified Logger name, debug prefix (e.g., "DEBUG"), debug flag, and PrintStream.
      MailLogger​(java.lang.String name, java.lang.String prefix, Session session)
      Deprecated.
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      void config​(java.lang.String msg)
      Log a message at the CONFIG level.
      private void debugOut​(java.lang.String msg)
      Common formatting for debug output.
      void fine​(java.lang.String msg)
      Log a message at the FINE level.
      void finer​(java.lang.String msg)
      Log a message at the FINER level.
      void finest​(java.lang.String msg)
      Log a message at the FINEST level.
      MailLogger getLogger​(java.lang.Class<?> clazz, java.lang.String prefix)
      Create a MailLogger using the specified class' package name as the Logger name and the specified prefix.
      MailLogger getLogger​(java.lang.String name, java.lang.String prefix)
      Create a MailLogger that uses a Logger with the specified name and prefix.
      MailLogger getSubLogger​(java.lang.String subname, java.lang.String prefix)
      Create a MailLogger that uses a Logger whose name is composed of this MailLogger's name plus the specified sub-name, separated by a dot.
      MailLogger getSubLogger​(java.lang.String subname, java.lang.String prefix, boolean debug)
      Create a MailLogger that uses a Logger whose name is composed of this MailLogger's name plus the specified sub-name, separated by a dot.
      private void ifDebugOut​(java.lang.String msg)
      Common code to conditionally log debug statements.
      private java.lang.StackTraceElement inferCaller()
      A disadvantage of not being able to use Logger directly in Jakarta Mail code is that the "source class" information that Logger guesses will always refer to this class instead of our caller.
      boolean isLoggable​(java.util.logging.Level level)
      If "debug" is set, or our embedded Logger is loggable at the given level, return true.
      private boolean isLoggerImplFrame​(java.lang.String cname)
      Frames to ignore as part of the MailLogger to JUL bridge.
      void log​(java.util.logging.Level level, java.lang.String msg)
      Log the message at the specified level.
      void log​(java.util.logging.Level level, java.lang.String msg, java.lang.Object param1)
      Log the message at the specified level.
      void log​(java.util.logging.Level level, java.lang.String msg, java.lang.Object... params)
      Log the message at the specified level.
      void log​(java.util.logging.Level level, java.lang.String msg, java.lang.Throwable thrown)
      Log the message at the specified level.
      void logf​(java.util.logging.Level level, java.lang.String msg, java.lang.Object... params)
      Log the message at the specified level using a format string.
      private java.lang.String packageOf​(java.lang.Class<?> clazz)
      Return the package name of the class.
      • Methods inherited from class java.lang.Object

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

      • logger

        private final java.util.logging.Logger logger
        For log messages.
      • prefix

        private final java.lang.String prefix
        For debug output.
      • debug

        private final boolean debug
        Produce debug output?
      • out

        private final java.io.PrintStream out
        Stream for debug output.
    • Constructor Detail

      • MailLogger

        public MailLogger​(java.lang.String name,
                          java.lang.String prefix,
                          boolean debug,
                          java.io.PrintStream out)
        Construct a new MailLogger using the specified Logger name, debug prefix (e.g., "DEBUG"), debug flag, and PrintStream.
        Parameters:
        name - the Logger name
        prefix - the prefix for debug output, or null for none
        debug - if true, write to PrintStream
        out - the PrintStream to write to
      • MailLogger

        public MailLogger​(java.lang.Class<?> clazz,
                          java.lang.String prefix,
                          boolean debug,
                          java.io.PrintStream out)
        Construct a new MailLogger using the specified class' package name as the Logger name, debug prefix (e.g., "DEBUG"), debug flag, and PrintStream.
        Parameters:
        clazz - the Logger name is the package name of this class
        prefix - the prefix for debug output, or null for none
        debug - if true, write to PrintStream
        out - the PrintStream to write to
      • MailLogger

        public MailLogger​(java.lang.Class<?> clazz,
                          java.lang.String subname,
                          java.lang.String prefix,
                          boolean debug,
                          java.io.PrintStream out)
        Construct a new MailLogger using the specified class' package name combined with the specified subname as the Logger name, debug prefix (e.g., "DEBUG"), debug flag, and PrintStream.
        Parameters:
        clazz - the Logger name is the package name of this class
        subname - the Logger name relative to this Logger name
        prefix - the prefix for debug output, or null for none
        debug - if true, write to PrintStream
        out - the PrintStream to write to
      • MailLogger

        @Deprecated
        public MailLogger​(java.lang.String name,
                          java.lang.String prefix,
                          Session session)
        Deprecated.
        Construct a new MailLogger using the specified Logger name and debug prefix (e.g., "DEBUG"). Get the debug flag and PrintStream from the Session.
        Parameters:
        name - the Logger name
        prefix - the prefix for debug output, or null for none
        session - where to get the debug flag and PrintStream
      • MailLogger

        @Deprecated
        public MailLogger​(java.lang.Class<?> clazz,
                          java.lang.String prefix,
                          Session session)
        Deprecated.
        Construct a new MailLogger using the specified class' package name as the Logger name and the specified debug prefix (e.g., "DEBUG"). Get the debug flag and PrintStream from the Session.
        Parameters:
        clazz - the Logger name is the package name of this class
        prefix - the prefix for debug output, or null for none
        session - where to get the debug flag and PrintStream
    • Method Detail

      • getLogger

        public MailLogger getLogger​(java.lang.String name,
                                    java.lang.String prefix)
        Create a MailLogger that uses a Logger with the specified name and prefix. The new MailLogger uses the same debug flag and PrintStream as this MailLogger.
        Parameters:
        name - the Logger name
        prefix - the prefix for debug output, or null for none
        Returns:
        a MailLogger for the given name and prefix.
      • getLogger

        public MailLogger getLogger​(java.lang.Class<?> clazz,
                                    java.lang.String prefix)
        Create a MailLogger using the specified class' package name as the Logger name and the specified prefix. The new MailLogger uses the same debug flag and PrintStream as this MailLogger.
        Parameters:
        clazz - the Logger name is the package name of this class
        prefix - the prefix for debug output, or null for none
        Returns:
        a MailLogger for the given name and prefix.
      • getSubLogger

        public MailLogger getSubLogger​(java.lang.String subname,
                                       java.lang.String prefix)
        Create a MailLogger that uses a Logger whose name is composed of this MailLogger's name plus the specified sub-name, separated by a dot. The new MailLogger uses the new prefix for debug output. This is used primarily by the protocol trace code that wants a different prefix (none).
        Parameters:
        subname - the Logger name relative to this Logger name
        prefix - the prefix for debug output, or null for none
        Returns:
        a MailLogger for the given name and prefix.
      • getSubLogger

        public MailLogger getSubLogger​(java.lang.String subname,
                                       java.lang.String prefix,
                                       boolean debug)
        Create a MailLogger that uses a Logger whose name is composed of this MailLogger's name plus the specified sub-name, separated by a dot. The new MailLogger uses the new prefix for debug output. This is used primarily by the protocol trace code that wants a different prefix (none).
        Parameters:
        subname - the Logger name relative to this Logger name
        prefix - the prefix for debug output, or null for none
        debug - the debug flag for the sub-logger
        Returns:
        a MailLogger for the given name and prefix.
      • log

        public void log​(java.util.logging.Level level,
                        java.lang.String msg)
        Log the message at the specified level.
        Parameters:
        level - the log level.
        msg - the message.
      • log

        public void log​(java.util.logging.Level level,
                        java.lang.String msg,
                        java.lang.Object param1)
        Log the message at the specified level.
        Parameters:
        level - the log level.
        msg - the message.
        param1 - the additional parameter.
      • log

        public void log​(java.util.logging.Level level,
                        java.lang.String msg,
                        java.lang.Object... params)
        Log the message at the specified level.
        Parameters:
        level - the log level.
        msg - the message.
        params - the message parameters.
      • logf

        public void logf​(java.util.logging.Level level,
                         java.lang.String msg,
                         java.lang.Object... params)
        Log the message at the specified level using a format string.
        Parameters:
        level - the log level.
        msg - the message format string.
        params - the message parameters.
        Since:
        JavaMail 1.5.4
      • log

        public void log​(java.util.logging.Level level,
                        java.lang.String msg,
                        java.lang.Throwable thrown)
        Log the message at the specified level.
        Parameters:
        level - the log level.
        msg - the message.
        thrown - the throwable to log.
      • config

        public void config​(java.lang.String msg)
        Log a message at the CONFIG level.
        Parameters:
        msg - the message.
      • fine

        public void fine​(java.lang.String msg)
        Log a message at the FINE level.
        Parameters:
        msg - the message.
      • finer

        public void finer​(java.lang.String msg)
        Log a message at the FINER level.
        Parameters:
        msg - the message.
      • finest

        public void finest​(java.lang.String msg)
        Log a message at the FINEST level.
        Parameters:
        msg - the message.
      • isLoggable

        public boolean isLoggable​(java.util.logging.Level level)
        If "debug" is set, or our embedded Logger is loggable at the given level, return true.
        Parameters:
        level - the log level.
        Returns:
        true if loggable.
      • ifDebugOut

        private void ifDebugOut​(java.lang.String msg)
        Common code to conditionally log debug statements.
        Parameters:
        msg - the message to log.
      • debugOut

        private void debugOut​(java.lang.String msg)
        Common formatting for debug output.
        Parameters:
        msg - the message to log.
      • packageOf

        private java.lang.String packageOf​(java.lang.Class<?> clazz)
        Return the package name of the class. Sometimes there will be no Package object for the class, e.g., if the class loader hasn't created one (see Class.getPackage()).
        Parameters:
        clazz - the class source.
        Returns:
        the package name or an empty string.
      • inferCaller

        private java.lang.StackTraceElement inferCaller()
        A disadvantage of not being able to use Logger directly in Jakarta Mail code is that the "source class" information that Logger guesses will always refer to this class instead of our caller. This method duplicates what Logger does to try to find *our* caller, so that Logger doesn't have to do it (and get the wrong answer), and because our caller is what's wanted.
        Returns:
        StackTraceElement that logged the message. Treat as read-only.
      • isLoggerImplFrame

        private boolean isLoggerImplFrame​(java.lang.String cname)
        Frames to ignore as part of the MailLogger to JUL bridge.
        Parameters:
        cname - the class name.
        Returns:
        true if the class name is part of the MailLogger bridge.