Multiple log files with Winston?

Domenic picture Domenic · Apr 6, 2012 · Viewed 17.7k times · Source

We'd like to use Winston for our logging in Node.js. But, we can't figure out how to have two log files: one for just errors, and one for everything else.

Doing this the naive way doesn't work, however: adding multiple winston.transports.File transports gives an error.

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

Any ideas?

Answer

Philipp Claßen picture Philipp Claßen · Jun 29, 2013

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.