LogsExtension is a JUnit 5 Extension responsible for initializing and managing the logging system during test execution.
It is based on Slf4jObserver and ensures that every report message (ReportEntry) is handled according to its source and log level.
How it works
The extension initializes Slf4jObserver, which receives Report messages and logs them to SLF4J according to the configuration:
- Supported log levels: TRACE, DEBUG, INFO, WARNING, ERROR, FATAL (FATAL is mapped to ERROR)
- Logging output can be customized using a
logback-test.xmlfile.
Logging configuration with logback-test.xml
The logback-test.xml file allows you to customize the logging output for your tests. Using this file, you can:
- Define log levels (TRACE, DEBUG, INFO, WARN, ERROR) for different packages or classes
- Configure appenders to control where the logs are written (console, file, etc.)
- Customize log formats, including timestamps, colors, and message patterns
- Enable or disable logging for specific tests or modules
This file is automatically picked up by the Logback framework during test execution, allowing you to experiment with different logging configurations without modifying your code.
For more details and examples, see the Baeldung guide on Logback.
Managing ReportSource
| Call | Behavior |
|---|---|
ReportSource.REPORT |
Not logged to SLF4J (intended for report observers only) |
ReportSource.LOG |
Logged only to SLF4J |
ReportSource.OTHER |
Logs to all targets (SLF4J and the other report observers) |
Usage examples
@ExtendWith(LogsExtension.class)
public class MyTest {
@Test
public void logTest(){
Report.info("report massage");
// Logged to all report observers at INFO level
Report.report(ReportSource.REPORT, ReportLevel.DEBUG, "report massage");
// Not logged to SLF4J
Report.report(ReportSource.LOG, ReportLevel.DEBUG, "report massage");
// Logged to SLF4J at DEBUG level
}
}