单独的信息和错误日志Bunyan [英] Separate info and error logs bunyan
问题描述
正如我在博客中看到的许多日志一样,我发现Bunyan适合记录日志,但是存在一个问题,即它无法根据其级别记录到文件中.
As I have seen many logs in blogs and I find bunyan suitable for logging but there is problem with it that it can't log to file according to their level.
下面是我要遵循的代码结构
Below is code structure I am following
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:
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":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}
{"name":"ShotPitch", "pid":7621,"level":50,"msg":"Hello World","time":"2017-09-03T18:29:04.181Z","v":0}
o/p:error.log:
o/p: error.log :
{"name":"ShotPitch","pid":7621,"level":50,"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}
结论:
info.log同时显示信息和错误日志
info.log shows both info and error logs
error.log仅显示错误日志
error.log shows only error logs
我只希望信息日志记录在info.log中,但无法执行.有没有人可以帮助您?另外,如果告诉我如何更改为级别:信息"而不是级别:30
I want info logs only in info.log but unable to do. Is there anyone that can help ?. Also if tell me how to change to level: "info" rather than level:30
推荐答案
我遇到了同样的问题,最后我创建了两个变量,如:
I met the same problem, finally I created 2 variables like:
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');
然后,日志将位于不同的日志文件中.
Then logs will be in different log files.
这篇关于单独的信息和错误日志Bunyan的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!