[英]Logging in nodejs using bunyan logger, Print DEBUG, INFO, ERROR to same file
[英]Separate info and error logs 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:
{“ 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
配置bunyan
時,需要指定旋轉文件流的日志級別。
默認情況下,日志輸出為stdout並處於“ info”級別。
將記錄器實例(或其流之一)設置為特定級別意味着該級別及更高級別的所有日志記錄都已記錄。 例如,設置為“ info”級別的記錄器將記錄級別為info或更高級別的記錄(警告,錯誤,致命)。
錯誤日志因此也被收集到信息日志中。
我遇到了同樣的問題,最后我創建了兩個變量,例如:
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');
然后,日志將位於不同的日志文件中。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.