Skip to content

Commit f9bd94e

Browse files
committed
Improve logged exec output readability (#36217)
* Improve logged exec output readability - Split error and out streams and log them separately - Log everything in a single call to prevent interference from other log messages
1 parent 4658291 commit f9bd94e

File tree

1 file changed

+12
-8
lines changed

1 file changed

+12
-8
lines changed

buildSrc/src/main/java/org/elasticsearch/gradle/LoggedExec.java

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -21,16 +21,18 @@ public class LoggedExec extends Exec {
2121

2222
public LoggedExec() {
2323
ByteArrayOutputStream output = new ByteArrayOutputStream();
24+
ByteArrayOutputStream error = new ByteArrayOutputStream();
2425
if (getLogger().isInfoEnabled() == false) {
2526
setStandardOutput(output);
26-
setErrorOutput(output);
27+
setErrorOutput(error);
2728
setIgnoreExitValue(true);
2829
doLast((unused) -> {
2930
if (getExecResult().getExitValue() != 0) {
3031
try {
31-
for (String line : output.toString("UTF-8").split("\\R")) {
32-
getLogger().error(line);
33-
}
32+
getLogger().error("Standard output:");
33+
getLogger().error(output.toString("UTF-8"));
34+
getLogger().error("Standard error:");
35+
getLogger().error(error.toString("UTF-8"));
3436
} catch (UnsupportedEncodingException e) {
3537
throw new GradleException("Failed to read exec output", e);
3638
}
@@ -65,17 +67,19 @@ private static <T extends BaseExecSpec> ExecResult genericExec(
6567
return function.apply(action);
6668
}
6769
ByteArrayOutputStream output = new ByteArrayOutputStream();
70+
ByteArrayOutputStream error = new ByteArrayOutputStream();
6871
try {
6972
return function.apply(spec -> {
7073
spec.setStandardOutput(output);
71-
spec.setErrorOutput(output);
74+
spec.setErrorOutput(error);
7275
action.execute(spec);
7376
});
7477
} catch (Exception e) {
7578
try {
76-
for (String line : output.toString("UTF-8").split("\\R")) {
77-
project.getLogger().error(line);
78-
}
79+
project.getLogger().error("Standard output:");
80+
project.getLogger().error(output.toString("UTF-8"));
81+
project.getLogger().error("Standard error:");
82+
project.getLogger().error(error.toString("UTF-8"));
7983
} catch (UnsupportedEncodingException ue) {
8084
throw new GradleException("Failed to read exec output", ue);
8185
}

0 commit comments

Comments
 (0)