如何阻止supervisord从我的Node.js服务器的日志输出中剥离颜色?

时间:2015-08-12 20:54:52

标签: linux node.js logging supervisord

我正在使用supervisord来管理我的Node.js服务器(确保它在发生崩溃时重新启动并发送崩溃警报电子邮件)。但是,我发现如果我通过supervisord运行app.js进程,则server.logconsole的输出都会被剥离颜色。我正在使用Winston library来处理我的日志记录。我在下面有一些输出示例:

server.log后的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

通过shell(server.log)运行后$ 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

我还注意到,如果我使用tail中的supervisorctl来监控我的节点服务器,那么颜色也会从那里剥离。从shell运行它时,我可以在控制台中看到颜色输出。

有谁知道为什么会发生这种情况以及如何解决这个问题?

编辑:因为有人要求我的Winston配置:

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 个答案:

答案 0 :(得分:3)

我在Super User Stack Echange找到了这个问题的答案。

引用它:

  

只需在任何命令之前插入unbuffer以使其认为是   写入交互式输出,即使它实际上是管道输入   另一个可执行这将在ls的情况下保留颜色。

     

例如

     

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

     

如果您还没有安装它,可以在Ubuntu和其他Debian-ish Linux发行版上安装unbuffer

     

sudo apt-get install expect-dev

相关问题