-
Notifications
You must be signed in to change notification settings - Fork 3
/
index.js
300 lines (243 loc) · 8.66 KB
/
index.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
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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
const prettyBytes = require('prettier-bytes');
const jsonParse = require('fast-json-parse');
const hasUnicode = require('has-unicode')();
const prettyMs = require('pretty-ms');
const padLeft = require('pad-left');
const chalk = require('chalk');
const yaml = require('js-yaml');
const nl = process.platform === 'win32' ? '\r\n' : '\n';
const emojiLog = {
fatal: '💀',
error: '🚨',
warn: '⚠️',
info: '✨',
debug: '🐛',
trace: '🔍'
};
const emojiMark = {
error: '🧨',
stack: '💥',
};
const indentMark = chalk.gray('|');
const anynl = /\r?\n/;
function isWideEmoji(character) {
return character !== '⚠️'
}
module.exports = MiamiVice;
function MiamiVice() {
return parse;
function parse(lineOrRecord) {
function unhandled() {
return lineOrRecord + nl
}
let record;
if (typeof lineOrRecord === 'string') {
let obj = jsonParse(lineOrRecord);
if (!obj.value || obj.err) return unhandled();
record = obj.value;
}
else if (typeof lineOrRecord === 'object' && lineOrRecord['v']) {
record = lineOrRecord;
}
else {
return unhandled();
}
if (!record.level) return unhandled();
if (typeof record.level === 'number') convertLogNumber(record);
return output(record) + nl
}
function extract(obj, ...props) {
let val = undefined;
for (const prop of props) {
val = (val !== undefined) ? val : obj[prop];
delete obj[prop];
}
return val;
}
function convertLogNumber(obj) {
if (obj.level === 10) obj.level = 'trace';
if (obj.level === 20) obj.level = 'debug';
if (obj.level === 30) obj.level = 'info';
if (obj.level === 40) obj.level = 'warn';
if (obj.level === 50) obj.level = 'error';
if (obj.level === 60) obj.level = 'fatal'
}
function output(obj) {
const output = [];
if (!obj.level) obj.level = 'userlvl';
if (!obj.name) obj.name = '';
if (!obj.ns) obj.ns = '';
const level = extract(obj, 'level');
output.push(formatDate(extract(obj, 'time')));
output.push(formatLevel(level));
output.push(formatNs(extract(obj, 'ns')));
output.push(formatName(extract(obj, 'name')));
const reqId = extract(obj, 'reqId');
if (reqId) {
output.push(chalk.blueBright(`[${reqId}]`));
}
const msg = extract(obj, 'message', 'msg') || '_';
output.push(formatMessage(msg, level));
/*const pid = */extract(obj, 'pid');
/*const hostname = */extract(obj, 'hostname');
/*const v = */extract(obj, 'v');
const req = extract(obj, 'req');
const res = extract(obj, 'res');
const statusCode = (res) ? res.statusCode : extract(obj, 'statusCode');
const responseTime = extract(obj, 'responseTime', 'elapsed');
const method = (req) ? req.method : extract(obj, 'method');
const contentLength = extract(obj, 'contentLength');
const url = (req) ? req.url : extract(obj, 'url');
if (method != null) {
output.push(formatMethod(method))
}
if (statusCode != null) {
output.push(formatStatusCode(statusCode))
}
if (url != null) output.push(formatUrl(url));
if (contentLength != null) output.push(formatBundleSize(contentLength));
if (responseTime != null) output.push(formatLoadTime(responseTime));
let err = extract(obj, 'err', 'error');
let trace = extract(obj, 'trace', 'stack');
let detailLines = [];
if (level !== 'info') {
const details = yaml.safeDump(obj, {skipInvalid: true, flowLevel: 0}).trimEnd();
if (details.length < 160) {
output.push(chalk.gray(details));
}
else {
detailLines = yaml.safeDump(obj, {skipInvalid: true})
.split(anynl)
.filter(noEmpty)
.map(indent)
.map((line) => chalk.gray(line));
}
}
let lines = [output.filter(noEmpty).join(' '), ...detailLines];
if (err) {
err = Object.assign({}, err);
trace = extract(err, 'trace', 'stack') || trace;
lines.push(...formatError(err, trace, msg));
}
if (trace) {
lines.push(...formatTrace(trace, err));
}
return lines.filter(line => line.trim()).join(nl);
}
function formatError(err, trace, msg) {
trace = trace instanceof Array ? trace[0] : (trace || '');
const errNameMatch = trace.match(errorNameRegex);
let errName = 'Error';
if (errNameMatch) {
errName = errNameMatch[1];
if (errName === err.type) {
extract(err, 'type');
}
}
if (err.name === errName) {
extract(err, 'name');
}
if (msg && msg.includes(err.message)) {
extract(err, 'message');
}
if (msg && msg.includes(err.msg)) {
extract(err, 'msg');
}
const errLines = yaml.safeDump(err, { skipInvalid: true })
.split(anynl).map(errLine => ' ' + errLine);
errLines.unshift(`${formatMark(emojiMark.error)} ${errName}:`);
return errLines
.filter(errLine => errLine.trim())
.map(indent);
}
function formatTrace(trace, err) {
if (!(trace instanceof Array)) {
trace = trace.toString().split(anynl);
}
let res = [];
const errNameMatch = trace[0].match(errorNameRegex);
if (!err) {
let errName = errNameMatch ? errNameMatch[1] : 'Error';
res.push(`${formatMark(emojiMark.error)} ${errName}:`);
}
if (errNameMatch) {
trace.shift();
}
res.push(`${formatMark(emojiMark.stack)} Stack trace:`);
res.push(...trace);
return res.map(indent);
}
function formatDate(time) {
const date = new Date(time);
const hours = padLeft(date.getHours().toString(), 2, '0');
const minutes = padLeft(date.getMinutes().toString(), 2, '0');
const seconds = padLeft(date.getSeconds().toString(), 2, '0');
const prettyDate = hours + ':' + minutes + ':' + seconds;
return chalk.gray(prettyDate)
}
function formatLevel(level) {
if (!hasUnicode) return formatMessage(level, level);
const emoji = emojiLog[level];
const padding = isWideEmoji(emoji) ? '' : ' ';
return emoji + padding;
}
function formatNs(name) {
return chalk.cyan(name)
}
function formatName(name) {
return chalk.blue(name)
}
function formatMessage(message, level) {
const msg = formatMessageName(message);
let pretty;
if (level === 'error') pretty = chalk.red(msg);
if (level === 'trace') pretty = chalk.white(msg);
if (level === 'warn') pretty = chalk.magenta(msg);
if (level === 'debug') pretty = chalk.yellow(msg);
if (level === 'info' || level === 'userlvl') pretty = chalk.green(msg);
if (level === 'fatal') pretty = chalk.white.bgRed(msg);
const lines = pretty.split(anynl);
if (lines.length === 1) {
return pretty;
}
return [lines[0], ...lines.slice(1).map(indentPlus)].join(nl);
}
function indent(line) {
return padLeft('', 9, ' ') + indentMark + line;
}
function indentPlus(line) {
return indent(' ' + line);
}
function formatUrl(url) {
return chalk.white(url)
}
function formatMethod(method) {
return chalk.white(method)
}
function formatStatusCode(statusCode) {
statusCode = statusCode || 'xxx';
return chalk.white(statusCode)
}
function formatLoadTime(elapsedTime) {
const elapsed = parseInt(elapsedTime, 10);
const time = prettyMs(elapsed);
return chalk.gray(time)
}
function formatBundleSize(bundle) {
const bytes = parseInt(bundle, 10);
const size = prettyBytes(bytes).replace(/ /, '');
return chalk.gray(size)
}
function formatMessageName(message) {
if (message === 'request' || message === 'incoming request') return '<--';
if (message === 'response' || message === 'request completed') return '-->';
return message
}
function formatMark(mark) {
return hasUnicode ? mark : '';
}
function noEmpty(val) {
return !!val
}
}
const errorNameRegex = /^((\w*)(Error|Exception)):/;