2017-09-03 61 views
1

正如我在博客中看到许多日志,我发现bunyan适合于日志记录,但有问题,它无法根据它们的级别登录到文件。单独的信息和错误日志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建立:

{ “名称”: “ShotPitch”, “PID”:7621, “电平”: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。日志:

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

结论:

info.log建立同时显示信息和错误日志

error.log中只显示错误日志

我想信息记录只在info.log建立但无法做到。有没有人可以帮忙?另外,如果告诉我如何更改级别:“info”而不是级别:30

回答

0

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

默认情况下,日志输出是stdout和“info”级别。

记录器实例(或它的流之一)设置为特定级别意味着所有的日志记录,在该水平以上将被记录。例如。设置为“信息”级别的记录器将记录等级信息及以上的记录(警告,错误,致命)。

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

+0

我不能为单独的日志记录制作单独的实例。如果可能的话,单个实例对我来说可以。此外,如果级别设置为信息的字符串,错误,那么读取日志将更加可行。 – Creator

+0

您只能设置严重级别。在这里查看严重性级别https://github.com/trentm/node-bunyan#levels –