sails.js中多个文件的多个记录器级别

时间:2015-11-27 13:53:04

标签: sails.js winston

我想为每个日志级别创建单独的文件,它应该存储相应的文件。说,

merge

它应存储在info.log文件中。

sails.log.info('Info log');

在config / log.js

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

我的问题是终端中没有显示某些日志,而info.log和debug.log文件不仅分别包含信息和调试日志。

如何实现这一目标?

1 个答案:

答案 0 :(得分:1)

好吧,看起来Winston根据数字级别写入日志。因此,错误消息将始终写入信息日志,但信息永远不会记录错误。

所以我认为最好将config / bootstrap.js中的不同实例分开:

sails.warnLog = new (winston.Logger)({
      transports: [
        new (winston.transports.Sentry)({
          name: 'warn-Sentry',
          dsn: '{my account dsn}',
          patchGlobal: true
          level: 'warn'
        })
      ]});

sails.infLog = new (winston.Logger)({
      transports: [
        new (winston.transports.Loggly)({
          name: 'info-Loggly',
          subdomain: '{my subdomain}',
          inputToken: '{my input token}'
          level: 'info'
        })
      ]});

sails.verbLog = new (winston.Logger)({
      transports: [
        new (winston.transports.Console)({
          name: 'verb-console',
          timestamp: true,
          prettyPrint: true,
          colorize: true,
          level: 'verbose'
        })
      ]});

然后你可以写日志:

sails.warnLog.warn('Warning');
sails.infLog.info('Information');
sails.verbLog.verbose('Verbose');