Skip to content

Commit

Permalink
Merge pull request #3 from 5minds/feature/remove-update-cycle-add-env
Browse files Browse the repository at this point in the history
Remove update cycle and allow Enviroment configurations
  • Loading branch information
luisthieme authored Dec 12, 2024
2 parents a1d6046 + 63b484f commit 26eddc0
Show file tree
Hide file tree
Showing 2 changed files with 125 additions and 179 deletions.
131 changes: 66 additions & 65 deletions elastic-search-logger.html
Original file line number Diff line number Diff line change
@@ -1,105 +1,106 @@
<script type="text/javascript">
RED.nodes.registerType('elastic-search-logger',{
RED.nodes.registerType('elastic-search-logger', {
category: 'config',
credentials: {
username: {type:"text"},
password: {type:"password"},
index: {type:"text"}
username: { type: 'text' },
password: { type: 'password' },
index: { type: 'text' },
},
defaults: {
name: { value: "" },
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 } },
name: { value: '' },
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({
$('#node-config-input-url').typedInput({
type: this.urlType,
types: [
"str",
"global"
],
typeField: "#node-config-input-urlType",
value: this.url
})
types: ['str', 'env'],
typeField: '#node-config-input-urlType',
value: this.url,
});

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

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

// Index
$("#node-config-input-index").typedInput({
$('#node-config-input-index').typedInput({
type: this.indexType,
types:[
"str",
"global"
],
typeField: "#node-config-input-indexType",
value: this.credentials.index
})
}
});
types: ['str', 'env'],
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">
<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>
</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">
<input type="hidden" id="node-config-input-urlType">
<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">
<input type="hidden" id="node-config-input-usernameType">
<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">
<input type="hidden" id="node-config-input-passwordType">
<input type="text" 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">
<input type="hidden" id="node-config-input-indexType">
<input type="text" id="node-config-input-index" />
<input type="hidden" id="node-config-input-indexType" />
</div>
</div>
</script>

<script type="text/x-red" data-help-name="elastic-search-logger">
<p>The configuration for the elastic-search-logger.</p>
<p>Configurations like the requested URL to Elastic, Username & Password and the Elastic Index.</p>
</script>

</div>
</script>

<script type="text/x-red" data-help-name="elastic-search-logger">
<p>The configuration for the elastic-search-logger.</p>
<p>Configurations like the requested URL to Elastic, Username & Password and the Elastic Index.</p>
</script>
173 changes: 59 additions & 114 deletions elastic-search-logger.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
module.exports = function (RED) {
'use strict';

let lock = false;

function LogElasticLoggerNode(config) {
let winston = require('winston');
let winstonElasticSearch = require('winston-elasticsearch');
Expand All @@ -11,113 +9,73 @@ module.exports = function (RED) {
this.logger = null;
let transports = [];

const stopRefreshing = periodicallyRefreshElasticSettings(10000);

function periodicallyRefreshElasticSettings(timeOut) {
let url, user, password, index;

function refresh() {
if (lock) return;

// Elastic settings
let newUrl = RED.util.evaluateNodeProperty(config.url, config.urlType, this);
if (newUrl == '') {
this.error('Elastic search url is not set');
return;
}

let newUser = RED.util.evaluateNodeProperty(this.credentials.username, config.usernameType, this);
if (newUser == '') {
this.error('Elastic search username is not set');
return;
}

let newPassword = RED.util.evaluateNodeProperty(this.credentials.password, config.passwordType, this);
if (newPassword == '') {
this.error('Elastic search password is not set');
return;
}

let newIndex = RED.util.evaluateNodeProperty(this.credentials.index, config.indexType, this);
if (newIndex == '') {
this.error('Elastic search index is not set');
return;
}

if (newUrl === url && newUser === user && newPassword === password && newIndex === index) {
return;
}

url = newUrl;
user = newUser;
password = newPassword;
index = newIndex;

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 = [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,
},
};

const newLogger = new winston.createLogger({
exitOnError: false,
level: 'Debug',
levels: logLevels.levels,
transports: transports,
});
// Elastic settings
let url = RED.util.evaluateNodeProperty(config.url, config.urlType, this);
if (url == '') {
this.error('Elastic search url is not set');
}

lock = true;
let user = RED.util.evaluateNodeProperty(this.credentials.username, config.usernameType, this);
if (user == '') {
this.error('Elastic search username is not set');
}

try {
if (this.logger) this.logger.close();
let password = RED.util.evaluateNodeProperty(this.credentials.password, config.passwordType, this);
if (password == '') {
this.error('Elastic search password is not set');
}

this.logger = newLogger;
} finally {
lock = false;
}
let index = RED.util.evaluateNodeProperty(this.credentials.index, config.indexType, this);
if (index == '') {
this.error('Elastic search index is not set');
}

this.debug('elastic-search logger created');
}
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,
});

refresh();
const intervalId = setInterval(refresh, timeOut);
transports.push(elasticSearchTransport);

return () => clearInterval(intervalId);
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) {
stopRefreshing();
// close logger
if (this.logger) {
if (this.loggger) {
this.logger.close();
}

Expand Down Expand Up @@ -152,20 +110,7 @@ module.exports = function (RED) {
},
});

LogElasticLoggerNode.prototype.addToLog = function addToLog(loglevel, msg) {
let attempt = 0;

const tryLog = () => {
if (!lock && this.logger) {
this.logger.log(loglevel, msg.payload.message, msg.payload.meta);
} else if (attempt < 5) {
attempt++;
setTimeout(tryLog, 10);
} else {
this.error('Failed to log message after multiple attempts due to logger lock.');
}
};

tryLog();
LogElasticLoggerNode.prototype.addToLog = function addTolog(loglevel, msg) {
this.logger.log(loglevel, msg.payload.message, msg.payload.meta);
};
};

0 comments on commit 26eddc0

Please sign in to comment.