-
Notifications
You must be signed in to change notification settings - Fork 0
/
set-log-level.js
executable file
·131 lines (115 loc) · 3.64 KB
/
set-log-level.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
#!/usr/bin/env node
'use strict';
/* eslint-disable no-console */
const _ = require('lodash');
const assert = require('assert');
const cmdParse = require('minimist');
const debug = require('debug')('niveau:set-level');
const redis = require('redis');
const timeParse = require('timeparse');
const { LOG_CONFIG_KEY, readRedisOptions } = require('./lib/common');
const allOptions = {
url: 'l',
header: 'h',
ip: 'i',
expire: 'x',
reset: 'r',
help: undefined,
_: undefined
};
const cmdOptions = cmdParse(process.argv.slice(2), {
alias: _.omit(_.invert(allOptions), undefined)
});
debug('Command line options:', cmdOptions);
try {
execute(cmdOptions);
} catch (err) {
debug(err);
console.error(err.message);
process.exit(1);
}
function execute(cmdOptions) {
function noOptions(options) {
return Object.keys(options).length === 1 && options._.length === 0;
}
let validOptions = new Set(_.flatten(_.entries(allOptions)));
for (let opt in cmdOptions) {
assert(validOptions.has(opt),
`Invalid option ${opt}. Run 'set-log-level --help' to see usage.`);
}
if (cmdOptions.help || noOptions(cmdOptions)) {
console.log(`Usage: set-log-level [options...] <level>
Options:
-l, --url <regex> - matches request URL (without protocol, host, port)
-h, --header <name>:<regex> - matches given request header value
-i, --ip <regex> - matches sender IP address
-x, --expire <value> - expiration time with s/m/h suffix
-r, --reset - reset log level to default (do not provide level)
--help - print usage
<level> - log level to use for matching requests, supported values depend on your log library
`);
process.exit(1);
}
if (cmdOptions.reset) {
assert(
!cmdOptions._.length &&
!['url', 'header', 'ip', 'expire'].some(opt => opt in cmdOptions),
'No other options allowed with reset'
);
} else {
assert(cmdOptions._.length === 1,
"Provide exactly one log level. Run 'set-log-level --help' to see usage.");
var level = cmdOptions._[0];
}
if (_.isString(cmdOptions.header)) {
cmdOptions.header = [cmdOptions.header];
}
let headers = _.fromPairs(_.map(cmdOptions.header, h => {
let match = /^([^:]+):(.+)$/.exec(h);
assert(match && match.length === 3,
`Invalid header ${h}. Use the format <name>:<regex>`);
return [match[1].trim(), match[2].trim()];
}));
headers = _.isEmpty(headers) ? undefined : headers;
let expire;
if (cmdOptions.expire) {
if (!cmdOptions.expire.endsWith('r')) {
expire = timeParse(cmdOptions.expire, 's');
}
}
let request = {
url: cmdOptions.url,
ip: cmdOptions.ip,
headers
};
request = _.some(request, v => v !== undefined) ? request : undefined;
let logConfig = {
request,
// requestCounterKey: 'counter-name',
level
};
const client = redis.createClient(readRedisOptions());
client.on("error", function (err) {
console.error(err);
});
client.config('set', 'notify-keyspace-events', 'KA', (err, reply) => {
err ? console.error('notify-keyspace-events', err) :
console.log('notify-keyspace-events', reply);
if (cmdOptions.reset) {
debug('redis DEL %s', LOG_CONFIG_KEY);
client.del(LOG_CONFIG_KEY, (err, reply) => {
err ? console.error('redis DEL:', err) : debug('redis:', reply);
client.quit();
});
} else {
let value = JSON.stringify(logConfig);
debug('redis SET %s %s', LOG_CONFIG_KEY, value);
let params = [LOG_CONFIG_KEY, value];
expire && params.push('EX', expire);
client.set(params, (err, reply) => {
err ? console.error('redis SET:', err) : debug('redis:', reply);
client.quit();
});
}
});
}