Skip to content
Snippets Groups Projects
Commit 3be0aed9 authored by Herfort, Thomas's avatar Herfort, Thomas
Browse files

Merge branch '1-replace-request-with-got' into 'master'

Resolve "replace request with got"

Closes #1

See merge request general/log!1
parents cb0390a1 1cd613e5
No related branches found
No related tags found
1 merge request!1Update index.test.js, examples/all-options.js, examples/log-to-file.js,...
# @general/log
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 loki-server via http or udp.
>__Graylog__ is no longer supported
## Installation
......@@ -31,7 +33,7 @@ e.g.:
will logged as (with default format)
__YYYY-MM-DDTHH:ii:ss SERVER INFO this ["array",8] 4 {"type": "test"}
__YYYY-MM-DDTHH:ii:ss SERVER INFO this ["array",8] 4 {"type": "test"}__
### Formatting
......@@ -47,6 +49,8 @@ Possible Setting:
### Options
>__Graylog__ is no longer supported
```json
{
"name": "Name of App. Default: Name of BaseFolder",
......@@ -56,12 +60,6 @@ Possible Setting:
"loglevel": "Minimal LogLevel. Default: WARN",
"delimeter": "How to join multiple outputs. Default: (a single space)",
"format": "Format the output. Default: {timestamp}\\t{hostname}\\t{loglevel}\\t{message}",
"graylog": {
"active": "true or false",
"mode": "http or udp, defaults to udp",
"server": "graylog-server",
"port": "graylog-port"
},
"loki": {
"active": "true or false",
"server":"loki-server"
......
......@@ -34,16 +34,8 @@ function Log (options) {
let os = require('os')
this.hostname = os.hostname()
}
if (this.options.graylog && this.options.graylog.active) {
if (this.options.graylog.mode && this.options.graylog.mode === 'http') {
this.request = require('request')
} else {
this.udp = require('dgram')
this.udpclient = this.udp.createSocket('udp4')
}
}
if (this.options.loki && this.options.loki.active) {
this.request = require('request')
this.got = require('got')
}
if (this.options.file) {
this.fs = require('fs')
......@@ -65,53 +57,53 @@ function Log (options) {
}
}
}
this.info = function (...message) {
this.info = (...message) => {
if (this.loglevel >= this.levels.INFO) {
return this.log('INFO', ...message)
} else {
return ''
}
}
this.notice = function (...message) {
this.notice = (...message) => {
if (this.loglevel >= this.levels.NOTICE) {
return this.log('NOTICE', ...message)
} else {
return ''
}
}
this.fatal = function (...message) {
this.fatal = (...message) => {
if (this.loglevel >= this.levels.FATAL) {
return this.log('FATAL', ...message)
} else {
return ''
}
}
this.warn = function (...message) {
this.warn = (...message) => {
if (this.loglevel >= this.levels.WARN) {
return this.log('WARN', ...message)
} else {
return ''
}
}
this.error = function (...message) {
this.error = (...message) => {
if (this.loglevel >= this.levels.ERROR) {
return this.log('ERROR', ...message)
} else {
return ''
}
}
this.debug = function (...message) {
this.debug = (...message) => {
if (this.loglevel >= this.levels.DEBUG) {
return this.log('DEBUG', ...message)
} else {
return ''
}
}
this.log = function (tag, ...message) {
this.log = (tag, ...message) => {
let msgString = ''
if (message.length > 1) {
let self = this
msgString = message.reduce(function (acc, cur) {
msgString = message.reduce( (acc, cur) => {
return acc + self.delimeter + self.objectToString(cur);
})
} else {
......@@ -153,28 +145,6 @@ function Log (options) {
}
})
}
if (this.options.graylog && this.options.graylog.active) {
let data = JSON.stringify({
short_message: message,
timestamp: Date.now() / 1000,
host: this.hostname,
facility: this.name,
level: this.levels[tag]
})
if (this.options.graylog.mode && this.options.graylog.mode === 'http') {
this.request.post(this.options.graylog.server + ':' + this.options.graylog.port + '/gelf', {body: data, strictSSL: false}, (error, res, body) => {
if (error) {
console.error(error)
}
})
} else {
this.udpclient.send(Buffer.from(data),this.options.graylog.port,this.options.graylog.server,function(error){
if(error){
console.error(error)
}
})
}
}
if (this.options.loki && this.options.loki.active) {
let data = {
streams: [
......@@ -190,20 +160,20 @@ function Log (options) {
}
]
}
this.request.post(this.options.loki.server + '/loki/api/v1/push', {body: data, json: true, strictSSL: false}, (error, res, body) => {
if (error) {
console.error(error)
try {
this.got.post(this.options.loki.server + '/loki/api/v1/push', {body: data, json: true, strictSSL: false})
} catch (error) {
console.error('Could not send to Loki: ' + error.toString())
}
})
}
return msg
}
this.getDate = function () {
this.getDate = () => {
var tzoffset = (new Date()).getTimezoneOffset() * 60000 // offset in milliseconds
var localISOTime = (new Date(Date.now() - tzoffset)).toISOString().slice(0, -1)
return localISOTime.split('.')[0].trim()
}
this.objectToString = function (obj) {
this.objectToString = (obj) => {
let add = ''
try {
if (typeof obj === 'string' || obj instanceof String) {
......
......@@ -79,7 +79,6 @@ describe('@general/log', () => {
expect(log.hostname).toBe(hostname)
expect(log.format).toBe('{message}')
})
it.todo('should use option graylog')
})
describe('log by level', () => {
it('should log fatal only if level is appropriate', () => {
......@@ -377,10 +376,6 @@ describe('@general/log', () => {
})
})
describe('log to graylog', () => {
it.todo('should log to graylog via udp')
it.todo('should log to graylog via http')
})
// TODO: log to loki
// TODO: add mutation test
})
This diff is collapsed.
{
"name": "@general/log",
"version": "2.14.0",
"version": "3.0.0",
"description": "A simple Logger with Options!",
"main": "index.js",
"scripts": {
......@@ -13,10 +13,10 @@
"url": "https://gitlab.br.de/general/log"
},
"dependencies": {
"request": "^2.88.2"
"got": "^12.3.0"
},
"devDependencies": {
"jest": "^27.2.2"
"jest": "^28.1.3"
},
"publishConfig": {
"@general:registry": "https://gitlab.br.de/api/v4/projects/6/packages/npm/"
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment