I would like to send custom log messages from one Node.js process to the stdout of a child process, without using intermediary files or separate streams.
The idea is to start a server in one terminal window and display status information in that window, while redirecting log messages to be displayed in a separate terminal window. I could save the log messages to a file and tail the file, but I don't want to handle files or extra hoops, I just want the messages displayed in another window.
I'm trying to spawn the child process like so:
import { spawn } from 'child_process';
const terminal = spawn("x-terminal-emulator", ["-e", "node", "log_client.js"], {
stdio: ['pipe', 'pipe', 'pipe']
});
And then the custom messages would be routed via a custom Console instance:
const { Console } = require("console");
const customConsole = new Console(terminal.stdout, terminal.stderr);
customConsole.log("This is printed in the spawned terminal window.");
The goal here is to have the log messages printed in the spawned window without having to set up event listeners. I don't want to process data, I just want it displayed as it is sent.
This approach isn't working, nothing is displayed in the child process. I also tried delaying the sending with setTimeout
or setInterval
but that didn't work either. Can I do it this way and what am I getting wrong?