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