重定向时节点标准输出的彩色输出

Colored output from node stdout when redirected

我正在使用 'colors' 模块和节点 js。当我 运行 只需使用 'node app.js' 时,我可以在终端中看到彩色输出。

但是,当我使用 'node app.js > out.log &' 重定向输出时,或者当我 运行 将其作为 systemd 守护进程并使用 journalctl 查看输出时,颜色消失了。

我希望能够 运行 节点程序作为 systemd 守护进程,同时能够在日志输出中保留颜色。如果可能,请帮助我理解问题并找到解决方案。

你看到的输出没有颜色,因为默认情况下 journalctl 使用 short 作为输出模式,如果你使用 cat,你会看到你的颜色

示例:

journalctl -o cat -u myservice.service