-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.js
142 lines (119 loc) · 3.14 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
'use strict'
const split = require('split2')
const pump = require('pump')
const through = require('through2')
const assign = require('deep-assign')
const jsonParser = require('fast-json-parse')
const defaults = {
levels: {
'*': 'info'
},
values: {
fatal: 60,
error: 50,
warn: 40,
info: 30,
debug: 20,
trace: 10
}
}
let config = defaults
const internals = {
getFilter (name) {
if (!name) return '*'
if (name.includes('*') === false) return name
if (name === '*') return name
return '~' + name.split('*')[0]
},
transport (chunk, enc, cb) {
if (!chunk.name) {
this.stream.write(JSON.stringify(chunk))
return cb()
}
const logName = chunk.name
const logLevelName = this.levels[logName] || 'info'
const logLevelValue = this.values[logLevelName]
const filter = this.getFilter(chunk.name)
if (filter === '*' && chunk.level >= logLevelValue) {
this.stream.write(JSON.stringify(chunk))
return cb()
}
for (let i = 0; i < this.filters.length; i += 1) {
const filter = this.filters[i]
if (filter === '*' && chunk.level >= logLevelValue) {
this.stream.write(JSON.stringify(chunk))
break
}
if (filter[0] === '~' && logName.startsWith(filter.substr(1))) {
if (chunk.level >= logLevelValue) this.stream.write(JSON.stringify(chunk))
break
}
if (filter === logName && chunk.level >= logLevelValue) {
this.stream.write(JSON.stringify(chunk))
break
}
}
cb()
},
stream: process.stdout
}
Object.defineProperty(internals, 'filters', {
set (input) {
if (Array.isArray(input) === false) {
this._filters = [this.getFilter(input)]
return
}
this._filters = []
input.forEach((f) => {
const newF = this.getFilter(f)
this._filters.push(newF)
})
},
get () { return this._filters }
})
Object.defineProperty(internals, 'levels', {
set (input) {
this._levels = input
},
get () { return this._levels || config.levels }
})
Object.defineProperty(internals, 'values', {
set (input) {
this._values = input
},
get () { return this._values || config.values }
})
function loadConfig () {
let result = defaults
try {
const userConfig = require(process.argv[2])
result = assign({}, defaults, userConfig)
} catch (e) {
console.error('could not load specified config: %s', e.message)
process.exit(1)
}
return result
}
function parser (input) {
const result = jsonParser(input)
if (result.err) return
return result.value
}
// We are being run as a "binary"
if (require.main === module) {
if (process.argv.length === 3) {
config = loadConfig()
}
internals.filters = Object.keys(config.levels)
internals.levels = config.levels
internals.values = config.values
process.on('SIGHUP', () => {
config = loadConfig()
internals.filters = Object.keys(config.levels)
internals.levels = config.levels
internals.values = config.values
})
const myTransport = through.obj(internals.transport.bind(internals))
pump(process.stdin, split(parser), myTransport)
}
module.exports.internals = internals