baidu-log-parser
v1.0.2
Published
Parse the log of baidu server
Downloads
3
Readme
baidu-log-parser
用于解析百度后端的日志。类似于这样的:
[2018-12-30 18:00:01.028] [INFO] swan - logid:229c391a-c0e7-4993-8963-a68bacb19866_DCSjv4fohve16_0 pool_info:{"spareResourceCapacity":0,"size":4,"available":4,"borrowed":0,"pending":0,"max":4,"min":2} request:{"renderInfo":{"clientName":"puffer","moduleName":"lottery_views","onlyData":true,"needCommands":true}}
如果你们服务器的日志格式和这个差不多,但是此仓库却无法解析,请联系 [email protected]
。
Quick Start
npm install -g baidu-log-parser
cat node.log.2018123018 | parse-baidu-log
解析单行
把单行 Log 解析成一个JS对象。
const LineParser = require('baidu-log-parser/line-parser');
new LineParser(
'[2018-12-30 18:00:01.027] [INFO] swan - [Perf][before ssr run .3][pid:14232],times: 1546164001027'
).parse();
/* 输出: {
date: 2018-12-30T10:00:01.027Z,
level: 'INFO',
name: 'swan',
fields: {},
text: '[Perf][before ssr run .3][pid:14232],times: 1546164001027'
} */
new LineParser(
`[2018-12-30 18:00:01.028] [INFO] swan - logid:229c391a-c0e7-4993-8963-a68bacb19866_DCSjv4fohve16_0 pool_info:{"spareResourceCapacity":0,"size":4,"available":4,"borrowed":0,"pending":0,"max":4,"min":2} request:{"renderInfo":{"clientName":"puffer","moduleName":"lottery_views","onlyData":true,"needCommands":true}}`
).parse();
/* 输出: {
date: 2018-12-30T10:00:01.028Z,
level: 'INFO',
name: 'swan',
fields: {
logid: '229c391a-c0e7-4993-8963-a68bacb19866_DCSjv4fohve16_0',
pool_info: {
spareResourceCapacity: 0,
size: 4,
available: 4,
borrowed: 0,
pending: 0,
max: 4,
min: 2
},
request: { renderInfo: [Object] }
},
text: ''
}*/
解析文件(或任意一个可读流)
从一个日志文件中逐行读取出JS对象。
const Fs = require('fs');
const StreamParser = require('baidu-log-parser/stream-parser');
const log = new StreamParser(
Fs.createReadStream('./node.log.2018123018')
);
(async function (){
let logInfo = null;
while (logInfo = await log.parseLine()) {
// ...
}
})();
在命令行中快速使用
可以在命令行中方便的将 stdin 的内容转换为JSON后 stdout 输出。
# 全局安装
npm install -g baidu-log-parser
# 将log文件按JSON格式输出
cat node.log.2018123018 | parse-baidu-log
cat node.log.2018123018 | grep '9031bd62-1340-4264-930f-3e9e903067c8_DCSiunsqxt09j_0' | parse-baidu-log
tail -f node.log.2018123018 | parse-baidu-log
该命令实际上就是将 process.stdin
传入 StreamParser
,源代码差不多是这样的:
#!/usr/bin/env node
const StreamParser = require('../stream-parser');
const log = new StreamParser(process.stdin);
(async function () {
let logInfo = null;
while (logInfo = await log.parseLine()) {
console.log(JSON.stringify(logInfo, null, ' '), '\n', '\n');
}
})();