Class LogRecordCapture

java.lang.Object
org.apache.juneau.commons.logging.LogRecordCapture
All Implemented Interfaces:
Closeable, AutoCloseable, LogRecordListener

public class LogRecordCapture extends Object implements LogRecordListener, Closeable
Captures log records for testing purposes.

This class implements LogRecordListener to receive log records and stores them in memory for inspection during tests. It implements Closeable to automatically remove itself from the logger's listeners when closed.

Usage:

// Capture records using try-with-resources try (LogRecordCapture capture = RichLogger.getLogger(MyClass.class).captureEvents()) { logger.info("Test message"); logger.warning("Warning message"); // Inspect captured records List<LogRecord> records = capture.getRecords(); assertEquals(2, records.size()); // Get formatted messages List<String> messages = capture.getRecords("{level}: {msg}"); assertEquals("INFO: Test message", messages.get(0)); }

Format String:

The format string supports placeholders as documented in LogRecord.formatted(String).

See Also:
  • Method Details

    • quietly

      public static List<LogRecord> quietly(String loggerName, Snippet body)
      Runs a block while capturing, and keeping off the console, everything logged to the named logger or any of its descendants.

      Unlike RichLogger.captureEvents(), which only observes records, this also stops the logger's records from reaching the parent (console) handlers for the duration of the block. The logger's previous parent-handler setting is restored in a finally block.

      List<LogRecord> records = LogRecordCapture.quietly(MyClass.class, () -> obj.doSomethingThatWarns()); assertEquals(1, records.size());

      Parameters:
      loggerName - The logger name. Records from descendant loggers are captured too.
      body - The code to run. Checked exceptions are rethrown wrapped in a RuntimeException.
      Returns:
      The captured records, in the order logged. Never null.
    • quiet

      public static LogRecordCapture.Quiet quiet(String loggerName)
      Keeps everything logged to the named logger, and its descendants, off the console until the returned handle is closed.

      Use this when a whole test class (for example from @BeforeAll / @AfterAll) expects a warning it is not asserting on. Calls nest and may overlap across threads: the logger's original parent-handler setting is restored when the last outstanding handle for that logger name is closed.

      Parameters:
      loggerName - The logger name.
      Returns:
      A handle that restores the logger when closed. Never null.
    • quiet

      public static LogRecordCapture.Quiet quiet(Class<?> loggerClass)
      Same as quiet(String), keyed by the logger's class name.
      Parameters:
      loggerClass - The class whose logger is silenced.
      Returns:
      A handle that restores the logger when closed. Never null.
    • quietly

      public static List<LogRecord> quietly(Class<?> loggerClass, Snippet body)
      Same as quietly(String, Snippet), keyed by the logger's class name.
      Parameters:
      loggerClass - The class whose logger is captured.
      body - The code to run.
      Returns:
      The captured records, in the order logged. Never null.
    • onLogRecord

      public void onLogRecord(LogRecord rec)
      Called when a log record is logged.
      Specified by:
      onLogRecord in interface LogRecordListener
      Parameters:
      rec - The log record that was logged.
    • getRecords

      Returns an unmodifiable list of all captured log records.
      Returns:
      An unmodifiable list of captured LogRecords.
    • getRecords

      public List<String> getRecords(String format)
      Returns captured log records formatted as strings.

      The format string supports placeholders as documented in LogRecord.formatted(String).

      Parameters:
      format - The format string with placeholders.
      Returns:
      A list of formatted record strings.
    • clear

      public void clear()
      Clears all captured records.
    • size

      public int size()
      Returns the number of captured records.
      Returns:
      The number of captured records.
    • isEmpty

      public boolean isEmpty()
      Returns true if any records have been captured.
      Returns:
      true if records have been captured.
    • messages

      public List<String> messages()
    • last

      public LogRecord last()
    • byLevel

      public List<LogRecord> byLevel(Level level)
    • matching

      public List<LogRecord> matching(String regex)
    • contextValue

      public Object contextValue(LogRecord rec, String key)
      Returns the value of a LogContext entry attached to the specified captured record.

      Convenience over LogRecordContext.of(java.util.logging.LogRecord) so capture-based tests do not need to know about the side table directly.

      Parameters:
      rec - The captured record.
      key - The context key.
      Returns:
      The attached value for the key, or null if not present.
    • assertMessage

    • assertLevel

      public Level assertLevel()
    • assertThrown

    • close

      public void close()
      Closes this capture and removes it from the logger's listeners.
      Specified by:
      close in interface AutoCloseable
      Specified by:
      close in interface Closeable