forked from ovhemert/pino-papertrail
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cli.js
81 lines (69 loc) · 1.46 KB
/
cli.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
#! /usr/bin/env node
'use strict'
const fs = require('fs')
const path = require('path')
const minimist = require('minimist')
const pumpify = require('pumpify')
const pinoPapertrail = require('./lib/pino-papertrail')
const pkg = require('./package.json')
const options = {
alias: {
version: 'v',
help: 'h',
echo: 'e',
host: 'H',
port: 'p',
connection: 'c',
appname: 'a',
'message-only': 'm'
},
default: {
appname: 'pino',
echo: true,
host: 'localhost',
port: '1234',
connection: 'udp',
'message-only': false,
'sigterm-exit': true,
'pipe-exit': true
},
boolean: [
'echo',
'message-only',
'sigterm-exit',
'pipe-exit'
]
}
const argv = minimist(process.argv.slice(2), options)
if (argv.help) {
console.log(fs.readFileSync(path.join(__dirname, './usage.txt'), 'utf8'))
process.exit(0)
}
if (argv.version) {
console.log(`${pkg.name} v${pkg.version}`)
process.exit(0)
}
const parseJson = pinoPapertrail.parseJson()
const toSyslog = pinoPapertrail.toSyslog(argv)
const papertrail = pinoPapertrail.toPapertrail(argv)
function shutdown () {
try {
papertrail.close()
} catch (e) {
process.exit()
}
}
process.on('SIGTERM', function () {
if(!argv['sigterm-exit']) {
return
}
shutdown()
})
process.stdin.on('end', function () {
if(!argv['pipe-exit']) {
return
}
shutdown()
})
const pipeline = pumpify(parseJson, toSyslog, papertrail)
process.stdin.pipe(pipeline)