如何使用不同的文件添加两个或更多记录器?

时间:2017-11-24 18:48:36

标签: node.js express logging winston

我正在研究节点Js日志库。我正在使用winston。我创建了日志文件,但是当我想要创建两个日志文件时我卡住了。假设我必须创建包含所有日志和错误日志文件的所有日志文件,其中只包含错误日志。但我坚持在这里:

if (!fs.existsSync(logDir)) {
    fs.mkdirSync(logDir);
}
const tsFormat = () => (new Date()).toLocaleTimeString();
let logger = new(winston.Logger)({
    transports: [
        new(winston.transports.Console)({
            level: 'debug',
            timestamp: tsFormat,
            handleExceptions: true,
            colorize: true,
            json: false
        }),
        new(winston.transports.File)({
            name: 'all-file',
            handleExceptions: true,
            filename: `${logDir}/all-file.log`,
            level: 'debug',
            maxsize: 100000000,
            json: true
        }),
        new(winston.transports.File)({
            name: 'error-file',
            handleExceptions: true,
            filename: `${logDir}/error-file.log`,
            level: 'error',
            maxsize: 100000000,
            json: false
        })
    ]
});

我希望错误日志显示不同类型的控制台而不是调试控制台。但默认情况下它只接受调试。如何为这两个文件手动添加控制台配置。当我尝试添加另一个transport.console时,它会显示错误。

Transport already attached: console, assign a different name

1 个答案:

答案 0 :(得分:0)

var winston = require('winston');
let consoleLogger = function(options){
  return new (winston.Logger)({
  transports: [
    new (winston.transports.Console)(options),
  ]
});
} 

// the CustomTransport is to prevent error log show multiple times 
class CustomTransport extends winston.Transport {
  constructor(options) {
    super(options);
    this.options = options;
    this.levels = options && options.levels || [this.level];
  }

  log(level, msg, meta, callback) {
    if(this.levels.indexOf(level) > -1){
      consoleLogger(this.options)[level](msg, meta);
    }
    callback(null, true);
  }
}

winston.transports.CustomTransport = CustomTransport;

const tsFormat = () => (new Date()).toLocaleTimeString();
var logger = new (winston.Logger)({
  transports: [
    // add name attribute to prevent Transport already attached error
    // no error log for this transport
      new (winston.transports.CustomTransport)(
        {
          name: 'info-console',
          level: 'debug',
          levels : ['debug', 'verbose', 'info', 'warn'],
          timestamp: tsFormat,
          handleExceptions: true,
          colorize: true,
          json: false
      }
      ),
      // only error log for this transport, modify the configuration as you need
      new (winston.transports.CustomTransport)(
        {
          name: 'error-console',
          level: 'error',
          timestamp: tsFormat,
          handleExceptions: true,
          colorize: true,
          json: false
        }
      ),
      new (winston.transports.File)(
          { 
              name: 'info-file',
              filename: 'info.log',
              timestamp: true,
              maxsize: 1024000,
              level: 'info'
          }),
      new (winston.transports.File)(
          { 
              name: 'error-file',
              filename: 'error.txt',
              timestamp: true,
              maxsize: 1024000,
              level: 'error'
          })
  ]});

用法:

 logger.debug('debug');
 logger.error('error');