Class InMemoryLoggingHandler

java.lang.Object
java.util.logging.Handler
io.helidon.common.testing.junit5.InMemoryLoggingHandler
All Implemented Interfaces:
AutoCloseable

public class InMemoryLoggingHandler extends Handler implements AutoCloseable
Captures log records in memory for later retrieval.

The handler is intended to be attached to a single logger. Callers can use the static factory methods to create an in-memory handler and attach it to the specified logger. The handler is auto-closable so a properly-constructed test--using try-with-resource--will automatically clear the handler's log records and detach the handler from the logger.

  • Method Details

    • create

      public static InMemoryLoggingHandler create(Logger logger)
      Creates a new in-memory logging handler and attaches it to the specified logger.
      Parameters:
      logger - the Logger to which to add a new in-memory handler
      Returns:
      the new handler
    • create

      public static InMemoryLoggingHandler create(Object objectWithNamedLogger)
      Creates a new in-memory logging handler and attaches it to the logger named after the class name of the specified object.
      Parameters:
      objectWithNamedLogger - the object whose class-name-based logger the handler should be added to
      Returns:
      the new handler
    • publish

      public void publish(LogRecord record)
      Specified by:
      publish in class Handler
    • flush

      public void flush()
      Specified by:
      flush in class Handler
    • close

      public void close() throws SecurityException
      Specified by:
      close in interface AutoCloseable
      Specified by:
      close in class Handler
      Throws:
      SecurityException
    • logRecords

      public List<LogRecord> logRecords()
      Returns the accumulated log records.
      Returns:
      list of log records