Class Log4J2Logger

java.lang.Object
org.tentackle.log.log4j2v.Log4J2Logger
All Implemented Interfaces:
Logger

@Service(Logger.class) public class Log4J2Logger extends Object implements Logger
Tentackle Logger backed by Log4J 2 (org.apache.logging.log4j).

Registered as @Service(Logger.class), so it is picked up by the DefaultLoggerFactory as soon as this module is on the class- or module path.
Messages are formatted with MessageFormat (not Log4J's {} placeholders) and logged on behalf of the caller, i.e. Log4J reports the caller's location, not this wrapper's.

Author:
harald
  • Constructor Details

    • Log4J2Logger

      public Log4J2Logger(String name)
      Creates a logger.
      Applications should use getLogger(String) instead, which reuses existing instances.
      Parameters:
      name - the name of the logger
  • Method Details

    • getLogger

      public static Log4J2Logger getLogger(String name)
      Gets the Log4J2Logger for the given name.
      If a logger with that name already exists, it will be reused.

      This is the method DefaultLoggerFactory prefers over the constructor. The cache is thread-safe and holds the loggers for the lifetime of the JVM.

      Parameters:
      name - the name of the logger
      Returns:
      the logger
    • getLoggerImpl

      public Object getLoggerImpl()
      Gets the concrete logger implementation.
      Useful to access the logging-backend.

      Returns the ExtendedLoggerWrapper around the Log4J logger, not the org.apache.logging.log4j.Logger obtained from the LogManager.

      Specified by:
      getLoggerImpl in interface Logger
      Returns:
      the logger object
    • isLoggable

      public boolean isLoggable(Logger.Level level)
      Description copied from interface: Logger
      Returns whether this logger will log the given level.
      Specified by:
      isLoggable in interface Logger
      Parameters:
      level - the logging level
      Returns:
      true if the given logging level is currently being logged
    • log

      public void log(Logger.Level level, String message, Throwable cause)
      Description copied from interface: Logger
      Logs a message.
      Specified by:
      log in interface Logger
      Parameters:
      level - the logging level
      message - the message
      cause - the throwable
    • log

      public void log(Logger.Level level, Throwable cause, Supplier<String> messageSupplier)
      Description copied from interface: Logger
      Logs a message.
      Specified by:
      log in interface Logger
      Parameters:
      level - the logging level
      cause - the throwable, null for none
      messageSupplier - the message supplier
    • log

      public void log(Logger.Level level, String message, Object... params)
      Description copied from interface: Logger
      Logs a message.
      Specified by:
      log in interface Logger
      Parameters:
      level - the logging level
      message - the message
      params - message parameters
    • finer

      public void finer(String message)
      Description copied from interface: Logger
      Logs a message with level FINER.
      Specified by:
      finer in interface Logger
      Parameters:
      message - the message
    • fine

      public void fine(String message)
      Description copied from interface: Logger
      Logs a message with level FINE.
      Specified by:
      fine in interface Logger
      Parameters:
      message - the message
    • info

      public void info(String message)
      Description copied from interface: Logger
      Logs a message with level INFO.
      Specified by:
      info in interface Logger
      Parameters:
      message - the message
    • warning

      public void warning(String message)
      Description copied from interface: Logger
      Logs a message with level WARNING.
      Specified by:
      warning in interface Logger
      Parameters:
      message - the message
    • severe

      public void severe(String message)
      Description copied from interface: Logger
      Logs a message with level SEVERE.
      Specified by:
      severe in interface Logger
      Parameters:
      message - the message
    • finer

      public void finer(String message, Object... params)
      Description copied from interface: Logger
      Logs a message with level FINER.
      Specified by:
      finer in interface Logger
      Parameters:
      message - the message
      params - message parameters
    • fine

      public void fine(String message, Object... params)
      Description copied from interface: Logger
      Logs a message with level FINE.
      Specified by:
      fine in interface Logger
      Parameters:
      message - the message
      params - message parameters
    • info

      public void info(String message, Object... params)
      Description copied from interface: Logger
      Logs a message with level INFO.
      Specified by:
      info in interface Logger
      Parameters:
      message - the message
      params - message parameters
    • warning

      public void warning(String message, Object... params)
      Description copied from interface: Logger
      Logs a message with level WARNING.
      Specified by:
      warning in interface Logger
      Parameters:
      message - the message
      params - message parameters
    • severe

      public void severe(String message, Object... params)
      Description copied from interface: Logger
      Logs a message with level SEVERE.
      Specified by:
      severe in interface Logger
      Parameters:
      message - the message
      params - message parameters
    • finer

      public void finer(String message, Supplier<?>... paramSuppliers)
      Description copied from interface: Logger
      Logs a message with level FINER.
      Specified by:
      finer in interface Logger
      Parameters:
      message - the message
      paramSuppliers - message parameter suppliers
    • fine

      public void fine(String message, Supplier<?>... paramSuppliers)
      Description copied from interface: Logger
      Logs a message with level FINE.
      Specified by:
      fine in interface Logger
      Parameters:
      message - the message
      paramSuppliers - message parameter suppliers
    • info

      public void info(String message, Supplier<?>... paramSuppliers)
      Description copied from interface: Logger
      Logs a message with level INFO.
      Specified by:
      info in interface Logger
      Parameters:
      message - the message
      paramSuppliers - message parameter suppliers
    • warning

      public void warning(String message, Supplier<?>... paramSuppliers)
      Description copied from interface: Logger
      Logs a message with level WARNING.
      Specified by:
      warning in interface Logger
      Parameters:
      message - the message
      paramSuppliers - message parameter suppliers
    • severe

      public void severe(String message, Supplier<?>... paramSuppliers)
      Description copied from interface: Logger
      Logs a message with level SEVERE.
      Specified by:
      severe in interface Logger
      Parameters:
      message - the message
      paramSuppliers - message parameter suppliers
    • finer

      public void finer(String message, Throwable cause)
      Description copied from interface: Logger
      Logs a message with level FINER.
      Specified by:
      finer in interface Logger
      Parameters:
      message - the message
      cause - the throwable
    • fine

      public void fine(String message, Throwable cause)
      Description copied from interface: Logger
      Logs a message with level FINE.
      Specified by:
      fine in interface Logger
      Parameters:
      message - the message
      cause - the throwable
    • info

      public void info(String message, Throwable cause)
      Description copied from interface: Logger
      Logs a message with level INFO.
      Specified by:
      info in interface Logger
      Parameters:
      message - the message
      cause - the throwable
    • warning

      public void warning(String message, Throwable cause)
      Description copied from interface: Logger
      Logs a message with level WARNING.
      Specified by:
      warning in interface Logger
      Parameters:
      message - the message
      cause - the throwable
    • severe

      public void severe(String message, Throwable cause)
      Description copied from interface: Logger
      Logs a message with level SEVERE.
      Specified by:
      severe in interface Logger
      Parameters:
      message - the message
      cause - the throwable
    • finer

      public void finer(Throwable cause, Supplier<String> messageSupplier)
      Description copied from interface: Logger
      Logs a message with level FINER.
      Specified by:
      finer in interface Logger
      Parameters:
      cause - the throwable
      messageSupplier - the message supplier
    • fine

      public void fine(Throwable cause, Supplier<String> messageSupplier)
      Description copied from interface: Logger
      Logs a message with level FINE.
      Specified by:
      fine in interface Logger
      Parameters:
      cause - the throwable
      messageSupplier - the message supplier
    • info

      public void info(Throwable cause, Supplier<String> messageSupplier)
      Description copied from interface: Logger
      Logs a message with level INFO.
      Specified by:
      info in interface Logger
      Parameters:
      cause - the throwable
      messageSupplier - the message supplier
    • warning

      public void warning(Throwable cause, Supplier<String> messageSupplier)
      Description copied from interface: Logger
      Logs a message with level WARNING.
      Specified by:
      warning in interface Logger
      Parameters:
      cause - the throwable
      messageSupplier - the message supplier
    • severe

      public void severe(Throwable cause, Supplier<String> messageSupplier)
      Description copied from interface: Logger
      Logs a message with level SEVERE.
      Specified by:
      severe in interface Logger
      Parameters:
      cause - the throwable
      messageSupplier - the message supplier
    • finer

      public void finer(Supplier<String> messageSupplier)
      Description copied from interface: Logger
      Logs a message with level FINER.
      Specified by:
      finer in interface Logger
      Parameters:
      messageSupplier - the message supplier
    • fine

      public void fine(Supplier<String> messageSupplier)
      Description copied from interface: Logger
      Logs a message with level FINE.
      Specified by:
      fine in interface Logger
      Parameters:
      messageSupplier - the message supplier
    • info

      public void info(Supplier<String> messageSupplier)
      Description copied from interface: Logger
      Logs a message with level INFO.
      Specified by:
      info in interface Logger
      Parameters:
      messageSupplier - the message supplier
    • warning

      public void warning(Supplier<String> messageSupplier)
      Description copied from interface: Logger
      Logs a message with level WARNING.
      Specified by:
      warning in interface Logger
      Parameters:
      messageSupplier - the message supplier
    • severe

      public void severe(Supplier<String> messageSupplier)
      Description copied from interface: Logger
      Logs a message with level SEVERE.
      Specified by:
      severe in interface Logger
      Parameters:
      messageSupplier - the message supplier
    • isFinerLoggable

      public boolean isFinerLoggable()
      Description copied from interface: Logger
      Checks if logger logs level FINER.
      Specified by:
      isFinerLoggable in interface Logger
      Returns:
      true if logger will log this level
    • isFineLoggable

      public boolean isFineLoggable()
      Description copied from interface: Logger
      Checks if logger logs level FINE.
      Specified by:
      isFineLoggable in interface Logger
      Returns:
      true if logger will log this level
    • isInfoLoggable

      public boolean isInfoLoggable()
      Description copied from interface: Logger
      Checks if logger logs level INFO.
      Specified by:
      isInfoLoggable in interface Logger
      Returns:
      true if logger will log this level
    • isWarningLoggable

      public boolean isWarningLoggable()
      Description copied from interface: Logger
      Checks if logger logs level WARNING.
      Specified by:
      isWarningLoggable in interface Logger
      Returns:
      true if logger will log this level
    • isSevereLoggable

      public boolean isSevereLoggable()
      Description copied from interface: Logger
      Checks if logger logs level SEVERE.
      Specified by:
      isSevereLoggable in interface Logger
      Returns:
      true if logger will log this level
    • logStacktrace

      public void logStacktrace(Logger.Level level, Throwable cause)
      Logs the stacktrace of a throwable.
      The stacktrace is logged line by line via a LoggerOutputStream.
      Specified by:
      logStacktrace in interface Logger
      Parameters:
      level - the logging level
      cause - the Throwable to log the stacktrace for
    • logStacktrace

      public void logStacktrace(Throwable cause)
      Logs the stacktrace of a throwable with a logging level of SEVERE.
      Specified by:
      logStacktrace in interface Logger
      Parameters:
      cause - the Throwable to log the stacktrace for
    • getMappedDiagnosticContext

      public MappedDiagnosticContext getMappedDiagnosticContext()
      Description copied from interface: Logger
      Gets the mapped diagnostic context (MDC).
      Usually a singleton (depending on the backend).
      Specified by:
      getMappedDiagnosticContext in interface Logger
      Returns:
      the MDC
    • doLog

      protected void doLog(Logger.Level level, String message, Throwable cause, Object... params)
      Logs a message with optional parameters.

      If there are parameters, the message is formatted with MessageFormat. Otherwise, it is logged verbatim, i.e. quotes are not unescaped and placeholders stay as they are.

      Parameters:
      level - the log level
      message - the message
      cause - the cause, null if none
      params - optional parameters
    • doLog

      protected void doLog(Logger.Level level, Throwable cause, String message, Supplier<?>... paramSuppliers)
      Logs a message with lazily evaluated parameters.

      The suppliers are evaluated only if the level is enabled. The message is always formatted with MessageFormat, even if there are no suppliers.

      Parameters:
      level - the log level
      cause - the cause, null if none
      message - the message
      paramSuppliers - parameter suppliers, null elements yield null parameters
    • doLog

      protected void doLog(Logger.Level level, Throwable cause, String message, Supplier<String> messageSupplier)
      Logs a message or the result of a message supplier.
      All other logging methods end up here.

      The message is created only if the level is enabled. The caller's location is determined by Log4J relative to this class.

      Parameters:
      level - the log level
      cause - the cause, null if none
      message - the message, ignored if messageSupplier is not null
      messageSupplier - the message supplier, null to log message