Skip to content

Commit 8339aaa

Browse files
committed
Add java.util.logging LogConsumer (#11203)
1 parent 73726f4 commit 8339aaa

File tree

3 files changed

+87
-39
lines changed

3 files changed

+87
-39
lines changed
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
package org.testcontainers.containers.output;
2+
3+
import java.util.function.Consumer;
4+
import java.util.function.Supplier;
5+
6+
public abstract class BaseLogConsumer<SELF extends BaseLogConsumer<SELF>> extends BaseConsumer<BaseLogConsumer<SELF>> {
7+
protected boolean separateOutputStreams;
8+
protected String prefix = "";
9+
10+
public BaseLogConsumer(boolean separateOutputStreams) {
11+
this.separateOutputStreams = separateOutputStreams;
12+
}
13+
14+
public BaseLogConsumer<SELF> withPrefix(String prefix) {
15+
this.prefix = "[" + prefix + "] ";
16+
return this;
17+
}
18+
19+
public BaseLogConsumer<SELF> withSeparateOutputStreams() {
20+
this.separateOutputStreams = true;
21+
return this;
22+
}
23+
24+
protected void log(Consumer<Supplier<String>> loggerError, Consumer<Supplier<String>> loggerInfo, OutputFrame outputFrame) {
25+
final OutputFrame.OutputType outputType = outputFrame.getType();
26+
final String utf8String = outputFrame.getUtf8StringWithoutLineEnding();
27+
Supplier<String> seperateOSSupplier = () -> (prefix.isEmpty() ? "" : (prefix + ": ")) + utf8String;
28+
Supplier<String> jointOSSupplier = () -> prefix + outputType + ": " + utf8String;
29+
switch (outputType) {
30+
case END:
31+
break;
32+
case STDOUT:
33+
if (separateOutputStreams) {
34+
loggerInfo.accept(seperateOSSupplier);
35+
} else {
36+
loggerInfo.accept(jointOSSupplier);
37+
}
38+
break;
39+
case STDERR:
40+
if (separateOutputStreams) {
41+
loggerError.accept(seperateOSSupplier);
42+
} else {
43+
loggerInfo.accept(jointOSSupplier);
44+
}
45+
break;
46+
default:
47+
throw new IllegalArgumentException("Unexpected outputType " + outputType);
48+
}
49+
50+
}
51+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package org.testcontainers.containers.output;
2+
3+
import java.util.logging.Logger;
4+
5+
/**
6+
* A consumer for container output that logs output to a {@link java.util.logging.Logger}.
7+
*/
8+
public class JulLogConsumer extends BaseLogConsumer<JulLogConsumer> {
9+
10+
private final Logger logger;
11+
12+
public JulLogConsumer(Logger logger) {
13+
this(logger, false);
14+
}
15+
16+
public JulLogConsumer(Logger logger, boolean separateOutputStreams) {
17+
super(separateOutputStreams);
18+
this.logger = logger;
19+
}
20+
21+
@Override
22+
public void accept(OutputFrame outputFrame) {
23+
log(logger::severe, logger::info, outputFrame);
24+
}
25+
}

core/src/main/java/org/testcontainers/containers/output/Slf4jLogConsumer.java

Lines changed: 11 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -9,28 +9,19 @@
99
/**
1010
* A consumer for container output that logs output to an SLF4J logger.
1111
*/
12-
public class Slf4jLogConsumer extends BaseConsumer<Slf4jLogConsumer> {
12+
public class Slf4jLogConsumer extends BaseLogConsumer<Slf4jLogConsumer> {
1313

1414
private final Logger logger;
1515

1616
private final Map<String, String> mdc = new HashMap<>();
1717

18-
private boolean separateOutputStreams;
19-
20-
private String prefix = "";
21-
2218
public Slf4jLogConsumer(Logger logger) {
2319
this(logger, false);
2420
}
2521

2622
public Slf4jLogConsumer(Logger logger, boolean separateOutputStreams) {
23+
super(separateOutputStreams);
2724
this.logger = logger;
28-
this.separateOutputStreams = separateOutputStreams;
29-
}
30-
31-
public Slf4jLogConsumer withPrefix(String prefix) {
32-
this.prefix = "[" + prefix + "] ";
33-
return this;
3425
}
3526

3627
public Slf4jLogConsumer withMdc(String key, String value) {
@@ -43,39 +34,20 @@ public Slf4jLogConsumer withMdc(Map<String, String> mdc) {
4334
return this;
4435
}
4536

46-
public Slf4jLogConsumer withSeparateOutputStreams() {
47-
this.separateOutputStreams = true;
48-
return this;
49-
}
50-
5137
@Override
5238
public void accept(OutputFrame outputFrame) {
53-
final OutputFrame.OutputType outputType = outputFrame.getType();
54-
final String utf8String = outputFrame.getUtf8StringWithoutLineEnding();
55-
5639
final Map<String, String> originalMdc = MDC.getCopyOfContextMap();
5740
MDC.setContextMap(mdc);
5841
try {
59-
switch (outputType) {
60-
case END:
61-
break;
62-
case STDOUT:
63-
if (separateOutputStreams) {
64-
logger.info("{}{}", prefix.isEmpty() ? "" : (prefix + ": "), utf8String);
65-
} else {
66-
logger.info("{}{}: {}", prefix, outputType, utf8String);
67-
}
68-
break;
69-
case STDERR:
70-
if (separateOutputStreams) {
71-
logger.error("{}{}", prefix.isEmpty() ? "" : (prefix + ": "), utf8String);
72-
} else {
73-
logger.info("{}{}: {}", prefix, outputType, utf8String);
74-
}
75-
break;
76-
default:
77-
throw new IllegalArgumentException("Unexpected outputType " + outputType);
78-
}
42+
log((s) -> {
43+
if (logger.isErrorEnabled()) {
44+
logger.error(s.get());
45+
}
46+
}, (s) -> {
47+
if (logger.isInfoEnabled()) {
48+
logger.info(s.get());
49+
}
50+
}, outputFrame);
7951
} finally {
8052
if (originalMdc == null) {
8153
MDC.clear();

0 commit comments

Comments
 (0)