forked from alibaba/flutter-go
-
Notifications
You must be signed in to change notification settings - Fork 1
/
parsLog.js
70 lines (62 loc) · 2.17 KB
/
parsLog.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
var path = require('path'); //系统路径模块
var fs = require('fs'); //文件模块
var http = require('http');
var child_process = require('child_process');
var file = path.join(__dirname, './log.json'); //文件路径,__dirname为当前运行js文件的目录
//读取json文件
fs.readFile(file, 'utf-8', function(err, data) {
if (err) {
res.send('文件读取失败');
} else {
// res.send(data);
if(data) {
const obj = JSON.parse(data);
const newObj ={};
obj.pr_req.map((item,i)=>{
const key = encodeURIComponent(item.message);
newObj[key] ={date: new Date(item.date).toLocaleDateString(),msg:item.message};
})
const newObjCategory = Object.values(newObj);
const categorys ={};
newObjCategory.map((item)=>{
if(!categorys[item.date]) {
categorys[item.date] =[];
}else {
categorys[item.date].push(item.msg);
}
})
//console.log('----------->',categorys);
rendHtml(categorys);
}
}
});
function rendHtml(categorys){
http.createServer(function (req, res) {
var html = buildHtml(categorys);
res.writeHead(200, {
'Content-Type': 'text/html;charset=utf-8',
//'Content-Length': html.length,
'Expires': new Date().toUTCString()
});
res.end(html);
}).listen(8889,"127.0.0.1");
console.log("Server running at http://127.0.0.1:8889/");
child_process.exec(`start "http://127.0.0.1:8080"`);
function buildHtml(obj) {
var header = '';
var body = '';
var content = '';
for(var i in obj) {
let liContent = `<ul style="list-style-type:none">#### ${i}`;
const list = obj[i];
list.map((it,index)=>{
liContent += `<li>  - [x] ${it}</li>`
})
liContent += `</ul>`;
content += liContent;
}
// console.log('content===>',content)
return '<!DOCTYPE html>'
+ '<html><header>' + header + '</header><body>' + content + '</body></html>';
};
}