Package rife.bld.extension.testing
Class TestLogHandler
java.lang.Object
java.util.logging.Handler
rife.bld.extension.testing.TestLogHandler
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 Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionvoid
clear()
Clears all captured log records and messages.void
close()
Closes this log handler and prevents further logging.boolean
containsExactMessage
(String message) Checks if the log contains the exact message.boolean
containsMessage
(String message) Checks if the log contains a message containing the given text.boolean
containsMessageMatching
(Pattern pattern) Checks if the log contains a message matching the given regex pattern.long
countMessagesContaining
(String message) Counts the number of messages containing the given text.long
countRecordsAtLevel
(Level level) Counts the number of log records at the specified level.void
flush()
Flushes this log handler.getFirstRecordContaining
(String message) Gets the first log record containing the given text.Gets the most recent log record, if any.getLastRecordContaining
(String message) Gets the last log record containing the given text.Gets all captured log messages as strings.Gets all captured log records.int
Gets the total number of captured log records.getRecordsAtOrAboveLevel
(Level level) Gets all log records at or above the specified level.boolean
hasLogLevel
(Level level) Checks if the log contains a record with the given level.boolean
isClosed()
Checks if this handler has been closed.boolean
isEmpty()
Checks if any log records have been captured.void
Publishes a log record if the handler is not closed.Methods inherited from class java.util.logging.Handler
getEncoding, getErrorManager, getFilter, getFormatter, getLevel, isLoggable, reportError, setEncoding, setErrorManager, setFilter, setFormatter, setLevel
-
Constructor Details
-
TestLogHandler
public TestLogHandler()
-
-
Method Details
-
clear
public void clear()Clears all captured log records and messages.Thread-safe operation.
-
containsExactMessage
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
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
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
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
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
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
Gets the most recent log record, if any.- Returns:
- the most recent log record, or
null
if no records exist
-
getLastRecordContaining
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
Gets all captured log messages as strings.Returns an immutable snapshot of current messages.
- Returns:
- immutable list of log messages
-
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
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
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
Publishes a log record if the handler is not closed. -
flush
public void flush()Flushes this log handler.No-op implementation as records are immediately available.
-
close
public void close()Closes this log handler and prevents further logging.Thread-safe operation.
-