如何停止monitor从节点的日志输出中剥离颜色。js服务器?

时间:2022-05-24 15:22:43

I am using supervisord to manage my Node.js server (ensure it reboots in the event of a crash and send a crash alert email). However, I am finding that if I run my app.js process through supervisord, the outputs to my server.log and to console are both stripped of color. I am using the Winston library to handle my logging. I have a few examples of output below:

我正在使用monitor sord管理我的节点。js服务器(确保在崩溃时重新启动,并发送崩溃警报电子邮件)。但是,我发现如果我通过monitor sord运行app.js进程,那么将输出到服务器。日志和控制台都没有颜色。我正在使用Winston库来处理日志记录。我有几个输出的例子:

Contents of server.log after running through supervisord:

内容服务器。通过监督管理:

2015-08-12T20:41:29.203Z - silly: Connected to redis
2015-08-12T20:41:29.206Z - debug: Connected to redis
2015-08-12T20:41:29.206Z - verbose: Connected to redis
2015-08-12T20:41:29.207Z - info: Connected to redis
2015-08-12T20:41:29.207Z - warn: Connected to redis
2015-08-12T20:41:29.207Z - error: Connected to redis

Contents of server.log after running through shell ($ node app.js):

内容服务器。通过shell运行后的日志($ node app.js):

2015-08-12T20:41:37.732Z - ^[[35msilly^[[39m: Connected to redis
2015-08-12T20:41:37.737Z - ^[[34mdebug^[[39m: Connected to redis
2015-08-12T20:41:37.741Z - ^[[36mverbose^[[39m: Connected to redis
2015-08-12T20:41:37.742Z - ^[[32minfo^[[39m: Connected to redis
2015-08-12T20:41:37.742Z - ^[[33mwarn^[[39m: Connected to redis
2015-08-12T20:41:37.742Z - ^[[31merror^[[39m: Connected to redis

I also noticed that if I use tail from supervisorctl to monitor my Node server, the color is stripped from there as well. When running it from the shell, I can see color output in the console.

我还注意到,如果我使用monitor sorctl的tail来监视节点服务器,那么颜色也会被从那里删除。当从shell中运行它时,我可以看到控制台中的颜色输出。

Does anyone know why this happens and how I can fix this issue?

有人知道这是为什么吗?我该如何解决这个问题?

EDIT: since someone asked for my Winston configuration:

编辑:因为有人问我温斯顿配置:

var winston = require( 'winston' ),
    fs = require( 'fs' ),
    logDir = 'logs', // Or read from a configuration
    logger;

winston.setLevels( winston.config.npm.levels );
winston.addColors( winston.config.npm.colors );

if ( !fs.existsSync( logDir ) ) { 
    // Create the directory if it does not exist
    fs.mkdirSync( logDir );
}
logger = new( winston.Logger )( {
    transports: [
        new winston.transports.Console( {
            level: 'silly',
            colorize: true
        } ),
        new winston.transports.File( {
            level: 'silly',
            json: false,
            colorize: true,
            filename: logDir + '/server.log',
            maxsize: 1024 * 1024 * 25 // 25MB
        } ) 
    ],  
    exceptionHandlers: [
        new winston.transports.File( {
            filename: 'log/exceptions.log'
        } ) 
    ]   
} );

module.exports = logger;

1 个解决方案

#1


3  

I found an answer to this question on the Super User Stack Echange.

我在超级用户栈Echange上找到了这个问题的答案。

To quote it:

引用:

Simply insert unbuffer before any command to make it think it is writing to an interactive output even if it is actually piping into another executable. This will preserve color in the case of ls.

只需在任何命令之前插入unbuffer,使其认为它正在向交互式输出写入数据,即使它实际上正在向另一个可执行文件传输。这将保留ls的颜色。

For example

例如

unbuffer ls -l --color=auto | tee output.log

取消缓冲ls -l——color=自动| tee输出。log

If you don't already have it installed, on Ubuntu and other Debian-ish Linux distributions you can install unbuffer by doing.

如果您还没有安装它,那么可以在Ubuntu和其他类似debian的Linux发行版上安装unbuffer。

sudo apt-get install expect-dev

sudo apt-get安装expect-dev

#1


3  

I found an answer to this question on the Super User Stack Echange.

我在超级用户栈Echange上找到了这个问题的答案。

To quote it:

引用:

Simply insert unbuffer before any command to make it think it is writing to an interactive output even if it is actually piping into another executable. This will preserve color in the case of ls.

只需在任何命令之前插入unbuffer,使其认为它正在向交互式输出写入数据,即使它实际上正在向另一个可执行文件传输。这将保留ls的颜色。

For example

例如

unbuffer ls -l --color=auto | tee output.log

取消缓冲ls -l——color=自动| tee输出。log

If you don't already have it installed, on Ubuntu and other Debian-ish Linux distributions you can install unbuffer by doing.

如果您还没有安装它,那么可以在Ubuntu和其他类似debian的Linux发行版上安装unbuffer。

sudo apt-get install expect-dev

sudo apt-get安装expect-dev