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 TypeMethodDescriptionvoidclear()Clears all captured log records and messages.voidclose()Closes this log handler and prevents further logging.booleancontainsExactMessage(String message) Checks if the log contains the exact message.booleancontainsMessage(String message) Checks if the log contains a message containing the given text.booleancontainsMessageMatching(Pattern pattern) Checks if the log contains a message matching the given regex pattern.longcountMessagesContaining(String message) Counts the number of messages containing the given text.longcountRecordsAtLevel(Level level) Counts the number of log records at the specified level.voidflush()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.intGets the total number of captured log records.getRecordsAtOrAboveLevel(Level level) Gets all log records at or above the specified level.booleanhasLogLevel(Level level) Checks if the log contains a record with the given level.booleanisClosed()Checks if this handler has been closed.booleanisEmpty()Checks if any log records have been captured.voidPrints all captured log messages to standard output.voidPrints all captured log messages to the specified output stream.voidPrints all captured log messages with their log levels to standard output.voidPrints all captured log messages with their log levels to the specified output stream.voidPrints all captured log messages with both level and timestamp to standard output.voidPrints all captured log messages with both level and timestamp to the specified output stream.voidPrints all captured log messages with timestamps to standard output.voidPrints all captured log messages with timestamps to the specified output stream.voidPublishes 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:
trueif the log contains the message,falseotherwise
-
containsMessage
Checks if the log contains a message containing the given text.- Parameters:
message- the text to check for- Returns:
trueif the log contains a message with the text,falseotherwise
-
containsMessageMatching
Checks if the log contains a message matching the given regex pattern.- Parameters:
pattern- the regex pattern to match against- Returns:
trueif any message matches the pattern,falseotherwise
-
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
nullif not found
-
getLastRecord
Gets the most recent log record, if any.- Returns:
- the most recent log record, or
nullif 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
nullif 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:
trueif the log contains a record with the given level,falseotherwise
-
isClosed
public boolean isClosed()Checks if this handler has been closed.- Returns:
trueif the handler is closed,falseotherwise
-
isEmpty
public boolean isEmpty()Checks if any log records have been captured.- Returns:
trueif no records have been captured,falseotherwise
-
printLogMessages
public void printLogMessages()Prints all captured log messages to standard output.Each message is printed on a separate line in the order it was logged.
-
printLogMessages
Prints all captured log messages to the specified output stream.Each message is printed on a separate line in the order it was logged.
- Parameters:
out- the output stream to print to
-
printLogMessagesWithLevel
public void printLogMessagesWithLevel()Prints all captured log messages with their log levels to standard output.Each message is printed in the format: [LEVEL] message
-
printLogMessagesWithLevel
Prints all captured log messages with their log levels to the specified output stream.Each message is printed in the format: [LEVEL] message
- Parameters:
out- the output stream to print to
-
printLogMessagesWithLevelAndTimestamp
public void printLogMessagesWithLevelAndTimestamp()Prints all captured log messages with both level and timestamp to standard output.Each message is printed in the format: [timestamp] [LEVEL] message
-
printLogMessagesWithLevelAndTimestamp
Prints all captured log messages with both level and timestamp to the specified output stream.Each message is printed in the format: [timestamp] [LEVEL] message
- Parameters:
out- the output stream to print to
-
printLogMessagesWithTimestamp
public void printLogMessagesWithTimestamp()Prints all captured log messages with timestamps to standard output.Each message is printed in the format: [timestamp] message
-
printLogMessagesWithTimestamp
Prints all captured log messages with timestamps to the specified output stream.Each message is printed in the format: [timestamp] message
- Parameters:
out- the output stream to print to
-
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.
-