简体   繁体   中英

How do I stop supervisord from stripping colors from the log output of my Node.js server?

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:

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 ):

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.

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;

I found an answer to this question on the Super User Stack 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 .

For example

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

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

sudo apt-get install expect-dev

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM