-
Notifications
You must be signed in to change notification settings - Fork 0
/
pinot.js
executable file
·151 lines (129 loc) · 3.44 KB
/
pinot.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
const chalk = require('chalk');
const fs = require('fs');
const path = require('path')
const showHeaders = process.argv.find(arg => arg === "-h" | arg === "--show-headers") !== undefined
const showErrors = process.argv.find(arg => arg === "-e" | arg === "--show-errors") !== undefined
// Set true to enable debugging
const showInternalErrors = false
const pinoLevels = {
0: "ALL",
10: "TRACE",
20: "DEBUG",
30: "INFO",
40: "WARN",
50: "ERROR",
60: "FATAL",
100: "OFF",
}
const processColors = {}
let lastProcessColor = 0
const foregroundColors = [
"red",
"green",
"yellow",
"blue",
"magenta",
"cyan",
"white",
"gray",
"redBright",
"greenBright",
"yellowBright",
"blueBright",
"magentaBright",
"cyanBright",
"whiteBright"
]
class PinoT {
constructor({showHeaders, showErrors, showInternalErrors}) {
this.showHeaders = showHeaders
this.showErrors = showErrors
this.showInternalErrors = showInternalErrors
}
processLine(line) {
try {
let object = JSON.parse(line)
if (object.err) {
this.printErrorObject(object)
}
this.printHTTPObject(object)
} catch (error) {
this.printError(error)
this.printRawLine(line)
}
}
// Print javascript (this process)
printError(error) {
if (!showInternalErrors) {
return
}
if (error.message.indexOf("JSON") === -1) {
console.log(error.toString())
}
}
// Print lines that could not be parsed as JSON objecd
printRawLine(line) {
if (!showErrors || !line.length) {
return
}
console.log(line)
}
// Print error objects parsed from JSON string
printErrorObject(object) {
if (!showErrors) {
return
}
process.stderr.write(JSON.stringify(object, null, 2))
process.stderr.write("\n")
}
// Print http objects parsed from JSON string
printHTTPObject(object) {
const level = pinoLevels[object.level]
const time = new Date(object.time).toISOString()
const processInfo = this.getProcessInfo(object)
const statusCode = this.getStatusCode(object)
const method = object.req.method.padEnd(5)
const url = object.req.url
const responseTime = object.responseTime || "-"
const output = `[${time}] ${level} ${processInfo} ${statusCode} ${method} ${url} ${responseTime} ms\n`
process.stdout.write(output)
if (showHeaders) {
this.printHeaders(object)
}
}
getStatusCode(object) {
const statusCode = object.res.statusCode;
const color = object.res.statusCode < 400 ? "green" : "red"
return `${chalk[color](object.res.statusCode)}`.padEnd(3)
}
printHeaders(object) {
const headers = object.req.headers
Object.keys(headers).forEach(key => {
process.stdout.write(` ${key}: ${headers[key]}\n`)
})
}
getProcessInfo(object) {
const name = object.name
const pid = object.pid
const processInfo= `${name}/${pid}`
const color = this.getProcessColor(name)
return `(${chalk[color](processInfo)}):`.padEnd(40)
}
getProcessColor(processName) {
let color = processColors[processName]
if (!color) {
let colorIndex = this.hashCode(processName)
color = foregroundColors[colorIndex % foregroundColors.length]
processColors[processName] = color
}
return color
}
hashCode(s) {
var h = 0, l = s.length, i = 0;
if ( l > 0 )
while (i < l)
h = (h << 5) - h + s.charCodeAt(i++) | 0
return Math.abs(h)
}
}
module.exports = PinoT