diff --git a/index.js b/index.js index 49683259fb01a81cb552efcc8f9966d64d870e4d..a6b3184b40db6cab26e83794c23c80b06d5cf79e 100755 --- a/index.js +++ b/index.js @@ -1,222 +1,222 @@ -function Log (options) { - this.levels = { - 'DEBUG': 5, - 'INFO': 4, - 'NOTICE': 3, - 'WARN': 2, - 'ERROR': 1, - 'FATAL': 0 - } - this.options = options || {} - if (this.options.loglevel) { // DEBUG, INFO, NOTICE, WARN, ERROR, FATAL - this.loglevel = this.levels[this.options.loglevel] - } else { - this.loglevel = 2 - } - if (this.options.format) { - this.format = this.options.format - } else { - this.format = '{timestamp}\t{hostname}\t{name}\t{loglevel}\t{message}' - } - if (this.options.delimeter) { - this.delimeter = this.options.delimeter - } else { - this.delimeter = ' ' - } - if (this.options.name) { - this.name = this.options.name - } else { - this.name = require('./package.json').name - } - if (this.options.hostname) { - this.hostname = this.options.hostname - } else { - 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') - } - if (this.options.file) { - this.fs = require('fs') - this.path = require('path') - try { - if (!this.fs.existsSync(this.options.path)) { - this.fs.mkdirSync(this.options.path, { recursive: true }) - } - } catch (error) { - throw new Error('Could not create path ' + this.options.path + '\n' + error.toString()) - } - try { - this.fs.accessSync(this.path.join(this.options.path, this.options.file), this.fs.constants.R_OK | this.fs.constants.W_OK) - } catch (err) { - try { - this.fs.writeFileSync(this.path.join(this.options.path, this.options.file), '', { flag: 'wx' }) - } catch (innerErr) { - throw new Error('Could not Create File ' + this.options.file + '\n' + innerErr.toString()) - } - } - } - this.info = function (...message) { - if (this.loglevel >= this.levels.INFO) { - return this.log('INFO', ...message) - } else { - return '' - } - } - this.notice = function (...message) { - if (this.loglevel >= this.levels.NOTICE) { - return this.log('NOTICE', ...message) - } else { - return '' - } - } - this.fatal = function (...message) { - if (this.loglevel >= this.levels.FATAL) { - return this.log('FATAL', ...message) - } else { - return '' - } - } - this.warn = function (...message) { - if (this.loglevel >= this.levels.WARN) { - return this.log('WARN', ...message) - } else { - return '' - } - } - this.error = function (...message) { - if (this.loglevel >= this.levels.ERROR) { - return this.log('ERROR', ...message) - } else { - return '' - } - } - this.debug = function (...message) { - if (this.loglevel >= this.levels.DEBUG) { - return this.log('DEBUG', ...message) - } else { - return '' - } - } - this.log = function (tag, ...message) { - let msgString = '' - if (message.length > 1) { - let self = this - msgString = message.reduce(function (acc, cur) { - return acc + self.delimeter + self.objectToString(cur); - }) - } else { - msgString = this.objectToString(message[0]) - } - let msg = this.format - msg = msg.replace('{timestamp}', this.getDate()) - msg = msg.replace('{hostname}', this.hostname) - msg = msg.replace('{name}', this.name) - msg = msg.replace('{loglevel}', tag) - msg = msg.replace('{message}', msgString) - - switch (tag) { - case 'INFO': - console.info(msg) - break - case 'NOTICE': - console.log(msg) - break - case 'WARN': - console.warn(msg) - break - case 'ERROR': - console.error(msg) - break - case 'FATAL': - console.error(msg) - break - case 'DEBUG': - console.log(msg) - break - default: - console.log(msg) - } - if (this.options.path && this.options.file) { - this.fs.appendFile(this.path.join(this.options.path, this.options.file), msg + '\n', function (error) { - if (error) { - console.error('Cannot write to File ' + this.options.file) - } - }) - } - 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}, (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 = JSON.stringify({ - streams: [ - { - stream: { - hostname: this.hostname, - name: this.name - }, - values: [ - [Date.now().toString(), 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 () { - 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) { - let add = '' - try { - if (typeof obj === 'string' || obj instanceof String) { - add = obj - } else if(obj instanceof Array) { - add = obj.toString() - } else { - add = JSON.stringify(obj) - } - } catch (error) { - add = obj.toString() - } - return add - } - return this -} -module.exports = Log +function Log (options) { + this.levels = { + 'DEBUG': 5, + 'INFO': 4, + 'NOTICE': 3, + 'WARN': 2, + 'ERROR': 1, + 'FATAL': 0 + } + this.options = options || {} + if (this.options.loglevel) { // DEBUG, INFO, NOTICE, WARN, ERROR, FATAL + this.loglevel = this.levels[this.options.loglevel] + } else { + this.loglevel = 2 + } + if (this.options.format) { + this.format = this.options.format + } else { + this.format = '{timestamp}\t{hostname}\t{name}\t{loglevel}\t{message}' + } + if (this.options.delimeter) { + this.delimeter = this.options.delimeter + } else { + this.delimeter = ' ' + } + if (this.options.name) { + this.name = this.options.name + } else { + this.name = require('./package.json').name + } + if (this.options.hostname) { + this.hostname = this.options.hostname + } else { + 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') + } + if (this.options.file) { + this.fs = require('fs') + this.path = require('path') + try { + if (!this.fs.existsSync(this.options.path)) { + this.fs.mkdirSync(this.options.path, { recursive: true }) + } + } catch (error) { + throw new Error('Could not create path ' + this.options.path + '\n' + error.toString()) + } + try { + this.fs.accessSync(this.path.join(this.options.path, this.options.file), this.fs.constants.R_OK | this.fs.constants.W_OK) + } catch (err) { + try { + this.fs.writeFileSync(this.path.join(this.options.path, this.options.file), '', { flag: 'wx' }) + } catch (innerErr) { + throw new Error('Could not Create File ' + this.options.file + '\n' + innerErr.toString()) + } + } + } + this.info = function (...message) { + if (this.loglevel >= this.levels.INFO) { + return this.log('INFO', ...message) + } else { + return '' + } + } + this.notice = function (...message) { + if (this.loglevel >= this.levels.NOTICE) { + return this.log('NOTICE', ...message) + } else { + return '' + } + } + this.fatal = function (...message) { + if (this.loglevel >= this.levels.FATAL) { + return this.log('FATAL', ...message) + } else { + return '' + } + } + this.warn = function (...message) { + if (this.loglevel >= this.levels.WARN) { + return this.log('WARN', ...message) + } else { + return '' + } + } + this.error = function (...message) { + if (this.loglevel >= this.levels.ERROR) { + return this.log('ERROR', ...message) + } else { + return '' + } + } + this.debug = function (...message) { + if (this.loglevel >= this.levels.DEBUG) { + return this.log('DEBUG', ...message) + } else { + return '' + } + } + this.log = function (tag, ...message) { + let msgString = '' + if (message.length > 1) { + let self = this + msgString = message.reduce(function (acc, cur) { + return acc + self.delimeter + self.objectToString(cur); + }) + } else { + msgString = this.objectToString(message[0]) + } + let msg = this.format + msg = msg.replace('{timestamp}', this.getDate()) + msg = msg.replace('{hostname}', this.hostname) + msg = msg.replace('{name}', this.name) + msg = msg.replace('{loglevel}', tag) + msg = msg.replace('{message}', msgString) + + switch (tag) { + case 'INFO': + console.info(msg) + break + case 'NOTICE': + console.log(msg) + break + case 'WARN': + console.warn(msg) + break + case 'ERROR': + console.error(msg) + break + case 'FATAL': + console.error(msg) + break + case 'DEBUG': + console.log(msg) + break + default: + console.log(msg) + } + if (this.options.path && this.options.file) { + this.fs.appendFile(this.path.join(this.options.path, this.options.file), msg + '\n', function (error) { + if (error) { + console.error('Cannot write to File ' + this.options.file) + } + }) + } + 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 = JSON.stringify({ + streams: [ + { + stream: { + hostname: this.hostname, + app: this.name + }, + values: [ + [Date.now().toString(), message] + ] + } + ] + }) + 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) + } + }) + } + return msg + } + this.getDate = function () { + 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) { + let add = '' + try { + if (typeof obj === 'string' || obj instanceof String) { + add = obj + } else if(obj instanceof Array) { + add = obj.toString() + } else { + add = JSON.stringify(obj) + } + } catch (error) { + add = obj.toString() + } + return add + } + return this +} +module.exports = Log