-
Notifications
You must be signed in to change notification settings - Fork 6
/
app.js
78 lines (67 loc) · 1.83 KB
/
app.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
const pm2 = require('pm2');
const pmx = require('pmx'); // docs say pmx is deprecated, module:generate still uses it, I confuze
const gelflog = require('gelf-pro');
const conf = pmx.initModule({
widget: {
logo: 'https://app.keymetrics.io/img/logo/keymetrics-300.png',
theme: ['#141A1F', '#222222', '#3ff', '#3ff'],
},
});
gelflog.setConfig({
adapterName: conf.gelfAdapterName,
adapterOptions: {
host: conf.graylogHost,
port: conf.graylogPort,
}
});
if (conf.graylogFields) {
try {
const fields = JSON.parse(conf.graylogFields);
gelflog.setConfig({ fields });
} catch (ex) {
console.log(`Could not parse JSON ${ex}`); // eslint-disable-line no-console
}
}
function getLogData(data) {
data=data.trim().replace(/\n/g, "\\\\n").replace(/\r/g, "\\\\r").replace(/\t/g, "\\\\t");
try {
json = JSON.parse(data);
if (json.message.trim().length === 0) {
return "";
}
return json;
} catch(err) {
return data;
}
}
pm2.Client.launchBus((err, bus) => {
if (err) return;
bus.on('log:out', (log) => {
if (log.process.name === 'pm2-gelf-pro') return;
data = getLogData(log.data);
if (typeof data === 'string'){
if (data.length == 0) return;
gelflog.info(data, {application_name: log.process.name});
} else {
data['application_name'] = log.process.name;
gelflog.info(data.message, data);
}
});
bus.on('log:err', (log) => {
if (log.process.name === 'pm2-gelf-pro') return;
data = getLogData(log.data);
if (typeof data === 'string'){
if (data.length == 0) return;
gelflog.info(data, {application_name: log.process.name});
} else {
data['application_name'] = log.process.name;
gelflog.info(data.message, data);
}
});
bus.on('close', () => {
pm2.disconnectBus();
});
});
module.exports = {
gelflog,
};