@5minds/node-red-contrib-processcube-elasticsearch 0.1.0 → 0.2.0-develop-3ee190-m3h6d1f1
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 +72 -19
- package/elastic-search-logger.js +19 -103
- package/package.json +5 -5
|
@@ -8,39 +8,92 @@
|
|
|
8
8
|
},
|
|
9
9
|
defaults: {
|
|
10
10
|
name: { value: "" },
|
|
11
|
-
url: {value:"http://localhost:9200"},
|
|
11
|
+
url: { value: "http://localhost:9200" },
|
|
12
|
+
urlType: { value: "str" },
|
|
13
|
+
usernameType: { value: "str" },
|
|
14
|
+
passwordType: { value: "str" },
|
|
15
|
+
indexType: { value: "str" },
|
|
12
16
|
filename: { value: "log-elastic.log", required: true },
|
|
13
17
|
maxsize: { value: 1, required: true, validate: function(v) { return v >= 1 } },
|
|
14
18
|
maxfiles: { value: 2, required: true, validate: function(v) { return v >= 1 } },
|
|
15
19
|
},
|
|
16
|
-
label: function() { return this.name; },
|
|
20
|
+
label: function () { return this.name; },
|
|
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
|
+
}
|
|
17
66
|
});
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
67
|
+
</script>
|
|
68
|
+
|
|
69
|
+
<script type="text/html" data-template-name="elastic-search-logger">
|
|
70
|
+
<div class="form-row">
|
|
71
|
+
<label for="node-config-input-name"><i class="fa fa-tag"></i> Name</label>
|
|
72
|
+
<input type="text" id="node-config-input-name" placeholder="Name">
|
|
73
|
+
</div>
|
|
74
|
+
<div id="elkfields">
|
|
26
75
|
<div class="form-row">
|
|
27
|
-
|
|
76
|
+
<label style="width:100%;"><span>ElasticSearch options</span></label>
|
|
28
77
|
</div>
|
|
29
78
|
<div class="form-row" style="padding-left:20px;">
|
|
30
|
-
|
|
31
|
-
|
|
79
|
+
<label for="node-config-input-url"><i class="fa fa-plug"></i> Elastic URL</label>
|
|
80
|
+
<input type="text" id="node-config-input-url">
|
|
81
|
+
<input type="hidden" id="node-config-input-urlType">
|
|
32
82
|
</div>
|
|
33
83
|
<div class="form-row" style="padding-left:20px;">
|
|
34
|
-
|
|
35
|
-
|
|
84
|
+
<label for="node-config-input-username"><i class="fa fa-user"></i> Username</label>
|
|
85
|
+
<input type="text" id="node-config-input-username">
|
|
86
|
+
<input type="hidden" id="node-config-input-usernameType">
|
|
36
87
|
</div>
|
|
37
88
|
<div class="form-row" style="padding-left:20px;">
|
|
38
|
-
|
|
39
|
-
|
|
89
|
+
<label for="node-config-input-password"><i class="fa fa-key"></i> Password</label>
|
|
90
|
+
<input type="password" id="node-config-input-password">
|
|
91
|
+
<input type="hidden" id="node-config-input-passwordType">
|
|
40
92
|
</div>
|
|
41
93
|
<div class="form-row" style="padding-left:20px;">
|
|
42
|
-
|
|
43
|
-
|
|
94
|
+
<label for="node-config-input-index"><i class="fa fa-tag"></i> Index</label>
|
|
95
|
+
<input type="text" id="node-config-input-index">
|
|
96
|
+
<input type="hidden" id="node-config-input-indexType">
|
|
44
97
|
</div>
|
|
45
98
|
</div>
|
|
46
99
|
</script>
|
package/elastic-search-logger.js
CHANGED
|
@@ -4,21 +4,32 @@ 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();
|
|
9
7
|
|
|
10
8
|
RED.nodes.createNode(this, config);
|
|
11
9
|
this.logger = null;
|
|
12
10
|
let transports = [];
|
|
13
11
|
|
|
14
12
|
// Elastic settings
|
|
15
|
-
let url = config.url;
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
let index = this.credentials.index || '';
|
|
19
|
-
if (index == '') {
|
|
20
|
-
this.error("Elastic search index is not set");
|
|
13
|
+
let url = RED.util.evaluateNodeProperty(config.url, config.urlType, this);
|
|
14
|
+
if (url == "") {
|
|
15
|
+
this.error("Elastic search url is not set");
|
|
21
16
|
}
|
|
17
|
+
|
|
18
|
+
let user = RED.util.evaluateNodeProperty(this.credentials.username, config.usernameType, this);
|
|
19
|
+
if (user == "") {
|
|
20
|
+
this.error("Elastic search username is not set");
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
let password = RED.util.evaluateNodeProperty(this.credentials.password, config.passwordType, this);
|
|
24
|
+
if (password == "") {
|
|
25
|
+
this.error("Elastic search password is not set");
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
let index = RED.util.evaluateNodeProperty(this.credentials.index, config.indexType, this);
|
|
29
|
+
if (index == "") {
|
|
30
|
+
this.error("Elastic search index is not set");
|
|
31
|
+
}
|
|
32
|
+
|
|
22
33
|
index = index.toLowerCase();
|
|
23
34
|
if (url) {
|
|
24
35
|
const elasticSearchTransport = new winstonElasticSearch.ElasticsearchTransport({
|
|
@@ -75,8 +86,6 @@ module.exports = function (RED) {
|
|
|
75
86
|
}
|
|
76
87
|
|
|
77
88
|
function setElasticFields(logData, node) {
|
|
78
|
-
logData = mergeFieldsIntoMessage(logData, node);
|
|
79
|
-
|
|
80
89
|
let { ElasticsearchTransformer } = require('winston-elasticsearch');
|
|
81
90
|
const transformed = ElasticsearchTransformer(logData);
|
|
82
91
|
transformed['@timestamp'] = logData.timestamp ? logData.timestamp : new Date().toISOString();
|
|
@@ -105,97 +114,4 @@ module.exports = function (RED) {
|
|
|
105
114
|
LogElasticLoggerNode.prototype.addToLog = function addTolog(loglevel, msg) {
|
|
106
115
|
this.logger.log(loglevel, msg.payload.message, msg.payload.meta);
|
|
107
116
|
}
|
|
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
|
-
}
|
|
201
117
|
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@5minds/node-red-contrib-processcube-elasticsearch",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.2.0-develop-3ee190-m3h6d1f1",
|
|
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",
|