Multiple log files with Winston?

You just need to give the transport a custom name property so you don't have a collision:

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 multiple transports in the docs: https://github.com/winstonjs/winston#multiple-transports-of-the-same-type


I just sent a pull request that allows using multiple File transports in one logger. 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


Unfortunately, the patch that pesho mentioned seems to be still not included in the official version (see stephenbeeson's comment in the pull request #149).

So, I used a workaround instead. As winston compares the name attributes, you can fool it by defining 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.


In the meantime, you can implement a rudimentary wrapper using the same interface like so

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 basic winston API. could be extended for metadata and so on...