Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Multiple log files with Winston?

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?

like image 278
Domenic Avatar asked Apr 06 '12 15:04

Domenic


People also ask

Does Winston logger use log4j?

While it's not as popular or highly rated as Winston, Log4js has all the qualities I look for in a logger. It also has a similar feel to log4j, making it a popular choice for Java developers who have transitioned to Node. js.

Is Winston logging asynchronous?

The Nodejitsu team has released Winston, a pluggable, async logger for Node. js that also supports multiple transports. Out of the box, Winston includes several transports including: Console: Output to the terminal.

How do I change the log level in Winston?

You can change the logging level in runtime by modifying the level property of the appropriate transport: var log = new (winston. Logger)({ transports: [ new (winston.

How do I view Winston logs?

Go to the example. log file in the logs folder to view the log. Winston allows you to implement multiple logging transports, i.e., a log can be recorded to a file, console, or database.


3 Answers

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.

like image 131
Philipp Claßen Avatar answered Oct 09 '22 00:10

Philipp Claßen


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...

like image 29
ntranq Avatar answered Oct 09 '22 00:10

ntranq


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

like image 38
pesho Avatar answered Oct 09 '22 01:10

pesho