### Configure Log Levels and Assertions Source: https://github.com/hakky54/log-captor/blob/master/README.md Shows how to set specific log levels using LogCaptor and verify that only expected logs are captured based on the configured threshold. ```java import static org.assertj.core.api.Assertions.assertThat; import nl.altindag.log.LogCaptor; import org.junit.jupiter.api.Test; public class FooServiceShould { @Test public void logInfoAndWarnMessages() { LogCaptor logCaptor = LogCaptor.forClass(FooService.class); logCaptor.setLogLevelToInfo(); FooService fooService = new FooService(); fooService.sayHello(); assertThat(logCaptor.getInfoLogs()).contains("Congratulations, you are pregnant!"); assertThat(logCaptor.getDebugLogs()) .doesNotContain("Keyboard not responding. Press any key to continue...") .isEmpty(); } } ``` -------------------------------- ### Capture logging events with MDC in Java Source: https://github.com/hakky54/log-captor/blob/master/README.md Demonstrates how to capture log events and verify Diagnostic Context (MDC) values using LogCaptor and AssertJ. ```java import static org.assertj.core.api.Assertions.assertThat; import nl.altindag.log.LogCaptor; import nl.altindag.log.model.LogEvent; import org.junit.jupiter.api.Test; public class FooServiceShould { @Test void captureLoggingEventsContainingMdc() { LogCaptor logCaptor = LogCaptor.forClass(FooService.class); FooService service = new FooService(); service.sayHello(); List logEvents = logCaptor.getLogEvents(); assertThat(logEvents).hasSize(2); assertThat(logEvents.get(0).getDiagnosticContext()) .hasSize(1) .extractingByKey("my-mdc-key") .isEqualTo("my-mdc-value"); assertThat(logEvents.get(1).getDiagnosticContext()).isEmpty(); } } ``` -------------------------------- ### Capture Exceptions in Logs Source: https://github.com/hakky54/log-captor/blob/master/README.md Illustrates how to capture and assert log events that include thrown exceptions, verifying the exception type and message. ```java import static org.assertj.core.api.Assertions.assertThat; import nl.altindag.log.LogCaptor; import nl.altindag.log.model.LogEvent; import org.junit.jupiter.api.Test; public class FooServiceShould { @Test void captureLoggingEventsContainingException() { LogCaptor logCaptor = LogCaptor.forClass(ZooService.class); FooService service = new FooService(); service.sayHello(); List logEvents = logCaptor.getLogEvents(); assertThat(logEvents).hasSize(1); LogEvent logEvent = logEvents.get(0); assertThat(logEvent.getMessage()).isEqualTo("Caught unexpected exception"); assertThat(logEvent.getLevel()).isEqualTo("ERROR"); assertThat(logEvent.getThrowable()).isPresent(); assertThat(logEvent.getThrowable().get()) .hasMessage("KABOOM!") .isInstanceOf(IOException.class); } } ``` -------------------------------- ### Configure Logback for Console Output Source: https://github.com/hakky54/log-captor/blob/master/README.md This XML configuration sets up Logback to append all log messages to the console with a specific pattern. This is used in conjunction with ConsoleCaptor. ```xml %d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n ``` -------------------------------- ### Initialize and Reuse LogCaptor in JUnit Tests Source: https://github.com/hakky54/log-captor/blob/master/README.md Demonstrates how to initialize LogCaptor once using @BeforeAll and clear logs between tests using @AfterEach. This approach ensures a clean state for each test execution. ```java import nl.altindag.log.LogCaptor; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.AfterAll; import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.BeforeAll; public class FooServiceShould { private static LogCaptor logCaptor; private static final String EXPECTED_INFO_MESSAGE = "Keyboard not responding. Press any key to continue..."; private static final String EXPECTED_WARN_MESSAGE = "Congratulations, you are pregnant!"; @BeforeAll public static void setupLogCaptor() { logCaptor = LogCaptor.forClass(FooService.class); } @AfterEach public void clearLogs() { logCaptor.clearLogs(); } @AfterAll public static void tearDown() { logCaptor.close(); } @Test public void logInfoAndWarnMessagesAndGetWithEnum() { FooService service = new FooService(); service.sayHello(); assertThat(logCaptor.getInfoLogs()).containsExactly(EXPECTED_INFO_MESSAGE); assertThat(logCaptor.getWarnLogs()).containsExactly(EXPECTED_WARN_MESSAGE); assertThat(logCaptor.getLogs()).hasSize(2); } } ``` -------------------------------- ### Add ConsoleCaptor and Logback Dependencies Source: https://github.com/hakky54/log-captor/blob/master/README.md These XML dependencies are required to use the ConsoleCaptor for capturing logs and logback-classic for logging configuration. ```xml io.github.hakky54 consolecaptor test ch.qos.logback logback-classic test ``` -------------------------------- ### Capture Logs with LogCaptor in Java Source: https://github.com/hakky54/log-captor/blob/master/README.md This snippet demonstrates how to capture log messages using LogCaptor for a Java class. It shows the service class that generates logs and a corresponding JUnit test class that initializes LogCaptor, triggers the logging, and then asserts the captured log messages based on their levels or retrieves all captured logs. ```java import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; public class FooService { private static final Logger LOGGER = LogManager.getLogger(FooService.class); public void sayHello() { LOGGER.info("Keyboard not responding. Press any key to continue..."); LOGGER.warn("Congratulations, you are pregnant!"); } } ``` ```java import static org.assertj.core.api.Assertions.assertThat; import nl.altindag.log.LogCaptor; import org.junit.jupiter.api.Test; public class FooServiceShould { @Test public void logInfoAndWarnMessages() { LogCaptor logCaptor = LogCaptor.forClass(FooService.class); FooService fooService = new FooService(); fooService.sayHello(); // Get logs based on level assertThat(logCaptor.getInfoLogs()).containsExactly("Keyboard not responding. Press any key to continue..."); assertThat(logCaptor.getWarnLogs()).containsExactly("Congratulations, you are pregnant!"); // Get all logs assertThat(logCaptor.getLogs()) .hasSize(2) .contains( "Keyboard not responding. Press any key to continue...", "Congratulations, you are pregnant!" ); } } ``` -------------------------------- ### Access LogCaptor returnable values in Java Source: https://github.com/hakky54/log-captor/blob/master/README.md Illustrates how to retrieve various log levels and extract detailed metadata from LogEvent objects such as thread names, timestamps, and throwables. ```java import nl.altindag.log.LogCaptor; import nl.altindag.log.model.LogEvent; import java.time.ZonedDateTime; import java.util.List; import java.util.Map; import java.util.Optional; import org.junit.jupiter.api.Test; class FooServiceShould { @Test void showCaseAllReturnableValues() { LogCaptor logCaptor = LogCaptor.forClass(FooService.class); List logs = logCaptor.getLogs(); List infoLogs = logCaptor.getInfoLogs(); List debugLogs = logCaptor.getDebugLogs(); List warnLogs = logCaptor.getWarnLogs(); List errorLogs = logCaptor.getErrorLogs(); List traceLogs = logCaptor.getTraceLogs(); LogEvent logEvent = logCaptor.getLogEvents().get(0); String message = logEvent.getMessage(); String formattedMessage = logEvent.getFormattedMessage(); String level = logEvent.getLevel(); List arguments = logEvent.getArguments(); String loggerName = logEvent.getLoggerName(); String threadName = logEvent.getThreadName(); ZonedDateTime timeStamp = logEvent.getTimeStamp(); Map diagnosticContext = logEvent.getDiagnosticContext(); Optional throwable = logEvent.getThrowable(); } } ``` -------------------------------- ### Capture Logs in Quarkus Test with ConsoleCaptor Source: https://github.com/hakky54/log-captor/blob/master/README.md This Java test class demonstrates how to use ConsoleCaptor within a Quarkus test to capture log output from a resource. It ensures that the log message 'Hello' is captured. ```java import io.quarkus.test.junit.QuarkusTest; import nl.altindag.console.ConsoleCaptor; import org.junit.jupiter.api.Test; import java.util.List; import static org.assertj.core.api.Assertions.assertThat; @QuarkusTest class QuarkusTestTest { @Test void captureLogs() { try(ConsoleCaptor consoleCaptor = new ConsoleCaptor()) { HelloResource resource = new HelloResource(); resource.hello(); List standardOutput = consoleCaptor.getStandardOutput(); assertThat(standardOutput) .hasSize(1) .contains("Hello"); } } } ``` -------------------------------- ### Disable console output via logback-test.xml Source: https://github.com/hakky54/log-captor/blob/master/README.md Configures Logback to suppress console output during tests by adding a status listener to the configuration file. ```xml ``` -------------------------------- ### Exclude conflicting SLF4J dependencies in build tools Source: https://github.com/hakky54/log-captor/blob/master/README.md Resolves 'multiple SLF4J bindings' errors by excluding specific logging implementations during test phases. This ensures Log Captor's Logback implementation takes precedence. ```xml org.apache.maven.plugins maven-surefire-plugin org.apache.logging.log4j:log4j-slf4j-impl org.apache.logging.log4j:log4j-slf4j2-impl org.apache.maven.plugins maven-failsafe-plugin org.apache.logging.log4j:log4j-slf4j-impl org.apache.logging.log4j:log4j-slf4j2-impl ``` ```groovy configurations { testImplementation { exclude(group = "org.apache.logging.log4j", module = "log4j-slf4j-impl") exclude(group = "org.apache.logging.log4j", module = "log4j-slf4j2-impl") } } ``` -------------------------------- ### Disable logs for a specific class in Java Source: https://github.com/hakky54/log-captor/blob/master/README.md Shows how to suppress logging output from a specific class during unit tests using LogCaptor's disable and reset methods. ```java import static org.assertj.core.api.Assertions.assertThat; import nl.altindag.log.LogCaptor; import org.junit.jupiter.api.AfterAll; import org.junit.jupiter.api.AutoClose; import org.junit.jupiter.api.BeforeAll; import org.junit.jupiter.api.Test; public class FooServiceShould { @AutoClose private static LogCaptor logCaptorForSomeOtherService = LogCaptor.forClass(SomeService.class); @BeforeAll static void disableLogs() { logCaptorForSomeOtherService.disableLogs(); } @AfterAll static void resetLogLevel() { logCaptorForSomeOtherService.resetLogLevel(); } @Test void logInfoAndWarnMessages() { LogCaptor logCaptor = LogCaptor.forClass(FooService.class); FooService service = new FooService(); service.sayHello(); assertThat(logCaptor.getLogs()) .hasSize(2) .contains( "Keyboard not responding. Press any key to continue...", "Congratulations, you are pregnant!" ); } } ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.