Best Testcontainers-java code snippet using org.testcontainers.containers.output.Slf4jLogConsumer.withMdc
Source:Slf4jLogConsumer.java
...21 public Slf4jLogConsumer withPrefix(String prefix) {22 this.prefix = "[" + prefix + "] ";23 return this;24 }25 public Slf4jLogConsumer withMdc(String key, String value) {26 mdc.put(key, value);27 return this;28 }29 public Slf4jLogConsumer withMdc(Map<String, String> mdc) {30 this.mdc.putAll(mdc);31 return this;32 }33 public Slf4jLogConsumer withSeparateOutputStreams() {34 this.separateOutputStreams = true;35 return this;36 }37 @Override38 public void accept(OutputFrame outputFrame) {39 OutputFrame.OutputType outputType = outputFrame.getType();40 String utf8String = outputFrame.getUtf8String();41 utf8String = utf8String.replaceAll(FrameConsumerResultCallback.LINE_BREAK_AT_END_REGEX, "");42 Map<String, String> originalMdc = MDC.getCopyOfContextMap();43 MDC.setContextMap(mdc);...
withMdc
Using AI Code Generation
1public class DockerTest {2 new DockerComposeContainer(new File("src/test/resources/docker-compose.yml"))3 .withLocalCompose(true)4 .withExposedService("selenium_hub", 4444)5 .withExposedService("selenium_node_chrome", 5555)6 .withLogConsumer("selenium_hub", new Slf4jLogConsumer(log).withPrefix("hub"))7 .withLogConsumer("selenium_node_chrome", new Slf4jLogConsumer(log).withPrefix("node_chrome"));8}9public class DockerTest {10 new DockerComposeContainer(new File("src/test/resources/docker-compose.yml"))11 .withLocalCompose(true)12 .withExposedService("selenium_hub", 4444)13 .withExposedService("selenium_node_chrome", 5555)14 .withTailChildContainers(true)15 .withLogConsumer("selenium_hub", new Slf4jLogConsumer(log).withPrefix("hub"))16 .withLogConsumer("selenium_node_chrome", new Slf4jLogConsumer(log).withPrefix("node_chrome"));17}18public class DockerTest {19 new DockerComposeContainer(new File("src/test/resources/docker-compose.yml"))20 .withLocalCompose(true)21 .withExposedService("selenium_hub", 4444)22 .withExposedService("selenium_node_chrome", 5555)23 .withLogConsumer("selenium_hub", new Slf4jLogConsumer(log).withPrefix("hub"))24 .withLogConsumer("selenium_node_chrome", new Slf4jLogConsumer(log).withPrefix("node_chrome"));25}
withMdc
Using AI Code Generation
1def slf4jLogConsumer = new Slf4jLogConsumer(logger)2def container = new GenericContainer('redis:3.2.8')3 .withExposedPorts(6379)4 .withCommand('redis-server --requirepass mysecretpassword')5 .withLogConsumer(slf4jLogConsumer)6 .withMdc()7 .withStartupTimeout(Duration.ofSeconds(60))8 .start()9def slf4jLogConsumer = new Slf4jLogConsumer(logger)10def container = new GenericContainer('redis:3.2.8')11 .withExposedPorts(6379)12 .withCommand('redis-server --requirepass mysecretpassword')13 .withLogConsumer(slf4jLogConsumer)14 .withStartupTimeout(Duration.ofSeconds(60))15 .start()16def slf4jLogConsumer = new Slf4jLogConsumer(logger)17def container = new GenericContainer('redis:3.2.8')18 .withExposedPorts(6379)19 .withCommand('redis-server --requirepass mysecretpassword')20 .withLogConsumer(slf4jLogConsumer)21 .withStartupTimeout(Duration.ofSeconds(60))22 .start()23def slf4jLogConsumer = new Slf4jLogConsumer(logger)24def container = new GenericContainer('redis:3.2.8')25 .withExposedPorts(6379)
withMdc
Using AI Code Generation
1public class Slf4jLogConsumer extends OutputFrame.OutputType {2 private static final String DEFAULT_LOGGER_NAME = "org.testcontainers";3 private final String loggerName;4 private final Map<String, String> mdcContextMap;5 public Slf4jLogConsumer(String loggerName, Map<String, String> mdcContextMap) {6 this.loggerName = loggerName;7 this.mdcContextMap = mdcContextMap;8 }9 public Slf4jLogConsumer(Map<String, String> mdcContextMap) {10 this(DEFAULT_LOGGER_NAME, mdcContextMap);11 }12 public Slf4jLogConsumer() {13 this(DEFAULT_LOGGER_NAME, new HashMap<>());14 }15 public void accept(OutputFrame outputFrame) {16 String message = outputFrame.getUtf8String();17 if (message.endsWith(System.lineSeparator())) {18 message = message.substring(0, message.length() - System.lineSeparator().length());19 }20 if (message.isEmpty()) {21 return;22 }23 try (MDC.MDCCloseable ignored = MDC.putCloseable(mdcContextMap)) {24 switch (outputFrame.getType()) {25 log(loggerName, Level.ERROR, message);26 break;27 log(loggerName, Level.INFO, message);28 break;29 throw new IllegalArgumentException("Unknown output type: " + outputFrame.getType());30 }31 }32 }33 private void log(String loggerName, Level level, String message) {34 Logger logger = LoggerFactory.getLogger(loggerName);35 switch (level) {36 logger.error(message);37 break;38 logger.warn(message);39 break;40 logger.info(message);41 break;42 logger.debug(message);43 break;44 logger.trace(message);45 break;46 throw new IllegalArgumentException("Unknown log level: " + level);47 }48 }49 public enum Level {50 }51}
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!