Class LoggingExtension
- All Implemented Interfaces:
AfterEachCallback
,BeforeEachCallback
,Extension
This extension sets up a console handler with a configurable logging level before each test method and restores the original logger configuration after each test method completes. This provides maximum isolation between individual test methods.
The logger is configured to output directly to the console without using parent handlers.
Usage Examples:
@ExtendWith(LoggingExtension.class) class MyTestClass { // Default configuration (uses LoggingExtension logger with Level.ALL) @Test void myTest() { ... } // Custom logger with default level @RegisterExtension private static final LOGGING_EXTENSION extension = new LoggingExtension("MyCustomLogger")); // Custom logger and level @RegisterExtension private static final LOGGING_EXTENSION extension = new LoggingExtension( MyClass.getLogger(), Level.INFO ); // Custom logger with test log handler private static final Logger LOGGER = Logger.getLogger(MyClass.class.getName()); private static final TestLogHandler TEST_LOG_HANDLER = new TestLogHandler(); @RegisterExtension private static final LOGGING_EXTENSION extension = new LoggingExtension( LOGGER, TEST_LOG_HANDLER, ); // Custom logger with existing handler and level override @RegisterExtension private static final LOGGING_EXTENSION extension = new LoggingExtension( MyClass.getLogger(), myExistingHandler, Level.WARNING ); }
- Since:
- 1.0
- Author:
- Erik C. Thauvin
- See Also:
-
Constructor Summary
ConstructorsConstructorDescriptionCreates a LoggingExtension with the default logger andLevel.ALL
.LoggingExtension
(String loggerName) Creates a LoggingExtension with a custom logger name andLevel.ALL
.LoggingExtension
(String loggerName, Handler handler) Creates a LoggingExtension with a custom logger name and existing handler.LoggingExtension
(String loggerName, Handler handler, Level level) Creates a LoggingExtension with a custom logger name, existing handler, and logging level override.LoggingExtension
(String loggerName, Level level) Creates a LoggingExtension with a custom logger name and logging level.LoggingExtension
(Logger logger) Creates a LoggingExtension with a custom logger andLevel.ALL
.LoggingExtension
(Logger logger, Handler handler) Creates a LoggingExtension with a custom logger and existing handler.LoggingExtension
(Logger logger, Handler handler, Level level) Creates a LoggingExtension with a custom logger, existing handler, and logging level override.LoggingExtension
(Logger logger, Level level) Creates a LoggingExtension with a custom logger and logging level. -
Method Summary
Modifier and TypeMethodDescriptionvoid
afterEach
(ExtensionContext context) Restores the original logger and handler configuration after each test method completes.void
beforeEach
(ExtensionContext context) Configures the logger with console output before each test method runs.
-
Constructor Details
-
LoggingExtension
public LoggingExtension()Creates a LoggingExtension with the default logger andLevel.ALL
.The default logger is named after
this class
. -
LoggingExtension
Creates a LoggingExtension with a custom logger andLevel.ALL
.- Parameters:
logger
- the logger to configure for console output- Throws:
NullPointerException
- if logger is null
-
LoggingExtension
Creates a LoggingExtension with a custom logger and logging level.- Parameters:
logger
- the logger to configure for console outputlevel
- the logging level to set for both logger and console handler- Throws:
NullPointerException
- if logger or level is null
-
LoggingExtension
Creates a LoggingExtension with a custom logger and existing handler.The handler's existing level will be preserved unless overridden by constructor parameters.
- Parameters:
logger
- the logger to configure for outputhandler
- the existing handler to use for logging output- Throws:
NullPointerException
- if logger or handler is null
-
LoggingExtension
Creates a LoggingExtension with a custom logger, existing handler, and logging level override.The specified level will be applied to both the logger and the handler, overriding the handler's existing level configuration.
- Parameters:
logger
- the logger to configure for outputhandler
- the existing handler to use for logging outputlevel
- the logging level to set for both logger and handler- Throws:
NullPointerException
- if logger or level is null
-
LoggingExtension
Creates a LoggingExtension with a custom logger name andLevel.ALL
.- Parameters:
loggerName
- the fully qualified logger name to configure for console output
-
LoggingExtension
Creates a LoggingExtension with a custom logger name and logging level.- Parameters:
loggerName
- the fully qualified logger name to configure for console outputlevel
- the logging level to set for both logger and console handler
-
LoggingExtension
Creates a LoggingExtension with a custom logger name and existing handler.The handler's existing level will be preserved.
- Parameters:
loggerName
- the fully qualified logger name to configure for outputhandler
- the existing handler to use for logging output- Throws:
NullPointerException
- if handler is null
-
LoggingExtension
Creates a LoggingExtension with a custom logger name, existing handler, and logging level override.- Parameters:
loggerName
- the fully qualified logger name to configure for outputhandler
- the existing handler to use for logging outputlevel
- the logging level to set for both logger and handler- Throws:
NullPointerException
- if handler or level is null
-
-
Method Details
-
afterEach
Restores the original logger and handler configuration after each test method completes.This method removes any handlers added by this extension, restores the logger's original level and parent handler usage settings, and resets any modified handler levels back to their original state. If the handler is a
TestLogHandler
, it also clears its captured log records. This ensures that both logger and handler state don't leak between individual test methods.- Specified by:
afterEach
in interfaceAfterEachCallback
- Parameters:
context
- the extension context providing access to the test class
-
beforeEach
Configures the logger with console output before each test method runs.This method is called before every test method. It performs the following configuration:
- Uses the provided handler or creates a
ConsoleHandler
with the specified level - Adds the handler to the logger
- Sets the logger's level
- Disables parent handler usage to prevent duplicate output
- Stores the original logger and handler state for restoration after the test method completes
- Specified by:
beforeEach
in interfaceBeforeEachCallback
- Parameters:
context
- the extension context providing access to the test class
- Uses the provided handler or creates a
-