最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

terminal - How to redirect console messages directly to the stdout of a child process in Node.js? - Stack Overflow

programmeradmin0浏览0评论

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?

发布评论

评论列表(0)

  1. 暂无评论