-
Notifications
You must be signed in to change notification settings - Fork 38
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
68 additions
and
10 deletions.
There are no files selected for viewing
60 changes: 60 additions & 0 deletions
60
src/test/java/org/jenkinsci/plugins/sonargerrit/test_infrastructure/JulLogConsumer.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,60 @@ | ||
package org.jenkinsci.plugins.sonargerrit.test_infrastructure; | ||
|
||
import java.util.logging.Level; | ||
import java.util.logging.Logger; | ||
import org.testcontainers.containers.output.BaseConsumer; | ||
import org.testcontainers.containers.output.OutputFrame; | ||
|
||
// based on org.testcontainers.containers.output.Slf4jLogConsumer | ||
public class JulLogConsumer extends BaseConsumer<JulLogConsumer> { | ||
private final Logger logger; | ||
private boolean separateOutputStreams; | ||
private String prefix = ""; | ||
|
||
public JulLogConsumer(Logger logger) { | ||
this(logger, false); | ||
} | ||
|
||
public JulLogConsumer(Logger logger, boolean separateOutputStreams) { | ||
this.logger = logger; | ||
this.separateOutputStreams = separateOutputStreams; | ||
} | ||
|
||
public JulLogConsumer withPrefix(String prefix) { | ||
this.prefix = "[" + prefix + "] "; | ||
return this; | ||
} | ||
|
||
public JulLogConsumer withSeparateOutputStreams() { | ||
this.separateOutputStreams = true; | ||
return this; | ||
} | ||
|
||
@Override | ||
public void accept(OutputFrame outputFrame) { | ||
OutputFrame.OutputType outputType = outputFrame.getType(); | ||
|
||
String utf8String = outputFrame.getUtf8String().replaceAll("((\\r?\\n)|(\\r))$", ""); | ||
|
||
switch (outputType) { | ||
case END: | ||
break; | ||
case STDOUT: | ||
if (separateOutputStreams) { | ||
logger.log(Level.INFO, () -> prefix.isEmpty() ? "" : (prefix + ": ") + utf8String); | ||
} else { | ||
logger.log(Level.INFO, () -> prefix + outputType + ": " + utf8String); | ||
} | ||
break; | ||
case STDERR: | ||
if (separateOutputStreams) { | ||
logger.log(Level.SEVERE, () -> prefix.isEmpty() ? "" : (prefix + ": ") + utf8String); | ||
} else { | ||
logger.log(Level.INFO, () -> prefix + outputType + ": " + 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
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