Search code examples
node.jsloggingnohupwinston

node.js winston logger no colors with nohup


We are using winston logger in our project with the following transport settings:

file: {
            filename: __base + '/log/server.log',
            colorize : true,
            timestamp : true,
            json : false,
            prettyPrint : true
        }

If the application is started with nohup, log file is not colorized. It works only without nohup.

nohup supervisor -w . -i node_modules/ server.js &

Is it problem with winston or nohup?


Solution

  • It's caused by colors package (used by winston) that performs the following check when trying to determine whether to support colors:

    if (process.stdout && !process.stdout.isTTY) {
        return false;
    }
    

    This means that when your application is running in a background, it doesn't have a terminal and colors are not used. This affects commands/apps other than nohup as well (see issue #121).

    A simple workaround is to start your application with --color=true argument (or to simulate it with process.argv.push('--color=true') before require('winston') is called.

    Alternatively, you can patch winston - just add one line to lib/winston/config.js:

    var colors = require('colors/safe');
    colors.enabled = true; // add this line
    

    However, all of these workarounds will most likely make the console logger to use colors even when there is no terminal.