From b5e0508962e0d0c3f8a4462c7a48960f6b8017c8 Mon Sep 17 00:00:00 2001 From: Erwin Vervondel <52445274+objt-ev@users.noreply.github.com> Date: Sat, 4 May 2024 17:02:47 +0200 Subject: [PATCH] 1.3.0 add loki support --- README.md | 5 +++-- log-elk-logger.html | 44 +++++++++++++++++++++++++++++++++++++++++++- log-elk-logger.js | 27 +++++++++++++++++++++++++++ log-elk.html | 4 ++-- package.json | 12 +++++++----- 5 files changed, 82 insertions(+), 10 deletions(-) diff --git a/README.md b/README.md index e7d6ca2..4204758 100644 --- a/README.md +++ b/README.md @@ -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) diff --git a/log-elk-logger.html b/log-elk-logger.html index 0683daf..c054b5a 100644 --- a/log-elk-logger.html +++ b/log-elk-logger.html @@ -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 } @@ -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(); + } + }); + } }); @@ -53,6 +69,10 @@
+ + +
+
@@ -82,6 +102,28 @@ +
+
+ +
+
+ + +
+
+ + +
+
+ + +
+
+ + +
+
+
diff --git a/log-elk-logger.js b/log-elk-logger.js index 2884bf1..beaf0da 100644 --- a/log-elk-logger.js +++ b/log-elk-logger.js @@ -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; @@ -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) { diff --git a/log-elk.html b/log-elk.html index 96ce36b..89d99e8 100644 --- a/log-elk.html +++ b/log-elk.html @@ -115,14 +115,14 @@