Skip to content

Commit bbe595e

Browse files
authored
Merge pull request #3 from zonkyio/fix-process-logging
Fix process logging
2 parents adb4ff8 + 2ec4d90 commit bbe595e

File tree

2 files changed

+15
-10
lines changed

2 files changed

+15
-10
lines changed

src/main/java/io/zonky/test/db/postgres/embedded/EmbeddedPostgres.java

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -252,7 +252,7 @@ private void startPostmaster(Map<String, String> connectConfig) throws IOExcepti
252252
final Process postmaster = builder.start();
253253

254254
if (outputRedirector.type() == ProcessBuilder.Redirect.Type.PIPE) {
255-
ProcessOutputLogger.logOutput(LOG, postmaster);
255+
ProcessOutputLogger.logOutput(LOG, postmaster, "pg_ctl");
256256
}
257257

258258
LOG.info("{} postmaster started as {} on port {}. Waiting up to {} for server startup to finish.", instanceId, postmaster.toString(), port, pgStartupWait);
@@ -573,20 +573,22 @@ public EmbeddedPostgres start() throws IOException {
573573
}
574574
}
575575

576-
private static List<String> system(String... command)
576+
private void system(String... command)
577577
{
578578
try {
579579
final ProcessBuilder builder = new ProcessBuilder(command);
580-
builder.redirectError(ProcessBuilder.Redirect.PIPE);
581-
builder.redirectOutput(ProcessBuilder.Redirect.PIPE);
582580
builder.redirectErrorStream(true);
581+
builder.redirectError(errorRedirector);
582+
builder.redirectOutput(outputRedirector);
583583
final Process process = builder.start();
584+
585+
if (outputRedirector.type() == ProcessBuilder.Redirect.Type.PIPE) {
586+
String processName = command[0].replaceAll("^.*[\\\\/](\\w+)(\\.exe)?$", "$1");
587+
ProcessOutputLogger.logOutput(LOG, process, processName);
588+
}
584589
if (0 != process.waitFor()) {
585590
throw new IllegalStateException(String.format("Process %s failed%n%s", Arrays.asList(command), IOUtils.toString(process.getErrorStream())));
586591
}
587-
try (InputStream stream = process.getInputStream()) {
588-
return IOUtils.readLines(stream);
589-
}
590592
} catch (final RuntimeException e) { // NOPMD
591593
throw e;
592594
} catch (final Exception e) {

src/main/java/io/zonky/test/db/postgres/embedded/ProcessOutputLogger.java

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,14 @@
1313
*/
1414
package io.zonky.test.db.postgres.embedded;
1515

16+
import org.slf4j.Logger;
17+
1618
import java.io.BufferedReader;
1719
import java.io.IOException;
1820
import java.io.InputStreamReader;
1921
import java.nio.charset.StandardCharsets;
2022

21-
import org.slf4j.Logger;
23+
import static org.apache.commons.lang3.StringUtils.isNotBlank;
2224

2325
/**
2426
* Read standard output of process and write lines to given {@link Logger} as INFO;
@@ -61,9 +63,10 @@ public void run() {
6163
}
6264
}
6365

64-
static void logOutput(final Logger logger, final Process process) {
66+
static void logOutput(final Logger logger, final Process process, final String processName) {
67+
final String threadName = isNotBlank(processName) ? processName + "@" + process.hashCode() : process.toString();
6568
final Thread t = new Thread(new ProcessOutputLogger(logger, process));
66-
t.setName("output redirector for " + process);
69+
t.setName(threadName);
6770
t.start();
6871
}
6972
}

0 commit comments

Comments
 (0)