Skip to content

Commit

Permalink
1.3.0 add loki support
Browse files Browse the repository at this point in the history
  • Loading branch information
objt-ev committed May 4, 2024
1 parent 42b0912 commit b5e0508
Show file tree
Hide file tree
Showing 5 changed files with 82 additions and 10 deletions.
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,13 @@

A node-red logging output node supporting multiple output targets:
- ElasticSearch
- Loki (Grafana)
- File, including size and number of file specs
- System Console
- Debug Window

This project uses the winston and winston-elasticsearch logging libraries
This project uses the winston, winston-elasticsearch and winston-loki logging libraries



Parts of this project is based on ['node-red-contrib-advance-logger'](https://github.com/jayathuam/node-red-contrib-advance-logger)
Initial Parts of this project was based on ['node-red-contrib-advance-logger'](https://github.com/jayathuam/node-red-contrib-advance-logger)
44 changes: 43 additions & 1 deletion log-elk-logger.html
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,20 @@
category: 'config',
credentials: {
username: {type:"text"},
password: {type:"password"}
password: {type:"password"},
loki_username: {type:"text"},
loki_password: {type:"password"}
},
defaults: {
name: { value: "" },
url: {value:"http://localhost:9200"},
loki_url: {value:"http://localhost:3100"},
loki_app: {value:"node-red"},
filename: { value: "log-elk.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 } },
logelk: { value: false },
logloki: { value: false },
logfile: { value: false },
logconsole: { value: false },
logdebug: { value: false }
Expand All @@ -38,6 +43,17 @@
$("#elkfields").hide();
}
});

$("#node-config-input-logloki").on("change", function() {
if ($("#node-config-input-logloki").is(':checked')) {
$("#node-config-input-logloki").attr("file", "file");
$("#lokifields").show();
} else {
$("#node-config-input-logloki").removeAttr("file");
$("#lokifields").hide();
}
});

}
});
</script>
Expand All @@ -53,6 +69,10 @@
<label style="width:auto; margin-top:7px;" for="node-config-input-logelk"><span>ElasticSearch</span></label>
<br>
<label>&nbsp;</label>
<input style="width:20px; vertical-align:baseline; margin-right:5px;" type="checkbox" id="node-config-input-logloki" autocomplete="off">
<label style="width:auto; margin-top:7px;" for="node-config-input-logloki"><span>Loki</span></label>
<br>
<label>&nbsp;</label>
<input style="width:20px; vertical-align:baseline; margin-right:5px;" type="checkbox" id="node-config-input-logfile" autocomplete="off">
<label style="width:auto; margin-top:7px;" for="node-config-input-logfile"><span>File</span></label>
<br>
Expand Down Expand Up @@ -82,6 +102,28 @@
</div>
</div>

<div id="lokifields">
<div class="form-row">
<label style="width:100%;"><span>Loki options</span></label>
</div>
<div class="form-row" style="padding-left:20px;">
<label for="node-config-input-loki_url"><i class="fa fa-plug"></i> Loki URL</label>
<input type="text" id="node-config-input-loki_url">
</div>
<div class="form-row" style="padding-left:20px;">
<label for="node-config-input-loki_username"><i class="fa fa-user"></i> Username</label>
<input type="text" id="node-config-input-loki_username">
</div>
<div class="form-row" style="padding-left:20px;">
<label for="node-config-input-loki_password"><i class="fa fa-key"></i> Password</label>
<input type="password" id="node-config-input-loki_password">
</div>
<div class="form-row" style="padding-left:20px;">
<label for="node-config-input-loki_app"><i class="fa fa-tag"></i> App Label</label>
<input type="text" id="node-config-input-loki_app">
</div>
</div>

<div id="filefields">
<div class="form-row">
<label style="width:100%;"><span>File options</span></label>
Expand Down
27 changes: 27 additions & 0 deletions log-elk-logger.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ module.exports = function (RED) {
function LogElkLoggerNode(config) {
var winston = require('winston');
var winstonElasticSearch = require('winston-elasticsearch');
const LokiTransport = require("winston-loki");

RED.nodes.createNode(this, config);
this.logger = null;
Expand Down Expand Up @@ -50,6 +51,32 @@ module.exports = function (RED) {
}
}

// Loki settings
var lokiLog = config.logloki;
if (lokiLog) {
var url = config.loki_url;
var auth;
if (this.credentials.loki_username) {
auth = this.credentials.loki_username + ':' + this.credentials.loki_password;
}
if (url) {
const lokiTransport = new LokiTransport({
host: url,
json: true,
basicAuth: auth,
labels: { app: config.loki_app || 'node-red' },
format: winston.format.combine(winston.format.timestamp(), winston.format.json())
})

transports.push(lokiTransport);

lokiTransport.on('error', (error) => {
console.error('Error in lokiTransport caught', error);
});
}
}


// File settings
var fileLog = config.logfile;
if (fileLog) {
Expand Down
4 changes: 2 additions & 2 deletions log-elk.html
Original file line number Diff line number Diff line change
Expand Up @@ -115,14 +115,14 @@
</script>

<script type="text/x-red" data-help-name="log-elk">
<p>A logging node with multiple log outputs using the winston and winston-elastic logging libraries</p>
<p>A logging node with multiple log outputs using the winston, winston-elastic and winston-loki logging libraries</p>
<h3>Details</h3>
<p>Output <code>msg.payload</code> (or <code>msg.&lt;custom path&gt;</code>) or complete <code>msg</code> is used as input of the logged message.
If it contains an Object it will be converted to a JSON string before being sent.</p>
<p>The Log Level (error, warn, info, debug) used can be configured in the node or can be set
by <code>msg.loglevel</code> (or <code>msg.&lt;custom path&gt;</code>).
If the content is not one of ('error', 'warn', 'info', 'debug'), 'debug' will be used as fallback</p>
<p>If <code>msg.meta</code> is set, the meta info Object will be added to the file and ElasticSearch output as a JSON string.</p>
<p>If <code>msg.meta</code> is set, the meta info Object will be added to the output.</p>

<h3>References</h3>
<p>This node is based on parts of node-red-contrib-advance-logger</p>
Expand Down
12 changes: 7 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,20 +1,21 @@
{
"name": "node-red-contrib-log-elk",
"version": "1.2.8",
"description": "A Node-RED logging node with multiple outputs using the winston and winston-elasticsearch logging libraries",
"version": "1.3.0",
"description": "A Node-RED logging node with multiple outputs using the winston,winston-elasticsearch and winston-loki logging libraries",
"main": "log-elk.js",
"scripts": {
"release": "standard-version",
"test": "echo \"Error: no test specified\" && exit 1"
},
"repository": {
"type": "git",
"url": "https://github.com/objt-ev/node-red-contrib-log-elk"
"url": "https://github.com/evervondel/node-red-contrib-log-elk"
},
"keywords": [
"logger",
"winston",
"elasticsearch",
"loki",
"node-red"
],
"node-red": {
Expand All @@ -30,8 +31,9 @@
"author": "ev",
"license": "MIT",
"dependencies": {
"winston": "^3.3.3",
"winston-elasticsearch": "^0.15.2",
"winston": "^3.13.0",
"winston-elasticsearch": "^0.18.0",
"winston-loki": "^6.1.2",
"json-stringify-safe": "^5.0.1"
}
}

0 comments on commit b5e0508

Please sign in to comment.