如何使用Winston将记录器传输到文件

时间:2017-10-09 06:15:32

标签: node.js logging winston

我需要将记录器传输到记录错误和警告的文本​​文件。现在,我使用Winston进行控制台记录传输:

SELECT * FROM ART WHERE category=val OR subject=val OR medium=val;

1 个答案:

答案 0 :(得分:1)

要在文件中记录跟踪,请尝试添加以下代码段

new (winston.transports.File)({ filename: 'somefile.log' });

添加logger作业后,应该看起来像

logger = new winston.Logger({
    transports: [
      new (winston.transports.Console)({
        name: 'info-console',
        level: configLogging.level,
        colorize: true,
        timestamp: function() { return m.utc().format(dateFormat); }
      }),
      new (winston.transports.File)({ filename: 'somefile.log' })
    ]
});

logger.info('Starting logging service');

<强>更新

单独记录ErrorsLogs请尝试使用

var logger = new (winston.Logger)({
  transports: [
    new (winston.transports.File)({
      name: 'info-file',
      filename: 'filelog-info.log',
      level: 'info'
    }),
    new (winston.transports.File)({
      name: 'error-file',
      filename: 'filelog-error.log',
      level: 'error'
    })
  ]
});

希望这有帮助!