Class Logger

  • All Implemented Interfaces:
    Serializable, Logger, ExtendedLogger, LocationAwareLogger, Supplier<LoggerConfig>
    Direct Known Subclasses:
    AsyncLogger

    public class Logger
    extends AbstractLogger
    implements Supplier<LoggerConfig>
    The core implementation of the Logger interface. Besides providing an implementation of all the Logger methods, this class also provides some convenience methods for Log4j 1.x compatibility as well as access to the Filters and Appenders associated with this Logger. Note that access to these underlying objects is provided primarily for use in unit tests or bridging legacy Log4j 1.x code. Future versions of this class may or may not include the various methods that are noted as not being part of the public API. TODO All the isEnabled methods could be pushed into a filter interface. Not sure of the utility of having isEnabled be able to examine the message pattern and parameters. (RG) Moving the isEnabled methods out of Logger noticeably impacts performance. The message pattern and parameters are required so that they can be used in global filters.
    See Also:
    Serialized Form
    • Field Detail

      • privateConfig

        protected volatile Logger.PrivateConfig privateConfig
        Config should be consistent across threads.
    • Constructor Detail

      • Logger

        protected Logger​(LoggerContext context,
                         String name,
                         MessageFactory messageFactory)
        The constructor.
        Parameters:
        context - The LoggerContext this Logger is associated with.
        messageFactory - The message factory.
        name - The name of the Logger.
    • Method Detail

      • getParent

        public Logger getParent()
        This method is only used for 1.x compatibility. Returns the parent of this Logger. If it doesn't already exist return a temporary Logger.
        Returns:
        The parent Logger.
      • getContext

        public LoggerContext getContext()
        Returns the LoggerContext this Logger is associated with.
        Returns:
        the LoggerContext.
      • setLevel

        public void setLevel​(Level level)
        This method is not exposed through the public API and is provided primarily for unit testing.

        If the new level is null, this logger inherits the level from its parent.

        Parameters:
        level - The Level to use on this Logger, may be null.
      • logMessage

        public void logMessage​(String fqcn,
                               Level level,
                               Marker marker,
                               Message message,
                               Throwable t)
        Description copied from interface: ExtendedLogger
        Always logs a message at the specified level. It is the responsibility of the caller to ensure the specified level is enabled.
        Specified by:
        logMessage in interface ExtendedLogger
        Parameters:
        fqcn - The fully qualified class name of the logger entry point, used to determine the caller class and method when location information needs to be logged.
        level - The logging Level to check.
        marker - A Marker or null.
        message - The Message.
        t - the exception to log, including its stack trace.
      • isEnabled

        public boolean isEnabled​(Level level,
                                 Marker marker,
                                 String message,
                                 Throwable t)
        Description copied from interface: ExtendedLogger
        Determines if logging is enabled.
        Specified by:
        isEnabled in interface ExtendedLogger
        Parameters:
        level - The logging Level to check.
        marker - A Marker or null.
        message - The message.
        t - the exception to log, including its stack trace.
        Returns:
        True if logging is enabled, false otherwise.
      • isEnabled

        public boolean isEnabled​(Level level,
                                 Marker marker,
                                 String message)
        Description copied from interface: ExtendedLogger
        Determine if logging is enabled.
        Specified by:
        isEnabled in interface ExtendedLogger
        Parameters:
        level - The logging Level to check.
        marker - A Marker or null.
        message - The message.
        Returns:
        True if logging is enabled, false otherwise.
      • isEnabled

        public boolean isEnabled​(Level level,
                                 Marker marker,
                                 String message,
                                 Object... params)
        Description copied from interface: ExtendedLogger
        Determines if logging is enabled.
        Specified by:
        isEnabled in interface ExtendedLogger
        Parameters:
        level - The logging Level to check.
        marker - A Marker or null.
        message - The message.
        params - The parameters.
        Returns:
        True if logging is enabled, false otherwise.
      • isEnabled

        public boolean isEnabled​(Level level,
                                 Marker marker,
                                 String message,
                                 Object p0)
        Description copied from interface: ExtendedLogger
        Determines if logging is enabled.
        Specified by:
        isEnabled in interface ExtendedLogger
        Parameters:
        level - The logging Level to check.
        marker - A Marker or null.
        message - The message.
        p0 - the message parameters
        Returns:
        True if logging is enabled, false otherwise.
      • isEnabled

        public boolean isEnabled​(Level level,
                                 Marker marker,
                                 String message,
                                 Object p0,
                                 Object p1)
        Description copied from interface: ExtendedLogger
        Determines if logging is enabled.
        Specified by:
        isEnabled in interface ExtendedLogger
        Parameters:
        level - The logging Level to check.
        marker - A Marker or null.
        message - The message.
        p0 - the message parameters
        p1 - the message parameters
        Returns:
        True if logging is enabled, false otherwise.
      • isEnabled

        public boolean isEnabled​(Level level,
                                 Marker marker,
                                 String message,
                                 Object p0,
                                 Object p1,
                                 Object p2)
        Description copied from interface: ExtendedLogger
        Determines if logging is enabled.
        Specified by:
        isEnabled in interface ExtendedLogger
        Parameters:
        level - The logging Level to check.
        marker - A Marker or null.
        message - The message.
        p0 - the message parameters
        p1 - the message parameters
        p2 - the message parameters
        Returns:
        True if logging is enabled, false otherwise.
      • isEnabled

        public boolean isEnabled​(Level level,
                                 Marker marker,
                                 String message,
                                 Object p0,
                                 Object p1,
                                 Object p2,
                                 Object p3)
        Description copied from interface: ExtendedLogger
        Determines if logging is enabled.
        Specified by:
        isEnabled in interface ExtendedLogger
        Parameters:
        level - The logging Level to check.
        marker - A Marker or null.
        message - The message.
        p0 - the message parameters
        p1 - the message parameters
        p2 - the message parameters
        p3 - the message parameters
        Returns:
        True if logging is enabled, false otherwise.
      • isEnabled

        public boolean isEnabled​(Level level,
                                 Marker marker,
                                 String message,
                                 Object p0,
                                 Object p1,
                                 Object p2,
                                 Object p3,
                                 Object p4)
        Description copied from interface: ExtendedLogger
        Determines if logging is enabled.
        Specified by:
        isEnabled in interface ExtendedLogger
        Parameters:
        level - The logging Level to check.
        marker - A Marker or null.
        message - The message.
        p0 - the message parameters
        p1 - the message parameters
        p2 - the message parameters
        p3 - the message parameters
        p4 - the message parameters
        Returns:
        True if logging is enabled, false otherwise.
      • isEnabled

        public boolean isEnabled​(Level level,
                                 Marker marker,
                                 String message,
                                 Object p0,
                                 Object p1,
                                 Object p2,
                                 Object p3,
                                 Object p4,
                                 Object p5)
        Description copied from interface: ExtendedLogger
        Determines if logging is enabled.
        Specified by:
        isEnabled in interface ExtendedLogger
        Parameters:
        level - The logging Level to check.
        marker - A Marker or null.
        message - The message.
        p0 - the message parameters
        p1 - the message parameters
        p2 - the message parameters
        p3 - the message parameters
        p4 - the message parameters
        p5 - the message parameters
        Returns:
        True if logging is enabled, false otherwise.
      • isEnabled

        public boolean isEnabled​(Level level,
                                 Marker marker,
                                 String message,
                                 Object p0,
                                 Object p1,
                                 Object p2,
                                 Object p3,
                                 Object p4,
                                 Object p5,
                                 Object p6)
        Description copied from interface: ExtendedLogger
        Determines if logging is enabled.
        Specified by:
        isEnabled in interface ExtendedLogger
        Parameters:
        level - The logging Level to check.
        marker - A Marker or null.
        message - The message.
        p0 - the message parameters
        p1 - the message parameters
        p2 - the message parameters
        p3 - the message parameters
        p4 - the message parameters
        p5 - the message parameters
        p6 - the message parameters
        Returns:
        True if logging is enabled, false otherwise.
      • isEnabled

        public boolean isEnabled​(Level level,
                                 Marker marker,
                                 String message,
                                 Object p0,
                                 Object p1,
                                 Object p2,
                                 Object p3,
                                 Object p4,
                                 Object p5,
                                 Object p6,
                                 Object p7)
        Description copied from interface: ExtendedLogger
        Determines if logging is enabled.
        Specified by:
        isEnabled in interface ExtendedLogger
        Parameters:
        level - The logging Level to check.
        marker - A Marker or null.
        message - The message.
        p0 - the message parameters
        p1 - the message parameters
        p2 - the message parameters
        p3 - the message parameters
        p4 - the message parameters
        p5 - the message parameters
        p6 - the message parameters
        p7 - the message parameters
        Returns:
        True if logging is enabled, false otherwise.
      • isEnabled

        public boolean isEnabled​(Level level,
                                 Marker marker,
                                 String message,
                                 Object p0,
                                 Object p1,
                                 Object p2,
                                 Object p3,
                                 Object p4,
                                 Object p5,
                                 Object p6,
                                 Object p7,
                                 Object p8)
        Description copied from interface: ExtendedLogger
        Determines if logging is enabled.
        Specified by:
        isEnabled in interface ExtendedLogger
        Parameters:
        level - The logging Level to check.
        marker - A Marker or null.
        message - The message.
        p0 - the message parameters
        p1 - the message parameters
        p2 - the message parameters
        p3 - the message parameters
        p4 - the message parameters
        p5 - the message parameters
        p6 - the message parameters
        p7 - the message parameters
        p8 - the message parameters
        Returns:
        True if logging is enabled, false otherwise.
      • isEnabled

        public boolean isEnabled​(Level level,
                                 Marker marker,
                                 String message,
                                 Object p0,
                                 Object p1,
                                 Object p2,
                                 Object p3,
                                 Object p4,
                                 Object p5,
                                 Object p6,
                                 Object p7,
                                 Object p8,
                                 Object p9)
        Description copied from interface: ExtendedLogger
        Determines if logging is enabled.
        Specified by:
        isEnabled in interface ExtendedLogger
        Parameters:
        level - The logging Level to check.
        marker - A Marker or null.
        message - The message.
        p0 - the message parameters
        p1 - the message parameters
        p2 - the message parameters
        p3 - the message parameters
        p4 - the message parameters
        p5 - the message parameters
        p6 - the message parameters
        p7 - the message parameters
        p8 - the message parameters
        p9 - the message parameters
        Returns:
        True if logging is enabled, false otherwise.
      • isEnabled

        public boolean isEnabled​(Level level,
                                 Marker marker,
                                 CharSequence message,
                                 Throwable t)
        Description copied from interface: ExtendedLogger
        Determines if logging is enabled.
        Specified by:
        isEnabled in interface ExtendedLogger
        Parameters:
        level - The logging Level to check.
        marker - A Marker or null.
        message - The message.
        t - A Throwable.
        Returns:
        True if logging is enabled, false otherwise.
      • isEnabled

        public boolean isEnabled​(Level level,
                                 Marker marker,
                                 Object message,
                                 Throwable t)
        Description copied from interface: ExtendedLogger
        Determines if logging is enabled.
        Specified by:
        isEnabled in interface ExtendedLogger
        Parameters:
        level - The logging Level to check.
        marker - A Marker or null.
        message - The message.
        t - A Throwable.
        Returns:
        True if logging is enabled, false otherwise.
      • isEnabled

        public boolean isEnabled​(Level level,
                                 Marker marker,
                                 Message message,
                                 Throwable t)
        Description copied from interface: ExtendedLogger
        Determines if logging is enabled.
        Specified by:
        isEnabled in interface ExtendedLogger
        Parameters:
        level - The logging Level to check.
        marker - A Marker or null.
        message - The Message.
        t - A Throwable.
        Returns:
        True if logging is enabled, false otherwise.
      • addAppender

        public void addAppender​(Appender appender)
        This method is not exposed through the public API and is used primarily for unit testing.
        Parameters:
        appender - The Appender to add to the Logger.
      • removeAppender

        public void removeAppender​(Appender appender)
        This method is not exposed through the public API and is used primarily for unit testing.
        Parameters:
        appender - The Appender to remove from the Logger.
      • getAppenders

        public Map<String,​Appender> getAppenders()
        This method is not exposed through the public API and is used primarily for unit testing.
        Returns:
        A Map containing the Appender's name as the key and the Appender as the value.
      • getFilters

        public Iterator<Filter> getFilters()
        This method is not exposed through the public API and is used primarily for unit testing.
        Returns:
        An Iterator over all the Filters associated with the Logger.
      • getLevel

        public Level getLevel()
        Gets the Level associated with the Logger.
        Specified by:
        getLevel in interface Logger
        Returns:
        the Level associate with the Logger.
      • filterCount

        public int filterCount()
        This method is not exposed through the public API and is used primarily for unit testing.
        Returns:
        The number of Filters associated with the Logger.
      • addFilter

        public void addFilter​(Filter filter)
        This method is not exposed through the public API and is used primarily for unit testing.
        Parameters:
        filter - The Filter to add.
      • isAdditive

        public boolean isAdditive()
        This method is not exposed through the public API and is present only to support the Log4j 1.2 compatibility bridge.
        Returns:
        true if the associated LoggerConfig is additive, false otherwise.
      • setAdditive

        public void setAdditive​(boolean additive)
        This method is not exposed through the public API and is present only to support the Log4j 1.2 compatibility bridge.
        Parameters:
        additive - Boolean value to indicate whether the Logger is additive or not.
      • updateConfiguration

        protected void updateConfiguration​(Configuration newConfig)
        Associates this Logger with a new Configuration. This method is not exposed through the public API.

        There are two ways this could be used to guarantee all threads are aware of changes to config.

        1. Synchronize this method. Accessors don't need to be synchronized as Java will treat all variables within a synchronized block as volatile.
        2. Declare the variable volatile. Option 2 is used here as the performance cost is very low and it does a better job at documenting how it is used.
        Parameters:
        newConfig - The new Configuration.
      • toString

        public String toString()
        Returns a String representation of this instance in the form "name:level[ in context_name]".
        Overrides:
        toString in class Object
        Returns:
        A String describing this Logger instance.
      • hashCode

        public int hashCode()
        Overrides:
        hashCode in class Object