Skip to content
Snippets Groups Projects
Unverified Commit f5613463 authored by Sigmund, Dominik's avatar Sigmund, Dominik
Browse files

Added Loki Logging

parent 3bd166c0
No related branches found
No related tags found
1 merge request!1Update index.test.js, examples/all-options.js, examples/log-to-file.js,...
# log # @general/log
A real simple logger application. A real simple logger application.
May Optional Log to A File and a graylog-server via http or udp. May Optional Log to A File and a graylog-server via http or udp.
## Installation ## Installation
- `npm install --save @general/config` - `npm install --save @general/log`
## Usage ## Usage
...@@ -61,6 +61,10 @@ Possible Setting: ...@@ -61,6 +61,10 @@ Possible Setting:
"mode": "http or udp, defaults to udp", "mode": "http or udp, defaults to udp",
"server": "graylog-server", "server": "graylog-server",
"port": "graylog-port" "port": "graylog-port"
},
"loki": {
"active": "true or false",
"server":"loki-server"
} }
} }
``` ```
......
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')
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
...@@ -42,6 +42,9 @@ function Log (options) { ...@@ -42,6 +42,9 @@ function Log (options) {
this.udpclient = this.udp.createSocket('udp4') this.udpclient = this.udp.createSocket('udp4')
} }
} }
if (this.options.loki && this.options.loki.active) {
this.request = require('request')
}
if (this.options.file) { if (this.options.file) {
this.fs = require('fs') this.fs = require('fs')
this.path = require('path') this.path = require('path')
...@@ -172,6 +175,26 @@ function Log (options) { ...@@ -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 return msg
} }
this.getDate = function () { this.getDate = function () {
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment