1

正如我在博客中看到的许多日志,我发现 bunyan 适合进行日志记录,但它存在无法根据其级别记录到文件的问题。

下面是我正在关注的代码结构

const RotatingFileStream = require('bunyan-rotating-file-stream');
const bunyan = require('bunyan');

    var log = bunyan.createLogger({
          name: 'ShotPitch',


          streams: [{
            name: 'info',
            level: 'info',
            stream: new RotatingFileStream({
              path: 'info.%d-%b-%y.log',
              period: '1d', // daily rotation
              totalFiles: 10, // keep 10 back copies
              rotateExisting: true, // Give ourselves a clean file when we start up, based on period
              threshold: '10m', // Rotate log files larger than 10 megabytes
              totalSize: '20m', // Don't keep more than 20mb of archived log files
              gzip: true // Compress the archive log files to save space
            })
          }, {
            name: 'error',
            level: 'error',
            stream: new RotatingFileStream({
              path: 'error.%d-%b-%y.log',
              period: '1d', // daily rotation
              totalFiles: 10, // keep 10 back copies
              rotateExisting: true, // Give ourselves a clean file when we start up, based on period
              threshold: '10m', // Rotate log files larger than 10 megabytes
              totalSize: '20m', // Don't keep more than 20mb of archived log files
              gzip: true // Compress the archive log files to save space
            })
          }] 
        });

 log.info('Hello World');
 log.error('Hello World error log');

o/p:info.log:

{"name":"ShotPitch", "pid":7621,"level":30,"msg":"Hello World","time":"2017-09-03T18:29:04.181Z","v" :0}

{"name":"ShotPitch", "pid":7621,"level":50,"msg":"Hello World","time":"2017-09-03T18:29:04.181Z","v" :0}

o/p:error.log:

{"name":"ShotPitch", "pid":7621,"level":50,"msg":"Hello World","time":"2017-09-03T18:29:04.181Z","v" :0}

结论:

info.log 显示信息和错误日志

error.log 仅显示错误日志

我只希望 info.log 中的信息日志但无法做到。有没有人可以帮助?另外,如果告诉我如何更改为级别:“信息”而不是级别:30

4

2 回答 2

0

我遇到了同样的问题,最后我创建了 2 个变量,例如:

var debugLog = bunyan.createLogger({
  name: 'general',
  streams: [
    {
      level: 'debug',
      path: './debug.log'
    }]
});

var errLog = bunyan.createLogger({
  name: 'general',
  streams: [
    {
      level: 'error',
      path: './error.log'
    }]
});

debugLog.info('hello world');
errLog.error('error');

然后日志将位于不同的日志文件中。

于 2018-03-16T03:19:03.063 回答
0

配置时需要指定旋转文件流的日志级别bunyan

默认情况下,日志输出到标准输出并处于“信息”级别。

将记录器实例(或其流之一)设置为特定级别意味着记录该级别及更高级别的所有日志记录。例如,设置为“info”级别的记录器将记录级别 info 及更高级别的记录(警告、错误、致命)。

因此,错误日志也被收集到信息日志中。

于 2017-09-03T19:43:56.587 回答