Skip to content
This repository has been archived by the owner on Sep 26, 2019. It is now read-only.

Prefix log messages from nodes running as separate processes with the node name #1018

Merged
merged 5 commits into from
Mar 4, 2019
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,11 @@
import tech.pegasys.pantheon.ethereum.jsonrpc.RpcApi;
import tech.pegasys.pantheon.ethereum.jsonrpc.RpcApis;

import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;
import java.lang.ProcessBuilder.Redirect;
import java.net.URI;
import java.nio.file.Files;
import java.nio.file.Path;
Expand All @@ -28,6 +31,8 @@
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;

Expand All @@ -38,8 +43,10 @@
public class ProcessPantheonNodeRunner implements PantheonNodeRunner {

private final Logger LOG = LogManager.getLogger();
private final Logger PROCESS_LOG = LogManager.getLogger("tech.pegasys.pantheon.SubProcessLog");

private final Map<String, Process> pantheonProcesses = new HashMap<>();
private final ExecutorService outputProcessorExecutor = Executors.newCachedThreadPool();

ProcessPantheonNodeRunner() {
Runtime.getRuntime().addShutdownHook(new Thread(this::shutdown));
Expand Down Expand Up @@ -157,10 +164,12 @@ public void startNode(final PantheonNode node) {
final ProcessBuilder processBuilder =
new ProcessBuilder(params)
.directory(new File(System.getProperty("user.dir")).getParentFile())
.inheritIO();
.redirectErrorStream(true)
.redirectInput(Redirect.INHERIT);

try {
final Process process = processBuilder.start();
outputProcessorExecutor.submit(() -> printOutput(node, process));
pantheonProcesses.put(node.getName(), process);
} catch (final IOException e) {
LOG.error("Error starting PantheonNode process", e);
Expand All @@ -169,6 +178,19 @@ public void startNode(final PantheonNode node) {
waitForPortsFile(dataDir);
}

private void printOutput(final PantheonNode node, final Process process) {
try (final BufferedReader in =
new BufferedReader(new InputStreamReader(process.getInputStream(), UTF_8))) {
String line = in.readLine();
while (line != null) {
PROCESS_LOG.info("{}: {}", node.getName(), line);
line = in.readLine();
}
} catch (final IOException e) {
LOG.error("Failed to read output from process", e);
}
}

private Path createGenesisFile(final PantheonNode node, final EthNetworkConfig ethNetworkConfig) {
try {
final Path genesisFile = Files.createTempFile(node.homeDirectory(), "genesis", "");
Expand Down Expand Up @@ -197,6 +219,15 @@ public void stopNode(final PantheonNode node) {
public synchronized void shutdown() {
final HashMap<String, Process> localMap = new HashMap<>(pantheonProcesses);
localMap.forEach(this::killPantheonProcess);
outputProcessorExecutor.shutdown();
try {
if (!outputProcessorExecutor.awaitTermination(5, TimeUnit.SECONDS)) {
LOG.error("Output processor executor did not shutdown cleanly.");
}
} catch (final InterruptedException e) {
LOG.error("Interrupted while already shutting down", e);
Thread.currentThread().interrupt();
}
}

@Override
Expand Down
6 changes: 6 additions & 0 deletions acceptance-tests/src/test/resources/log4j2.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,14 @@
<Console name="Console" target="SYSTEM_OUT">
<PatternLayout pattern="%d{HH:mm:ss.SSS} | %t | %-5level | %c{1} | %msg%n" />
</Console>
<Console name="SubProcessConsole" target="SYSTEM_OUT">
<PatternLayout pattern="Process %msg%n" />
</Console>
</Appenders>
<Loggers>
<Logger name="tech.pegasys.pantheon.SubProcessLog" level="INFO" additivity="false">
<AppenderRef ref="SubProcessConsole" />
</Logger>
<Root level="${sys:root.log.level}">
<AppenderRef ref="Console" />
</Root>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -201,13 +201,6 @@ protected void handleIncomingPacket(final Endpoint sourceEndpoint, final Packet
* @param packet the packet to send
*/
protected void handleOutgoingPacket(final DiscoveryPeer peer, final Packet packet) {
LOG.trace(
">>> Sending {} discovery packet to {} ({}): {}",
packet.getType(),
peer.getEndpoint(),
peer.getId().slice(0, 16),
packet);

sendOutgoingPacket(peer, packet)
.whenComplete(
(res, err) -> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public class DiscoveryProtocolLogger {

static void logSendingPacket(final Peer peer, final Packet packet) {
LOG.trace(
"<<< Sending {} packet from peer {} ({}): {}",
"<<< Sending {} packet to peer {} ({}): {}",
shortenPacketType(packet),
peer.getId().slice(0, 16),
peer.getEndpoint(),
Expand Down