-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathelastic-search-logger.js
116 lines (97 loc) · 3.93 KB
/
elastic-search-logger.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
module.exports = function (RED) {
'use strict';
function LogElasticLoggerNode(config) {
let winston = require('winston');
let winstonElasticSearch = require('winston-elasticsearch');
RED.nodes.createNode(this, config);
this.logger = null;
let transports = [];
// Elastic settings
let url = RED.util.evaluateNodeProperty(config.url, config.urlType, this);
if (url == '') {
this.error('Elastic search url is not set');
}
let user = RED.util.evaluateNodeProperty(this.credentials.username, config.usernameType, this);
if (user == '') {
this.error('Elastic search username is not set');
}
let password = RED.util.evaluateNodeProperty(this.credentials.password, config.passwordType, this);
if (password == '') {
this.error('Elastic search password is not set');
}
let index = RED.util.evaluateNodeProperty(this.credentials.index, config.indexType, this);
if (index == '') {
this.error('Elastic search index is not set');
}
index = index.toLowerCase();
if (url) {
const elasticSearchTransport = new winstonElasticSearch.ElasticsearchTransport({
clientOpts: {
node: url,
auth: {
username: user,
password: password,
},
ssl: {
// accept any
rejectUnauthorized: false,
},
},
transformer: (logData) => setElasticFields(logData, this),
index: index,
});
transports.push(elasticSearchTransport);
elasticSearchTransport.on('error', (error) => {
this.error(`Error in elasticSearchTransport caught: ${error.message}`);
console.error('Error in elasticSearchTransport caught', error);
});
}
let logLevels = {
levels: {
Error: 0,
Warning: 1,
Information: 2,
Debug: 3,
},
};
this.logger = new winston.createLogger({
exitOnError: false,
level: 'Debug',
levels: logLevels.levels,
transports: transports,
});
this.debug('elastic-search logger created');
this.on('close', function (removed, done) {
// close logger
if (this.loggger) {
this.logger.close();
}
this.debug('elastic-search logger closed');
if (done) done();
});
}
function setElasticFields(logData, node) {
let { ElasticsearchTransformer } = require('winston-elasticsearch');
const transformed = ElasticsearchTransformer(logData);
transformed['@timestamp'] = logData.timestamp ? logData.timestamp : new Date().toISOString();
transformed.message = logData.message;
transformed.messageTemplate = logData.messageTemplate;
transformed.severity = logData.level;
transformed.level = logData.level;
transformed.fields = logData.meta;
if (logData.meta['transaction.id']) transformed.transaction = { id: logData.meta['transaction.id'] };
if (logData.meta['trace.id']) transformed.trace = { id: logData.meta['trace.id'] };
if (logData.meta['span.id']) transformed.span = { id: logData.meta['span.id'] };
return transformed;
}
RED.nodes.registerType('elastic-search-logger', LogElasticLoggerNode, {
credentials: {
username: { type: 'text' },
password: { type: 'password' },
index: { type: 'text' },
},
});
LogElasticLoggerNode.prototype.addToLog = function addTolog(loglevel, msg) {
this.logger.log(loglevel, msg.payload.message, msg.payload.meta);
};
};