Class TestLogHandler

java.lang.Object
java.util.logging.Handler
rife.bld.extension.testing.TestLogHandler

public class TestLogHandler extends Handler
Thread-safe custom log handler for capturing log messages during tests.

Usage Examples:

 // Using the LoggingExtension
 private static final Logger LOGGER = Logger.getLogger(MyClass.class.getName());
 private static final TestLogHandler TEST_LOG_HANDLER = new TestLogHandler();

 @RegisterExtension
 @SuppressWarnings("unused")
 private static final LoggingExtension LOGGING_EXTENSION = new LoggingExtension(
     LOGGER,
     TEST_LOG_HANDLER,
     Level.ALL
 );

 // Manually, in a test method
 @Test
 void testMethod() {
     var logger = Logger.getLogger(MyClass.class.getName());
     var logHandler = new TestLogHandler();

     logger.addHandler(logHandler);
     logger.setLevel(Level.ALL);

     // ...

     logger.removeHandler(logHandler);
 }
Since:
1.0
Author:
Erik C. Thauvin
See Also:
  • Constructor Details

    • TestLogHandler

      public TestLogHandler()
  • Method Details

    • clear

      public void clear()
      Clears all captured log records and messages.

      Thread-safe operation.

    • containsExactMessage

      public boolean containsExactMessage(String message)
      Checks if the log contains the exact message.
      Parameters:
      message - the message to check for
      Returns:
      true if the log contains the message, false otherwise
    • containsMessage

      public boolean containsMessage(String message)
      Checks if the log contains a message containing the given text.
      Parameters:
      message - the text to check for
      Returns:
      true if the log contains a message with the text, false otherwise
    • containsMessageMatching

      public boolean containsMessageMatching(Pattern pattern)
      Checks if the log contains a message matching the given regex pattern.
      Parameters:
      pattern - the regex pattern to match against
      Returns:
      true if any message matches the pattern, false otherwise
    • countMessagesContaining

      public long countMessagesContaining(String message)
      Counts the number of messages containing the given text.
      Parameters:
      message - the text to check for
      Returns:
      the number of messages containing the given text
    • countRecordsAtLevel

      public long countRecordsAtLevel(Level level)
      Counts the number of log records at the specified level.
      Parameters:
      level - the log level to count
      Returns:
      the number of records at the specified level
    • getFirstRecordContaining

      public LogRecord getFirstRecordContaining(String message)
      Gets the first log record containing the given text.
      Parameters:
      message - the text to check for
      Returns:
      the first log record containing the given text, or null if not found
    • getLastRecord

      public LogRecord getLastRecord()
      Gets the most recent log record, if any.
      Returns:
      the most recent log record, or null if no records exist
    • getLastRecordContaining

      public LogRecord getLastRecordContaining(String message)
      Gets the last log record containing the given text.
      Parameters:
      message - the text to check for
      Returns:
      the last log record containing the given text, or null if not found
    • getLogMessages

      public List<String> getLogMessages()
      Gets all captured log messages as strings.

      Returns an immutable snapshot of current messages.

      Returns:
      immutable list of log messages
    • getLogRecords

      public List<LogRecord> getLogRecords()
      Gets all captured log records.

      Returns an immutable snapshot of current records.

      Returns:
      immutable list of log records
    • getRecordCount

      public int getRecordCount()
      Gets the total number of captured log records.
      Returns:
      the number of log records
    • getRecordsAtOrAboveLevel

      public List<LogRecord> getRecordsAtOrAboveLevel(Level level)
      Gets all log records at or above the specified level.
      Parameters:
      level - the minimum log level
      Returns:
      immutable list of log records at or above the specified level
    • hasLogLevel

      public boolean hasLogLevel(Level level)
      Checks if the log contains a record with the given level.
      Parameters:
      level - the level to check for
      Returns:
      true if the log contains a record with the given level, false otherwise
    • isClosed

      public boolean isClosed()
      Checks if this handler has been closed.
      Returns:
      true if the handler is closed, false otherwise
    • isEmpty

      public boolean isEmpty()
      Checks if any log records have been captured.
      Returns:
      true if no records have been captured, false otherwise
    • publish

      public void publish(LogRecord record)
      Publishes a log record if the handler is not closed.
      Specified by:
      publish in class Handler
      Parameters:
      record - description of the log event. A null record is silently ignored and is not published
    • flush

      public void flush()
      Flushes this log handler.

      No-op implementation as records are immediately available.

      Specified by:
      flush in class Handler
    • close

      public void close()
      Closes this log handler and prevents further logging.

      Thread-safe operation.

      Specified by:
      close in class Handler