From df9efee25abce8b53c91c8c42673ed20ba039829 Mon Sep 17 00:00:00 2001 From: "James R. Perkins" Date: Tue, 2 Jul 2024 14:55:42 -0700 Subject: [PATCH] [17] Create a simple log consumer which writes the container logs to a logger. Signed-off-by: James R. Perkins --- .../testcontainers/api/LoggingConsumer.java | 75 +++++++++++++++++++ .../test/common/SimpleTestContainer.java | 7 ++ 2 files changed, 82 insertions(+) create mode 100644 src/main/java/org/jboss/arquillian/testcontainers/api/LoggingConsumer.java diff --git a/src/main/java/org/jboss/arquillian/testcontainers/api/LoggingConsumer.java b/src/main/java/org/jboss/arquillian/testcontainers/api/LoggingConsumer.java new file mode 100644 index 0000000..5352b92 --- /dev/null +++ b/src/main/java/org/jboss/arquillian/testcontainers/api/LoggingConsumer.java @@ -0,0 +1,75 @@ +/* + * Copyright The Arquillian Authors + * SPDX-License-Identifier: Apache-2.0 + */ + +package org.jboss.arquillian.testcontainers.api; + +import java.util.function.Consumer; +import java.util.logging.Logger; + +import org.testcontainers.containers.output.OutputFrame; + +/** + * A simple consumer for containers which logs the container lines to a {@linkplain Logger logger}. + * + * @author James R. Perkins + */ +public class LoggingConsumer implements Consumer { + + private final Logger logger; + + /** + * Creates a new logger with the name of {@link Class#getName()}. + * + * @param type the type to extract the name from + */ + public LoggingConsumer(final Class type) { + this(type.getName()); + } + + /** + * Creates a new logger with the name passed in. + * + * @param name the name for the logger + */ + public LoggingConsumer(final String name) { + this.logger = Logger.getLogger(name); + } + + /** + * Creates a new logger with the name of {@link Class#getName()}. + * + * @param type the type to extract the name from + */ + public static LoggingConsumer of(final Class type) { + return new LoggingConsumer(type); + } + + /** + * Creates a new logger with the name passed in. + * + * @param name the name for the logger + */ + public static LoggingConsumer of(final String name) { + return new LoggingConsumer(name); + } + + @Override + public void accept(final OutputFrame outputFrame) { + final OutputFrame.OutputType outputType = outputFrame.getType(); + final String utf8String = outputFrame.getUtf8StringWithoutLineEnding(); + switch (outputType) { + case END: + break; + case STDOUT: + logger.info(utf8String); + break; + case STDERR: + logger.severe(utf8String); + break; + default: + throw new IllegalArgumentException("Unexpected outputType " + outputType); + } + } +} diff --git a/src/test/java/org/jboss/arquillian/testcontainers/test/common/SimpleTestContainer.java b/src/test/java/org/jboss/arquillian/testcontainers/test/common/SimpleTestContainer.java index 6527648..76125fa 100644 --- a/src/test/java/org/jboss/arquillian/testcontainers/test/common/SimpleTestContainer.java +++ b/src/test/java/org/jboss/arquillian/testcontainers/test/common/SimpleTestContainer.java @@ -5,6 +5,7 @@ package org.jboss.arquillian.testcontainers.test.common; +import org.jboss.arquillian.testcontainers.api.LoggingConsumer; import org.testcontainers.containers.MockServerContainer; import org.testcontainers.utility.DockerImageName; @@ -18,4 +19,10 @@ public SimpleTestContainer() { .parse("mockserver/mockserver") .withTag("latest")); } + + @Override + protected void configure() { + super.configure(); + withLogConsumer(LoggingConsumer.of(SimpleTestContainer.class)); + } }