2017-09-03 64 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 –