forked from electrode-io/electrode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlog-parser.ts
117 lines (104 loc) · 2.1 KB
/
log-parser.ts
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
/* eslint-disable @typescript-eslint/no-var-requires */
/* eslint-disable max-statements, complexity */
const AnsiConvert = require("ansi-to-html");
const convert = new AnsiConvert();
const BunyanLevelLookup = {
60: "error",
50: "error",
40: "warn",
30: "info",
20: "debug",
10: "silly"
};
const tagLevelMap = {
"warn:": "warn",
"error:": "error",
fail: "error",
rejection: "error",
unhandled: "error",
exception: "error",
"debugger listening on": "silly"
};
export function parse(str: string, last: any) {
let jsonData;
let show;
try {
if (str[0] === "{" || str[0] === "[") {
jsonData = JSON.parse(str);
}
} catch {
//
}
let message;
let level;
if (jsonData) {
level = BunyanLevelLookup[jsonData.level];
message = jsonData.msg || jsonData.message;
if (level === "warn" || level === "error") {
show = 2;
}
}
if (!level) {
const match = str.match(
/warn\:|error\:|fail|rejection|unhandled|exception|debugger listening on/i
);
if (match) {
const tag = match[0].toLowerCase();
if (!level) {
level = tagLevelMap[tag];
}
show = tag === "debugger listening on" ? 1 : 2;
}
}
const entry: any = {
level: level || "info",
ts: Date.now(),
message: message || str,
json: jsonData,
show
};
if (last && entry.ts === last.ts) {
entry.tx = (last.tx || 0) + 1;
}
return entry;
}
const Levels = {
error: {
color: "red",
index: 0,
name: "error"
},
warn: {
color: "yellow",
index: 1,
name: "warn"
},
info: {
index: 2,
name: "info"
},
http: {
index: 3,
name: "http"
},
verbose: {
index: 4,
name: "verbose"
},
debug: {
index: 5,
name: "debug"
},
silly: {
index: 6,
name: "silly"
}
};
export function getLogEventAsHtml(event) {
const levelInfo = Levels[event.level];
const levelName = levelInfo.name.substring(0, 4);
const name = levelInfo.color
? `<span style="color: ${levelInfo.color}">${levelName}</span>`
: levelName;
return `${name}: ${convert.toHtml(event.message)}`;
}