Multiple log files with Winston?

We would like to use Winston to log into Node.js. But we cannot figure out how to have two log files: one for simple errors and one for everything else.

Doing this naive method does not work, however: adding multiple winston.transports.File transports gives an error.

Others ran into this problem, with vague hints of a solution, but no real answer .

Any ideas?

+16
source share
5 answers

I just sent a transfer request, which allows you to use multiple file transports in one registrar. https://github.com/flatiron/winston/pull/149

It is already merged into flatiron / winston.

You can also use my forked repo: https://github.com/pdobrev/winston

+7
source

Unfortunately, the patch that was mentioned in pesho is still not included in the official version (see stephenbeeson's comment in checkout request # 149 ).

So, instead, I used a workaround. Since Winston compares the attributes of a name, you can trick it by specifying the name yourself:

 winston = require 'winston' logger = new winston.Logger transports: [ new winston.transports.File name: 'file#debug' level: 'debug' filename: '/tmp/debug.log' new winston.transports.File name: 'file#error' level: 'error' filename: '/tmp/error.log' ] logger.error 'error' # both logs logger.debug 'debug' # on debug log 

Maybe not elegant, but at least it works.

+18
source

In the meantime, you can implement a rudimentary shell using the same interface as

 var winston = require('winston'); var configs = require('./env.js'); var debug = new winston.Logger({ levels: { debug: 0 }, transports: [ new (winston.transports.File)({ filename: configs.PATH_TO_LOG, level: 'debug'}), new (winston.transports.Console)({level: 'debug'}) ] }); var info = new winston.Logger({ levels: { info: 1 }, transports: [ new (winston.transports.File)({ filename: configs.PATH_TO_LOG, level: 'info'}), new (winston.transports.Console)({level: 'info'}) ] }); var warn = new winston.Logger({ levels: { warn: 2 }, transports: [ new (winston.transports.File)({ filename: configs.PATH_TO_LOG, level: 'warn'}), new (winston.transports.Console)({level: 'warn'}) ] }); var error = new winston.Logger({ levels: { error: 3 }, transports: [ new (winston.transports.File)({ filename: configs.PATH_TO_LOG, level: 'error'}), new (winston.transports.Console)({level: 'error'}) ] }); var exports = { debug: function(msg){ debug.debug(msg); }, info: function(msg){ info.info(msg); }, warn: function(msg){ warn.warn(msg); }, error: function(msg){ error.error(msg); }, log: function(level,msg){ var lvl = exports[level]; lvl(msg); } }; module.exports = exports; 

This will cover the core winston API. can be expanded for metadata, etc.

+10
source

You just need to give the transport its own name so that you do not have collisions:

 const logger = new (winston.Logger)({ transports: [ new (winston.transports.Console)(), new (winston.transports.File)({ name: 'text', filename: logFile, json: false }), new (winston.transports.File)({ name: 'json', filename: logFileJson }) ] }); 

You can read more about several transports in the documentation: https://github.com/winstonjs/winston#multiple-transports-of-the-same-type

+5
source

This feature is now officially supported in Winston and is discussed in README here.

Code example:

 const logger = winston.createLogger({ level: 'info', format: winston.format.json(), defaultMeta: { service: 'user-service' }, transports: [ // // - Write to all logs with level 'info' and below to 'combined.log' // - Write all logs error (and below) to 'error.log'. // new winston.transports.File({ filename: 'error.log', level: 'error' }), new winston.transports.File({ filename: 'combined.log' }) ] }); // // If we're not in production then log to the 'console' with the format: // '${info.level}: ${info.message} JSON.stringify({ ...rest }) ' // if (process.env.NODE_ENV !== 'production') { logger.add(new winston.transports.Console({ format: winston.format.simple() })); } 
0
source

All Articles