nodejs logging to multiple files

772 views Asked by At

Reading How to log - the 12 factor application way it states to use this command :

node app.js 2> app.err 1> app.out

I understand the > operator sends the output of command to file.

So I could also use node app.js output.log.

For command node app.js 2> app.err 1> app.out what is the mechanism that determines if app.err or app.out is logged to. Is it by the logging level within in the app ? What extra config is required to send logging data to app.err & app.out ?

1

There are 1 answers

2
ewcz On BEST ANSWER

The expected behavior is that the output sent to stderr will be redirected to app.err, while output sent to stdout will go into app.out. For example this script:

var winston = require('winston');
var transports = [
    new winston.transports.Console({
        level: 'debug',
        handleExceptions: true,
        json: false,
        timestamp: () => (new Date()).toLocaleString(),
        colorize: false
    })
];

var logger = new winston.Logger({
    transports: transports,
    exitOnError: false
});

logger.error('goes to stderr');
logger.info('goes to stdout');

would write goes to stderr to app.err, while goes to stdout would appear in app.out. This default behavior can be however overridden with the stderrLevels property using which one can specify which debug levels go to stderr.

Alternatively, using console, the behavior is documented here - by default, console.error goes to stderr (i.e., app.err in your setup) while console.log would output to stdout (app.out).