Class LogRecordCapture
- All Implemented Interfaces:
Closeable,AutoCloseable,LogRecordListener
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:
Format String:
The format string supports placeholders as documented in LogRecord.formatted(String).
See Also:
-
Nested Class Summary
Nested Classes -
Method Summary
Modifier and TypeMethodDescriptionvoidclear()Clears all captured records.voidclose()Closes this capture and removes it from the logger's listeners.contextValue(LogRecord rec, String key) Returns the value of aLogContextentry attached to the specified captured record.Returns an unmodifiable list of all captured log records.getRecords(String format) Returns captured log records formatted as strings.booleanisEmpty()Returnstrue if any records have been captured.last()messages()voidonLogRecord(LogRecord rec) Called when a log record is logged.static LogRecordCapture.QuietSame asquiet(String), keyed by the logger's class name.static LogRecordCapture.QuietKeeps everything logged to the named logger, and its descendants, off the console until the returned handle is closed.Same asquietly(String, Snippet), keyed by the logger's class name.Runs a block while capturing, and keeping off the console, everything logged to the named logger or any of its descendants.intsize()Returns the number of captured records.
-
Method Details
-
quietly
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 afinally 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 aRuntimeException.- Returns:
- The captured records, in the order logged. Never
null .
-
quiet
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
Same asquiet(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
Same asquietly(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
Called when a log record is logged.- Specified by:
onLogRecordin interfaceLogRecordListener- 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
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
Clears all captured records. -
size
Returns the number of captured records.- Returns:
- The number of captured records.
-
isEmpty
Returnstrue if any records have been captured.- Returns:
true if records have been captured.
-
messages
-
last
-
byLevel
-
matching
-
contextValue
Returns the value of aLogContextentry 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
-
assertThrown
-
close
Closes this capture and removes it from the logger's listeners.- Specified by:
closein interfaceAutoCloseable- Specified by:
closein interfaceCloseable
-