Multiple log files with Winston?

ぐ巨炮叔叔 提交于 2019-12-17 23:11:43

问题


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?


回答1:


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




回答2:


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.




回答3:


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




回答4:


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




回答5:


This feature is now officially supported in Winston and is addressed in the 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()
  }));
}


来源:https://stackoverflow.com/questions/10045891/multiple-log-files-with-winston

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!