-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[17] Create a simple log consumer which writes the container logs to …
…a logger. Signed-off-by: James R. Perkins <[email protected]>
- Loading branch information
Showing
2 changed files
with
82 additions
and
0 deletions.
There are no files selected for viewing
75 changes: 75 additions & 0 deletions
75
src/main/java/org/jboss/arquillian/testcontainers/api/LoggingConsumer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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:[email protected]">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); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters