From ecd4b988980ba678b582e4411a0f406f03f75428 Mon Sep 17 00:00:00 2001 From: Domink Sigmund <sigmund.dominik@googlemail.com> Date: Fri, 5 Oct 2018 09:53:58 +0200 Subject: [PATCH] Added Option to Add Tags --- README.md | 25 ++++++++++++++++++------- index.js | 42 +++++++++++++++++++++++++++--------------- samples/use-tags.js | 10 ++++++++++ 3 files changed, 55 insertions(+), 22 deletions(-) create mode 100644 samples/use-tags.js diff --git a/README.md b/README.md index 78ee80d..ccfefad 100644 --- a/README.md +++ b/README.md @@ -12,16 +12,27 @@ May Optional Log to A File and / or a Graylog-Server `const Log = require('log')` `let log = new Log(options)` -Note: The options Part may be omitted, as all parts are optional, but using the name is recommended, as without it, the folder-name will be used +Note: The options Part may be omitted, as all parts are optional, but using the name is recommended, as without it, the package-name will be used ### Methods -* `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')` + +* `log.info('This is an Information' [, tags])` +* `log.notice('This is a Notice' [, tags])` +* `log.warn('This is a Warning' [, tags])` +* `log.error('This is an Error' [, tags])` +* `log.fatal('This is a Fatal Message' [, tags])` +* `log.debug('This is a Debug Message' [, tags])` + +You may add Tags to a logline. These Tags may take the following forms: + +* string = 'tag' +* string, seperated by whitespace = 'tag1 tag2 tag3' +* string, seperated by comma = 'tag1,tag2,tag3' +* array of strings = ['tag1', 'tag2', 'tag3'] + +These Tags will be added to the line with octothorpes added. +(#tag1 #tag2 #tag3) ### Options diff --git a/index.js b/index.js index 5de346d..7409786 100644 --- a/index.js +++ b/index.js @@ -14,29 +14,41 @@ function Log (options) { if (this.options.file) { this.fs = require('fs') } - this.info = function (message) { - this.log('INFO', message) + this.info = function (message, tags) { + this.log('INFO', message, tags) } - this.notice = function (message) { - this.log('NOTICE', message) + this.notice = function (message, tags) { + this.log('NOTICE', message, tags) } - this.fatal = function (message) { - this.log('FATAL', message) + this.fatal = function (message, tags) { + this.log('FATAL', message, tags) } - this.warn = function (message) { - this.log('WARN', message) + this.warn = function (message, tags) { + this.log('WARN', message, tags) } - this.error = function (message) { - this.log('ERROR', message) + this.error = function (message, tags) { + this.log('ERROR', message, tags) } - this.debug = function (message) { - this.log('DEBUG', message) + this.debug = function (message, tags) { + this.log('DEBUG', message, tags) } - this.log = function (tag, message) { - let msg = this.getDate() + '\t' + this.hostname + '\t' + this.name + '\t' + tag + '\t' + message + this.log = function (tag, message, tags) { + let tadd = '' + if (typeof tags !== 'undefined') { + if (Array.isArray(tags)) { + tadd = '#' + tags.join(' #') + } else if (tags.includes(' ')) { + tadd = '#' + tags.split(' ').join(' #') + } else if (tags.includes(',')) { + tadd = '#' + tags.split(',').join(' #') + } else { + tadd = '#' + tags + } + } + let msg = this.getDate() + '\t' + this.hostname + '\t' + this.name + '\t' + tag + '\t' + message + '\t' + tadd switch (tag) { case 'INFO': - console.log(msg) + console.info(msg) break case 'NOTICE': console.log(msg) diff --git a/samples/use-tags.js b/samples/use-tags.js new file mode 100644 index 0000000..d59633f --- /dev/null +++ b/samples/use-tags.js @@ -0,0 +1,10 @@ +const Log = require('../index.js') + +let log = new Log() + +log.info('This is an Information', 'info') +log.notice('This is a Notice', 'notice system ping') +log.warn('This is a Warning', 'some,tags,added') +log.error('This is an Error', ['tag', 'error', 'crit', 'api']) +log.debug('This is a Debug Message') +log.fatal('This is a Fatal Message') -- GitLab