Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow to load configuration from globals #1

Merged
merged 5 commits into from
Nov 14, 2024
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
91 changes: 72 additions & 19 deletions elastic-search-logger.html
Original file line number Diff line number Diff line change
Expand Up @@ -8,39 +8,92 @@
},
defaults: {
name: { value: "" },
url: {value:"http://localhost:9200"},
url: { value: "http://localhost:9200" },
urlType: { value: "str" },
usernameType: { value: "str" },
passwordType: { value: "str" },
indexType: { value: "str" },
filename: { value: "log-elastic.log", required: true },
maxsize: { value: 1, required: true, validate: function(v) { return v >= 1 } },
maxfiles: { value: 2, required: true, validate: function(v) { return v >= 1 } },
},
label: function() { return this.name; },
label: function () { return this.name; },
oneditprepare: function () {
// URL
$("#node-config-input-url").typedInput({
type: this.urlType,
types: [
"str",
"global"
],
typeField: "#node-config-input-urlType",
value: this.url
})

// Username
$("#node-config-input-username").typedInput({
type: this.usernameType,
types:[
"str",
"global"
],
typeField: "#node-config-input-usernameType",
value: this.credentials.username
})

// Password
$("#node-config-input-password").typedInput({
type: this.passwordType,
types:[
"str",
"global"
],
typeField: "#node-config-input-passwordType",
value: this.credentials.password
})

// Index
$("#node-config-input-index").typedInput({
type: this.indexType,
types:[
"str",
"global"
],
typeField: "#node-config-input-indexType",
value: this.credentials.index
})
}
});
</script>
<script type="text/html" data-template-name="elastic-search-logger">
<div class="form-row">
<label for="node-config-input-name"><i class="fa fa-tag"></i> Name</label>
<input type="text" id="node-config-input-name" placeholder="Name">
</div>
<div id="elkfields">
</script>

<script type="text/html" data-template-name="elastic-search-logger">
<div class="form-row">
<label for="node-config-input-name"><i class="fa fa-tag"></i> Name</label>
<input type="text" id="node-config-input-name" placeholder="Name">
</div>
<div id="elkfields">
<div class="form-row">
<label style="width:100%;"><span>ElasticSearch options</span></label>
<label style="width:100%;"><span>ElasticSearch options</span></label>
</div>
<div class="form-row" style="padding-left:20px;">
<label for="node-config-input-url"><i class="fa fa-plug"></i> Elastic URL</label>
<input type="text" id="node-config-input-url">
<label for="node-config-input-url"><i class="fa fa-plug"></i> Elastic URL</label>
<input type="text" id="node-config-input-url">
<input type="hidden" id="node-config-input-urlType">
</div>
<div class="form-row" style="padding-left:20px;">
<label for="node-config-input-username"><i class="fa fa-user"></i> Username</label>
<input type="text" id="node-config-input-username">
<label for="node-config-input-username"><i class="fa fa-user"></i> Username</label>
<input type="text" id="node-config-input-username">
<input type="hidden" id="node-config-input-usernameType">
</div>
<div class="form-row" style="padding-left:20px;">
<label for="node-config-input-password"><i class="fa fa-key"></i> Password</label>
<input type="password" id="node-config-input-password">
<label for="node-config-input-password"><i class="fa fa-key"></i> Password</label>
<input type="password" id="node-config-input-password">
<input type="hidden" id="node-config-input-passwordType">
</div>
<div class="form-row" style="padding-left:20px;">
<label for="node-config-input-index"><i class="fa fa-tag"></i> Index</label>
<input type="text" id="node-config-input-index">
<label for="node-config-input-index"><i class="fa fa-tag"></i> Index</label>
<input type="text" id="node-config-input-index">
<input type="hidden" id="node-config-input-indexType">
</div>
</div>
</script>
Expand Down
142 changes: 39 additions & 103 deletions elastic-search-logger.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,52 @@ module.exports = function (RED) {
function LogElasticLoggerNode(config) {
let winston = require('winston');
let winstonElasticSearch = require('winston-elasticsearch');
let os = require("os");
this.hostname = os.hostname();

RED.nodes.createNode(this, config);
this.logger = null;
let transports = [];

// Elastic settings
let url = config.url;
let user = this.credentials.username || '';
let password = this.credentials.password || '';
let index = this.credentials.index || '';
if (index == '') {
this.error("Elastic search index is not set");
let url = "";
if(config.urlType == "global"){
url = this.context().global.get(config.url);
} else {
url = config.url;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Es gibt von Node-RED eine eigene Funktion um TypedInputs auszuwerten:

Suggested change
let url = "";
if(config.urlType == "global"){
url = this.context().global.get(config.url);
} else {
url = config.url;
let url = RED.util.evaluateNodeProperty(n.url, n.urlType, node);

}
if (url == "") {
this.error("Elastic search url is not set");
}

let user = "";
if(config.usernameType == "global") {
user = this.context().global.get(this.credentials.username);
} else {
user = this.credentials.username;
}
if (user == "") {
this.error("Elastic search username is not set");
}

let password = "";
if(config.passwordType == "global") {
password = this.context().global.get(this.credentials.password);
} else {
password = this.credentials.password;
}
if (password == "") {
this.error("Elastic search password is not set");
}

let index = "";
if(config.indexType == "global") {
index = this.context().global.get(this.credentials.index);
} else {
index = this.credentials.index;
}
if (index == "") {
this.error("Elastic search index is not set");
}

index = index.toLowerCase();
if (url) {
const elasticSearchTransport = new winstonElasticSearch.ElasticsearchTransport({
Expand Down Expand Up @@ -75,8 +106,6 @@ module.exports = function (RED) {
}

function setElasticFields(logData, node) {
logData = mergeFieldsIntoMessage(logData, node);

let { ElasticsearchTransformer } = require('winston-elasticsearch');
const transformed = ElasticsearchTransformer(logData);
transformed['@timestamp'] = logData.timestamp ? logData.timestamp : new Date().toISOString();
Expand Down Expand Up @@ -105,97 +134,4 @@ module.exports = function (RED) {
LogElasticLoggerNode.prototype.addToLog = function addTolog(loglevel, msg) {
this.logger.log(loglevel, msg.payload.message, msg.payload.meta);
}

// Extract {fields} from message and add them to meta.
// Store original message in messageTemplate.
// Replace {fields} with values in message.
function mergeFieldsIntoMessage(logData, node) {
try {
if (logData.message == null) {
node.error("Message is null");
return logData;
}

var fieldNames = extractAllFieldNames(logData);
addAllFieldsToMeta(logData, fieldNames);
replaceFieldsInMessage(logData, fieldNames);
addFixedFieldsToLogData(logData, node);
} catch (error) {
const serializedLogData = serializeData(logData);
node.error(`Error merging fields into message: ${error.message}\nlogData: ${serializedLogData}\nStack trace: ${error.stack}`);
}

return logData;
}

function extractAllFieldNames(logData) {
var fieldNames = [];
var message = logData.message;

var index = 0;
while (index != -1 && fieldNames.length < logData.meta?.fields?.length) {
index = message.indexOf("{", index);
if (index != -1) {
var endIndex = message.indexOf("}", index);
var fieldName = message.substring(index + 1, endIndex);
fieldNames.push(fieldName);
index = endIndex;
}
}

return fieldNames;
}

function addAllFieldsToMeta(logData, fieldNames) {
for (var i = 0; i < fieldNames.length; i++) {
var fieldName = fieldNames[i];
var fieldValue = logData.meta.fields[i];

if (typeof fieldValue === 'string') {
if (fieldValue.match(/^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i)) {
logData.meta[fieldName + "_Guid"] = fieldValue;
} else {
logData.meta[fieldName + "_String"] = fieldValue;
}
} else if (typeof fieldValue === 'number') {
logData.meta[fieldName + "_Number"] = fieldValue;
} else if (typeof fieldValue === 'boolean') {
logData.meta[fieldName + "_Boolean"] = fieldValue;
} else if (typeof fieldValue === 'object' && fieldValue instanceof Date) {
logData.meta[fieldName + "_DateTime"] = fieldValue;
} else {
logData.meta[fieldName] = fieldValue;
}
}
}

function replaceFieldsInMessage(logData, fieldNames) {
// Replace all field names in message with values from fields
logData.messageTemplate = logData.message.slice();
for (var i = 0; i < fieldNames.length; i++) {
var fieldName = fieldNames[i];
var fieldValue = logData.meta.fields[i];
var replacement = typeof fieldValue === 'string' ? "'" + fieldValue + "'" : fieldValue;
logData.message = logData.message.replace("{" + fieldName + "}", replacement);
}

// Remove fields from meta
delete logData.meta.fields;
}

function addFixedFieldsToLogData(logData, node) {
logData.meta['Origin_String'] = 'Node-RED';
logData.meta['MachineName_String'] = node.hostname;
}

function serializeData(data) {
return JSON.stringify(data, (key, value) => {
if (typeof value === 'object' && value !== null && value !== undefined) {
if (value instanceof Date) {
return value.toISOString();
}
}
return value;
}, 2);
}
};
Loading