@5minds/node-red-contrib-processcube-elasticsearch 0.2.0-feature-4d4e37-m3fm4udz → 0.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/elastic-search-logger.html +19 -72
- package/elastic-search-logger.js +103 -39
- package/package.json +5 -5
|
@@ -8,92 +8,39 @@
|
|
|
8
8
|
},
|
|
9
9
|
defaults: {
|
|
10
10
|
name: { value: "" },
|
|
11
|
-
url: {
|
|
12
|
-
urlType: { value: "str" },
|
|
13
|
-
usernameType: { value: "str" },
|
|
14
|
-
passwordType: { value: "str" },
|
|
15
|
-
indexType: { value: "str" },
|
|
11
|
+
url: {value:"http://localhost:9200"},
|
|
16
12
|
filename: { value: "log-elastic.log", required: true },
|
|
17
13
|
maxsize: { value: 1, required: true, validate: function(v) { return v >= 1 } },
|
|
18
14
|
maxfiles: { value: 2, required: true, validate: function(v) { return v >= 1 } },
|
|
19
15
|
},
|
|
20
|
-
label: function
|
|
21
|
-
oneditprepare: function () {
|
|
22
|
-
// URL
|
|
23
|
-
$("#node-config-input-url").typedInput({
|
|
24
|
-
type: this.urlType,
|
|
25
|
-
types: [
|
|
26
|
-
"str",
|
|
27
|
-
"global"
|
|
28
|
-
],
|
|
29
|
-
typeField: "#node-config-input-urlType",
|
|
30
|
-
value: this.url
|
|
31
|
-
})
|
|
32
|
-
|
|
33
|
-
// Username
|
|
34
|
-
$("#node-config-input-username").typedInput({
|
|
35
|
-
type: this.usernameType,
|
|
36
|
-
types:[
|
|
37
|
-
"str",
|
|
38
|
-
"global"
|
|
39
|
-
],
|
|
40
|
-
typeField: "#node-config-input-usernameType",
|
|
41
|
-
value: this.credentials.username
|
|
42
|
-
})
|
|
43
|
-
|
|
44
|
-
// Password
|
|
45
|
-
$("#node-config-input-password").typedInput({
|
|
46
|
-
type: this.passwordType,
|
|
47
|
-
types:[
|
|
48
|
-
"str",
|
|
49
|
-
"global"
|
|
50
|
-
],
|
|
51
|
-
typeField: "#node-config-input-passwordType",
|
|
52
|
-
value: this.credentials.password
|
|
53
|
-
})
|
|
54
|
-
|
|
55
|
-
// Index
|
|
56
|
-
$("#node-config-input-index").typedInput({
|
|
57
|
-
type: this.indexType,
|
|
58
|
-
types:[
|
|
59
|
-
"str",
|
|
60
|
-
"global"
|
|
61
|
-
],
|
|
62
|
-
typeField: "#node-config-input-indexType",
|
|
63
|
-
value: this.credentials.index
|
|
64
|
-
})
|
|
65
|
-
}
|
|
16
|
+
label: function() { return this.name; },
|
|
66
17
|
});
|
|
67
|
-
</script>
|
|
68
|
-
|
|
69
|
-
<script type="text/html" data-template-name="elastic-search-logger">
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
18
|
+
</script>
|
|
19
|
+
|
|
20
|
+
<script type="text/html" data-template-name="elastic-search-logger">
|
|
21
|
+
<div class="form-row">
|
|
22
|
+
<label for="node-config-input-name"><i class="fa fa-tag"></i> Name</label>
|
|
23
|
+
<input type="text" id="node-config-input-name" placeholder="Name">
|
|
24
|
+
</div>
|
|
25
|
+
<div id="elkfields">
|
|
75
26
|
<div class="form-row">
|
|
76
|
-
|
|
27
|
+
<label style="width:100%;"><span>ElasticSearch options</span></label>
|
|
77
28
|
</div>
|
|
78
29
|
<div class="form-row" style="padding-left:20px;">
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
<input type="hidden" id="node-config-input-urlType">
|
|
30
|
+
<label for="node-config-input-url"><i class="fa fa-plug"></i> Elastic URL</label>
|
|
31
|
+
<input type="text" id="node-config-input-url">
|
|
82
32
|
</div>
|
|
83
33
|
<div class="form-row" style="padding-left:20px;">
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
<input type="hidden" id="node-config-input-usernameType">
|
|
34
|
+
<label for="node-config-input-username"><i class="fa fa-user"></i> Username</label>
|
|
35
|
+
<input type="text" id="node-config-input-username">
|
|
87
36
|
</div>
|
|
88
37
|
<div class="form-row" style="padding-left:20px;">
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
<input type="hidden" id="node-config-input-passwordType">
|
|
38
|
+
<label for="node-config-input-password"><i class="fa fa-key"></i> Password</label>
|
|
39
|
+
<input type="password" id="node-config-input-password">
|
|
92
40
|
</div>
|
|
93
41
|
<div class="form-row" style="padding-left:20px;">
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
<input type="hidden" id="node-config-input-indexType">
|
|
42
|
+
<label for="node-config-input-index"><i class="fa fa-tag"></i> Index</label>
|
|
43
|
+
<input type="text" id="node-config-input-index">
|
|
97
44
|
</div>
|
|
98
45
|
</div>
|
|
99
46
|
</script>
|
package/elastic-search-logger.js
CHANGED
|
@@ -4,52 +4,21 @@ module.exports = function (RED) {
|
|
|
4
4
|
function LogElasticLoggerNode(config) {
|
|
5
5
|
let winston = require('winston');
|
|
6
6
|
let winstonElasticSearch = require('winston-elasticsearch');
|
|
7
|
+
let os = require("os");
|
|
8
|
+
this.hostname = os.hostname();
|
|
7
9
|
|
|
8
10
|
RED.nodes.createNode(this, config);
|
|
9
11
|
this.logger = null;
|
|
10
12
|
let transports = [];
|
|
11
13
|
|
|
12
14
|
// Elastic settings
|
|
13
|
-
let url =
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
15
|
+
let url = config.url;
|
|
16
|
+
let user = this.credentials.username || '';
|
|
17
|
+
let password = this.credentials.password || '';
|
|
18
|
+
let index = this.credentials.index || '';
|
|
19
|
+
if (index == '') {
|
|
20
|
+
this.error("Elastic search index is not set");
|
|
18
21
|
}
|
|
19
|
-
if (url == "") {
|
|
20
|
-
this.error("Elastic search url is not set");
|
|
21
|
-
}
|
|
22
|
-
|
|
23
|
-
let user = "";
|
|
24
|
-
if(config.usernameType == "global") {
|
|
25
|
-
user = this.context().global.get(this.credentials.username);
|
|
26
|
-
} else {
|
|
27
|
-
user = this.credentials.username;
|
|
28
|
-
}
|
|
29
|
-
if (user == "") {
|
|
30
|
-
this.error("Elastic search username is not set");
|
|
31
|
-
}
|
|
32
|
-
|
|
33
|
-
let password = "";
|
|
34
|
-
if(config.passwordType == "global") {
|
|
35
|
-
password = this.context().global.get(this.credentials.password);
|
|
36
|
-
} else {
|
|
37
|
-
password = this.credentials.password;
|
|
38
|
-
}
|
|
39
|
-
if (password == "") {
|
|
40
|
-
this.error("Elastic search password is not set");
|
|
41
|
-
}
|
|
42
|
-
|
|
43
|
-
let index = "";
|
|
44
|
-
if(config.indexType == "global") {
|
|
45
|
-
index = this.context().global.get(this.credentials.index);
|
|
46
|
-
} else {
|
|
47
|
-
index = this.credentials.index;
|
|
48
|
-
}
|
|
49
|
-
if (index == "") {
|
|
50
|
-
this.error("Elastic search index is not set");
|
|
51
|
-
}
|
|
52
|
-
|
|
53
22
|
index = index.toLowerCase();
|
|
54
23
|
if (url) {
|
|
55
24
|
const elasticSearchTransport = new winstonElasticSearch.ElasticsearchTransport({
|
|
@@ -106,6 +75,8 @@ module.exports = function (RED) {
|
|
|
106
75
|
}
|
|
107
76
|
|
|
108
77
|
function setElasticFields(logData, node) {
|
|
78
|
+
logData = mergeFieldsIntoMessage(logData, node);
|
|
79
|
+
|
|
109
80
|
let { ElasticsearchTransformer } = require('winston-elasticsearch');
|
|
110
81
|
const transformed = ElasticsearchTransformer(logData);
|
|
111
82
|
transformed['@timestamp'] = logData.timestamp ? logData.timestamp : new Date().toISOString();
|
|
@@ -134,4 +105,97 @@ module.exports = function (RED) {
|
|
|
134
105
|
LogElasticLoggerNode.prototype.addToLog = function addTolog(loglevel, msg) {
|
|
135
106
|
this.logger.log(loglevel, msg.payload.message, msg.payload.meta);
|
|
136
107
|
}
|
|
108
|
+
|
|
109
|
+
// Extract {fields} from message and add them to meta.
|
|
110
|
+
// Store original message in messageTemplate.
|
|
111
|
+
// Replace {fields} with values in message.
|
|
112
|
+
function mergeFieldsIntoMessage(logData, node) {
|
|
113
|
+
try {
|
|
114
|
+
if (logData.message == null) {
|
|
115
|
+
node.error("Message is null");
|
|
116
|
+
return logData;
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
var fieldNames = extractAllFieldNames(logData);
|
|
120
|
+
addAllFieldsToMeta(logData, fieldNames);
|
|
121
|
+
replaceFieldsInMessage(logData, fieldNames);
|
|
122
|
+
addFixedFieldsToLogData(logData, node);
|
|
123
|
+
} catch (error) {
|
|
124
|
+
const serializedLogData = serializeData(logData);
|
|
125
|
+
node.error(`Error merging fields into message: ${error.message}\nlogData: ${serializedLogData}\nStack trace: ${error.stack}`);
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
return logData;
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
function extractAllFieldNames(logData) {
|
|
132
|
+
var fieldNames = [];
|
|
133
|
+
var message = logData.message;
|
|
134
|
+
|
|
135
|
+
var index = 0;
|
|
136
|
+
while (index != -1 && fieldNames.length < logData.meta?.fields?.length) {
|
|
137
|
+
index = message.indexOf("{", index);
|
|
138
|
+
if (index != -1) {
|
|
139
|
+
var endIndex = message.indexOf("}", index);
|
|
140
|
+
var fieldName = message.substring(index + 1, endIndex);
|
|
141
|
+
fieldNames.push(fieldName);
|
|
142
|
+
index = endIndex;
|
|
143
|
+
}
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
return fieldNames;
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
function addAllFieldsToMeta(logData, fieldNames) {
|
|
150
|
+
for (var i = 0; i < fieldNames.length; i++) {
|
|
151
|
+
var fieldName = fieldNames[i];
|
|
152
|
+
var fieldValue = logData.meta.fields[i];
|
|
153
|
+
|
|
154
|
+
if (typeof fieldValue === 'string') {
|
|
155
|
+
if (fieldValue.match(/^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i)) {
|
|
156
|
+
logData.meta[fieldName + "_Guid"] = fieldValue;
|
|
157
|
+
} else {
|
|
158
|
+
logData.meta[fieldName + "_String"] = fieldValue;
|
|
159
|
+
}
|
|
160
|
+
} else if (typeof fieldValue === 'number') {
|
|
161
|
+
logData.meta[fieldName + "_Number"] = fieldValue;
|
|
162
|
+
} else if (typeof fieldValue === 'boolean') {
|
|
163
|
+
logData.meta[fieldName + "_Boolean"] = fieldValue;
|
|
164
|
+
} else if (typeof fieldValue === 'object' && fieldValue instanceof Date) {
|
|
165
|
+
logData.meta[fieldName + "_DateTime"] = fieldValue;
|
|
166
|
+
} else {
|
|
167
|
+
logData.meta[fieldName] = fieldValue;
|
|
168
|
+
}
|
|
169
|
+
}
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
function replaceFieldsInMessage(logData, fieldNames) {
|
|
173
|
+
// Replace all field names in message with values from fields
|
|
174
|
+
logData.messageTemplate = logData.message.slice();
|
|
175
|
+
for (var i = 0; i < fieldNames.length; i++) {
|
|
176
|
+
var fieldName = fieldNames[i];
|
|
177
|
+
var fieldValue = logData.meta.fields[i];
|
|
178
|
+
var replacement = typeof fieldValue === 'string' ? "'" + fieldValue + "'" : fieldValue;
|
|
179
|
+
logData.message = logData.message.replace("{" + fieldName + "}", replacement);
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
// Remove fields from meta
|
|
183
|
+
delete logData.meta.fields;
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
function addFixedFieldsToLogData(logData, node) {
|
|
187
|
+
logData.meta['Origin_String'] = 'Node-RED';
|
|
188
|
+
logData.meta['MachineName_String'] = node.hostname;
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
function serializeData(data) {
|
|
192
|
+
return JSON.stringify(data, (key, value) => {
|
|
193
|
+
if (typeof value === 'object' && value !== null && value !== undefined) {
|
|
194
|
+
if (value instanceof Date) {
|
|
195
|
+
return value.toISOString();
|
|
196
|
+
}
|
|
197
|
+
}
|
|
198
|
+
return value;
|
|
199
|
+
}, 2);
|
|
200
|
+
}
|
|
137
201
|
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@5minds/node-red-contrib-processcube-elasticsearch",
|
|
3
|
-
"version": "0.2.0
|
|
3
|
+
"version": "0.2.0",
|
|
4
4
|
"license": "MIT",
|
|
5
5
|
"description": "Node-RED nodes for Elasticsearch",
|
|
6
6
|
"scripts": {
|
|
@@ -27,14 +27,14 @@
|
|
|
27
27
|
"node-red": {
|
|
28
28
|
"version": ">=3.1.9",
|
|
29
29
|
"nodes": {
|
|
30
|
-
"elastic-search":
|
|
31
|
-
"elastic-search-logger":
|
|
30
|
+
"elastic-search": "elastic-search.js",
|
|
31
|
+
"elastic-search-logger": "elastic-search-logger.js"
|
|
32
32
|
},
|
|
33
33
|
"examples": "examples"
|
|
34
34
|
},
|
|
35
35
|
"dependencies": {
|
|
36
|
-
"winston":
|
|
37
|
-
"winston-elasticsearch":
|
|
36
|
+
"winston": "^3.17.0",
|
|
37
|
+
"winston-elasticsearch": "^0.19.0"
|
|
38
38
|
},
|
|
39
39
|
"keywords": [
|
|
40
40
|
"node-red",
|