Class AbstractLogger

  • All Implemented Interfaces:
    Logger

    public abstract class AbstractLogger
    extends java.lang.Object
    implements Logger
    An abstract implementation of the Logger interface, providing common functionality for concrete logger implementations.

    This class ensures consistent handling of logging operations across different logging frameworks by implementing shared logic such as message formatting and exception handling. Subclasses are expected to implement the basic logging methods (e.g., Logger.trace(String), Logger.debug(String), etc.) according to their specific logging backend.

    Message Formatting

    The class supports formatted logging using a syntax similar to SLF4J or Java's String.format(String, Object...). For example:

    
     logger.info("User {} logged in from {}", username, ip);
     

    Exception Logging

    If the last argument passed to a formatted logging method is an instance of Throwable, it will be treated as an exception, and the appropriate method accepting both a message and a throwable will be invoked:

    
     try {
         // some operation that may throw an exception
     } catch (Exception e) {
         logger.error("Operation failed", e);
     }
     

    This behavior is handled internally by the log(Consumer, BiConsumer, String, Object...) method.

    Since:
    1.0.0
    Author:
    Mercy
    See Also:
    Logger
    • Constructor Summary

      Constructors 
      Modifier Constructor Description
      protected AbstractLogger​(java.lang.String name)  
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      void debug​(java.lang.String format, java.lang.Object... arguments)
      Log a message at the DEBUG level according to the specified format and arguments.
      void error​(java.lang.String format, java.lang.Object... arguments)
      Log a message at the ERROR level according to the specified format and arguments.
      java.lang.String getName()
      Return the name of this Logger instance.
      void info​(java.lang.String format, java.lang.Object... arguments)
      Log a message at the INFO level according to the specified format and arguments.
      protected void log​(java.util.function.Consumer<java.lang.String> messageHandler, java.util.function.BiConsumer<java.lang.String,​java.lang.Throwable> messageThrowableHandler, java.lang.String format, java.lang.Object... arguments)  
      protected java.lang.String resolveMessage​(java.lang.String format, java.lang.Object... arguments)
      Resolve the format message
      void trace​(java.lang.String format, java.lang.Object... arguments)
      Log a message at the TRACE level according to the specified format and arguments.
      void warn​(java.lang.String format, java.lang.Object... arguments)
      Log a message at the WARN level according to the specified format and arguments.
      • Methods inherited from class java.lang.Object

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

      • AbstractLogger

        protected AbstractLogger​(java.lang.String name)
    • Method Detail

      • getName

        public final java.lang.String getName()
        Description copied from interface: Logger
        Return the name of this Logger instance.
        Specified by:
        getName in interface Logger
        Returns:
        name of this logger instance
      • trace

        public void trace​(java.lang.String format,
                          java.lang.Object... arguments)
        Description copied from interface: Logger
        Log a message at the TRACE level according to the specified format and arguments.

        This form avoids superfluous string concatenation when the logger is disabled for the TRACE level. However, this variant incurs the hidden (and relatively small) cost of creating an Object[] before invoking the method, even if this logger is disabled for TRACE.

        Specified by:
        trace in interface Logger
        Parameters:
        format - the format string
        arguments - the arguments
      • debug

        public void debug​(java.lang.String format,
                          java.lang.Object... arguments)
        Description copied from interface: Logger
        Log a message at the DEBUG level according to the specified format and arguments.
        Specified by:
        debug in interface Logger
        Parameters:
        format - the format string
        arguments - the arguments
      • info

        public void info​(java.lang.String format,
                         java.lang.Object... arguments)
        Description copied from interface: Logger
        Log a message at the INFO level according to the specified format and arguments.
        Specified by:
        info in interface Logger
        Parameters:
        format - the format string
        arguments - the arguments
      • warn

        public void warn​(java.lang.String format,
                         java.lang.Object... arguments)
        Description copied from interface: Logger
        Log a message at the WARN level according to the specified format and arguments.
        Specified by:
        warn in interface Logger
        Parameters:
        format - the format string
        arguments - the arguments
      • error

        public void error​(java.lang.String format,
                          java.lang.Object... arguments)
        Description copied from interface: Logger
        Log a message at the ERROR level according to the specified format and arguments.
        Specified by:
        error in interface Logger
        Parameters:
        format - the format string
        arguments - the arguments
      • log

        protected void log​(java.util.function.Consumer<java.lang.String> messageHandler,
                           java.util.function.BiConsumer<java.lang.String,​java.lang.Throwable> messageThrowableHandler,
                           java.lang.String format,
                           java.lang.Object... arguments)
        Parameters:
        messageHandler - only message to log
        messageThrowableHandler - message and Throwable to log
        format - the format message or regular message
        arguments - zero or more arguments for the format pattern
      • resolveMessage

        protected java.lang.String resolveMessage​(java.lang.String format,
                                                  java.lang.Object... arguments)
        Resolve the format message
        Parameters:
        format - the format message
        arguments - zero or more arguments for the format pattern
        Returns:
        non-null