From f56134635661289892a187882b45d9e3b5e07937 Mon Sep 17 00:00:00 2001
From: Dominik Sigmund <dominik.sigmund@br.de>
Date: Tue, 28 Sep 2021 14:44:28 +0200
Subject: [PATCH] Added Loki Logging

---
 README.md                    |  8 ++++++--
 examples/log-to-loki.js      | 17 +++++++++++++++++
 examples/loki-http-server.js | 17 +++++++++++++++++
 index.js                     | 23 +++++++++++++++++++++++
 4 files changed, 63 insertions(+), 2 deletions(-)
 create mode 100755 examples/log-to-loki.js
 create mode 100755 examples/loki-http-server.js

diff --git a/README.md b/README.md
index 7e2fcbc..9e993ed 100755
--- a/README.md
+++ b/README.md
@@ -1,11 +1,11 @@
-# log
+# @general/log
 
 A real simple logger application.
 May Optional Log to A File and a graylog-server via http or udp.
 
 ## Installation
 
-- `npm install --save @general/config`
+- `npm install --save @general/log`
 
 ## Usage
 
@@ -61,6 +61,10 @@ Possible Setting:
     "mode": "http or udp, defaults to udp",
     "server": "graylog-server",  
     "port": "graylog-port"
+  },
+  "loki": {
+    "active": "true or false",
+    "server":"loki-server"
   }
 }
 ```
diff --git a/examples/log-to-loki.js b/examples/log-to-loki.js
new file mode 100755
index 0000000..c2063aa
--- /dev/null
+++ b/examples/log-to-loki.js
@@ -0,0 +1,17 @@
+const Log = require('../index.js')
+
+let log = new Log({
+  name: 'Sample-Application',
+  hostname: 'test-server',
+  loki: {
+    active: true,
+    server: 'http://localhost:9999'
+  }
+})
+
+log.info('This is an Information')
+log.notice('This is a Notice')
+log.warn('This is a Warning')
+log.error('This is an Error')
+log.fatal('This is a Fatal Message')
+log.debug('This is a Debug Message')
diff --git a/examples/loki-http-server.js b/examples/loki-http-server.js
new file mode 100755
index 0000000..f6af201
--- /dev/null
+++ b/examples/loki-http-server.js
@@ -0,0 +1,17 @@
+var http = require('http');
+http.createServer(function (req, res) {
+  console.log(req.method, req.url, req.headers['content-type'])
+  console.dir(req.headers)
+  if (req.method === 'POST' && req.url === '/loki/api/v1/push' && req.headers['content-type'] === 'application/json') {
+    var body = ''
+    req.on('data', function(data) {
+      body += data
+    })
+    req.on('end', function() {
+      console.log(body)
+      res.writeHead(202, {'Content-Type': 'text/html'})
+      res.end('post received')
+    })
+    
+  }
+}).listen(9999)
\ No newline at end of file
diff --git a/index.js b/index.js
index 850df8e..8e74edc 100755
--- a/index.js
+++ b/index.js
@@ -42,6 +42,9 @@ function Log (options) {
       this.udpclient = this.udp.createSocket('udp4')
     }
   }
+  if (this.options.loki && this.options.loki.active) {
+    this.request = require('request')
+  }
   if (this.options.file) {
     this.fs = require('fs')
     this.path = require('path')
@@ -172,6 +175,26 @@ function Log (options) {
         })
       }
     }
+    if (this.options.loki && this.options.loki.active) {
+      let data = JSON.stringify({
+        streams: [
+          {
+            stream: {
+              hostname: this.hostname,
+              name: this.name
+            },
+            values: [
+              [Date.now() / 1000, message]
+            ]
+          }
+        ]
+      })
+      this.request.post(this.options.loki.server + '/loki/api/v1/push', {body: data, json: true}, (error, res, body) => {
+        if (error) {
+          console.error(error)
+        }
+      })
+    }
     return msg
   }
   this.getDate = function () {
-- 
GitLab