Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[17] Create a simple log consumer which writes the container logs to … #18

Merged
merged 2 commits into from
Jul 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions .github/workflows/maven.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,13 @@ on:
- 'dependabot/**'
paths:
- '**/pom.xml'
- 'src/*'
- 'src/**'
pull_request:
branches:
- '**'
paths:
- '**/pom.xml'
- 'src/*'
- 'src/**'

# Only run the latest job
concurrency:
Expand Down
Original file line number Diff line number Diff line change
@@ -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 <a href="mailto:jperkins@redhat.com">James R. Perkins</a>
*/
public class LoggingConsumer implements Consumer<OutputFrame> {

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);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -18,4 +19,10 @@ public SimpleTestContainer() {
.parse("mockserver/mockserver")
.withTag("latest"));
}

@Override
protected void configure() {
super.configure();
withLogConsumer(LoggingConsumer.of(SimpleTestContainer.class));
}
}