forked from pinojs/pino
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pretty.js
executable file
Β·139 lines (115 loc) Β· 2.76 KB
/
pretty.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
'use strict'
var split = require('split2')
var Parse = require('fast-json-parse')
var chalk = require('chalk')
var levels = {
default: 'USERLVL',
60: 'FATAL',
50: 'ERROR',
40: 'WARN',
30: 'INFO',
20: 'DEBUG',
10: 'TRACE'
}
var standardKeys = [
'pid',
'hostname',
'name',
'level',
'msg',
'time',
'v'
]
function withSpaces (value) {
var lines = value.split('\n')
for (var i = 1; i < lines.length; i++) {
lines[i] = ' ' + lines[i]
}
return lines.join('\n')
}
function filter (value) {
var keys = Object.keys(value)
var result = ''
for (var i = 0; i < keys.length; i++) {
if (standardKeys.indexOf(keys[i]) < 0) {
result += ' ' + keys[i] + ': ' + withSpaces(JSON.stringify(value[keys[i]], null, 2)) + '\n'
}
}
return result
}
function isPinoLine (line) {
return line &&
line.hasOwnProperty('hostname') &&
line.hasOwnProperty('pid') &&
(line.hasOwnProperty('v') && line.v === 1)
}
function pretty (opts) {
var timeTransOnly = opts && opts.timeTransOnly
var formatter = opts && opts.formatter
var levelFirst = opts && opts.levelFirst
var stream = split(mapLine)
var ctx
var levelColors
var pipe = stream.pipe
stream.pipe = function (dest, opts) {
ctx = new chalk.constructor({
enabled: !!(chalk.supportsColor && dest.isTTY)
})
levelColors = {
default: ctx.white,
60: ctx.bgRed,
50: ctx.red,
40: ctx.yellow,
30: ctx.green,
20: ctx.blue,
10: ctx.grey
}
pipe.call(stream, dest, opts)
}
return stream
function mapLine (line) {
var parsed = new Parse(line)
var value = parsed.value
if (parsed.err || !isPinoLine(value)) {
// pass through
return line + '\n'
}
if (formatter) {
return opts.formatter(parsed.value) + '\n'
}
if (timeTransOnly) {
value.time = asISODate(value.time)
return JSON.stringify(value) + '\n'
}
line = (levelFirst)
? asColoredLevel(value) + ' [' + asISODate(value.time) + ']'
: '[' + asISODate(value.time) + '] ' + asColoredLevel(value)
line += ' ('
if (value.name) {
line += value.name + '/'
}
line += value.pid + ' on ' + value.hostname + ')'
line += ': '
if (value.msg) {
line += ctx.cyan(value.msg)
}
line += '\n'
if (value.type === 'Error') {
line += ' ' + withSpaces(value.stack) + '\n'
} else {
line += filter(value)
}
return line
}
function asISODate (time) {
return new Date(time).toISOString()
}
function asColoredLevel (value) {
if (levelColors.hasOwnProperty(value.level)) {
return levelColors[value.level](levels[value.level])
} else {
return levelColors.default(levels.default)
}
}
}
module.exports = pretty