-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
121 lines (101 loc) · 2.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
'use strict';
const readline = require('readline');
const prettyjson = require('prettyjson');
const { DateTime } = require('luxon');
const chalk = require('chalk');
const levelFormat = {
0: chalk.bgBlackBright.white,
10: chalk.bgBlackBright.white,
20: chalk.bgBlackBright.white,
30: chalk.bgBlue.black,
40: chalk.bgYellow.black,
50: chalk.bgRed.black,
60: chalk.bgRed.black,
};
const textLevelFormats = {
0: chalk.blackBright,
10: chalk.blackBright,
20: chalk.blackBright,
30: chalk.blue,
40: chalk.yellow,
50: chalk.red,
60: chalk.red,
};
const levelName = {
0: 'LINE',
10: 'TRACE',
20: 'DEBUG',
30: 'INFO',
40: 'WARN',
50: 'ERROR',
60: 'FATAL',
};
const labelLevelFormat = (level, title = null) => {
return levelFormat[level](` ${ title || levelName[level]} `);
};
const textLevelFormat = (level, title) => {
return textLevelFormats[level](` ${ title } `);
};
const rl = readline.createInterface({
input: process.stdin,
});
rl.on('line', (line) => {
let parse = {};
try {
parse = JSON.parse(line);
} catch (e) {
return process.stdout.write(labelLevelFormat(0) + ` ${line}\n\n`);
}
const {
level,
pid,
name,
hostname,
time,
msg,
...object
} = parse;
if (!level) {
return process.stdout.write(labelLevelFormat(0) + ` ${line}\n\n`);
}
try {
if (object.data) {
const a = object.data.replace(/'/g, '"').replace(/(\s*?{\s*?|\s*?,\s*?)(['"])?(\w+)(['"])?:/g, '$1"$3":');
object.data = JSON.parse(a);
}
} catch (e) {
// console.error(e);
}
try {
if (object.cookies) {
object.cookies = JSON.parse(object.cookies);
}
} catch (e) {
// console.error(e);
}
try {
if (object.error.stack) {
object.error.stack = String(object.error.stack)
.split('\n')
.reduce((o,v,k) => {
o[k] = v;
return o;
}, {});
}
} catch (e) {
// console.error(e);
}
const head = [];
head.push(labelLevelFormat(level, pid));
head.push(labelLevelFormat(level));
try {
head.push(labelLevelFormat(level, DateTime.fromISO(time).setLocale('ru').toLocaleString(DateTime.DATETIME_SHORT)));
} catch (e) {
// console.error(e);
}
head.push(textLevelFormat(level, `${hostname}: ${msg}`));
const message = [];
message.push(head.join(' '));
message.push(prettyjson.render(object));
process.stdout.write(message.join('\n') + '\n\n');
});