Class LoggingExtension

java.lang.Object
rife.bld.extension.testing.LoggingExtension
All Implemented Interfaces:
AfterEachCallback, BeforeEachCallback, Extension

public class LoggingExtension extends Object implements BeforeEachCallback, AfterEachCallback
JUnit extension for configuring console logging for test suites.

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 Details

    • LoggingExtension

      public LoggingExtension()
      Creates a LoggingExtension with the default logger and Level.ALL.

      The default logger is named after this class.

    • LoggingExtension

      public LoggingExtension(Logger logger)
      Creates a LoggingExtension with a custom logger and Level.ALL.
      Parameters:
      logger - the logger to configure for console output
      Throws:
      NullPointerException - if logger is null
    • LoggingExtension

      public LoggingExtension(Logger logger, Level level)
      Creates a LoggingExtension with a custom logger and logging level.
      Parameters:
      logger - the logger to configure for console output
      level - the logging level to set for both logger and console handler
      Throws:
      NullPointerException - if logger or level is null
    • LoggingExtension

      public LoggingExtension(Logger logger, Handler handler)
      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 output
      handler - the existing handler to use for logging output
      Throws:
      NullPointerException - if logger or handler is null
    • LoggingExtension

      public LoggingExtension(Logger logger, Handler handler, Level level)
      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 output
      handler - the existing handler to use for logging output
      level - the logging level to set for both logger and handler
      Throws:
      NullPointerException - if logger or level is null
    • LoggingExtension

      public LoggingExtension(String loggerName)
      Creates a LoggingExtension with a custom logger name and Level.ALL.
      Parameters:
      loggerName - the fully qualified logger name to configure for console output
    • LoggingExtension

      public LoggingExtension(String loggerName, Level level)
      Creates a LoggingExtension with a custom logger name and logging level.
      Parameters:
      loggerName - the fully qualified logger name to configure for console output
      level - the logging level to set for both logger and console handler
    • LoggingExtension

      public LoggingExtension(String loggerName, Handler handler)
      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 output
      handler - the existing handler to use for logging output
      Throws:
      NullPointerException - if handler is null
    • LoggingExtension

      public LoggingExtension(String loggerName, Handler handler, Level level)
      Creates a LoggingExtension with a custom logger name, existing handler, and logging level override.
      Parameters:
      loggerName - the fully qualified logger name to configure for output
      handler - the existing handler to use for logging output
      level - the logging level to set for both logger and handler
      Throws:
      NullPointerException - if handler or level is null
  • Method Details

    • afterEach

      public void afterEach(ExtensionContext context)
      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 interface AfterEachCallback
      Parameters:
      context - the extension context providing access to the test class
    • beforeEach

      public void beforeEach(ExtensionContext context)
      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 interface BeforeEachCallback
      Parameters:
      context - the extension context providing access to the test class