Class Log4j2Handler

java.lang.Object
io.github.merlimat.slog.handler.Log4j2Handler
All Implemented Interfaces:
Handler

public class Log4j2Handler extends Object implements Handler
Handler implementation that delegates to Log4j2.

Structured attributes are placed into the Log4j2 ThreadContext for the duration of each log call. When using JsonLayout with properties="true", each attribute appears as an individual field inside the contextMap JSON object.

This handler is selected automatically when org.apache.logging.log4j.LogManager is present on the classpath.

  • Constructor Details

    • Log4j2Handler

      public Log4j2Handler()
      Creates a new Log4j2 handler.
  • Method Details

    • isEnabled

      public boolean isEnabled(String loggerName, Level level)
      Description copied from interface: Handler
      Checks whether logging is enabled for the given logger name and level.

      This is called before any event construction, ensuring zero overhead when the level is disabled.

      Specified by:
      isEnabled in interface Handler
      Parameters:
      loggerName - the logger name (typically a fully-qualified class name)
      level - the log level to check
      Returns:
      true if a log record at this level would be emitted
    • handle

      public void handle(LogRecord record)
      Description copied from interface: Handler
      Processes a structured log record, emitting it to the underlying logging framework.
      Specified by:
      handle in interface Handler
      Parameters:
      record - the log record to emit