Skip to content
Snippets Groups Projects
Commit ecd4b988 authored by admin-sigmundd's avatar admin-sigmundd
Browse files

Added Option to Add Tags

parent ed7a8f33
Branches
Tags
1 merge request!1Update index.test.js, examples/all-options.js, examples/log-to-file.js,...
......@@ -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
......
......@@ -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)
......
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')
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment