Created
March 30, 2017 16:03
-
-
Save shide1989/d902bc14982f533e9d70336ff8f80f78 to your computer and use it in GitHub Desktop.
A simple logger to know when, where and what's happening (using sweet npm colors)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
Logging levels : | |
0 : Show error | |
1 : Show warning | |
2 : Show valid | |
3 : Show info | |
4 : Show debug | |
5 : show verbose | |
[level]Raw : Raw info without time & msg location | |
Usage : | |
config.js : | |
module.exports = { | |
component :{ | |
module: 3 //will show errors, warnings and infos | |
} | |
} | |
mymodule.js : | |
module.exports = { | |
var logger = require('./logger')('component.module'); | |
return : { | |
myfunction : function(data){ | |
logger.debug('myfunction', 'data :', data); | |
} | |
}; | |
} | |
*/ | |
"use strict"; | |
var color = require('colors'); | |
var config = require('../app').config; | |
module.exports = function (tag) { | |
var _tag = tag || 'tag undefined'; | |
var param = null; | |
color.setTheme({custom: ['cyan', 'inverse']}); | |
if (_tag.indexOf('.') > -1 && _tag.split('.').length > 1) { | |
var t0 = _tag.split('.')[0], | |
t1 = _tag.split('.')[1], | |
t2 = _tag.split('.')[2]; | |
param = config.logger[t0] && (config.logger[t0][t1][t2] || config.logger[t0][t1]); | |
if (!param) | |
throw new Error('Wrong configuration value for :' + tag); | |
} | |
else | |
param = config.logger[_tag]; | |
var _getDate = function () { | |
var now = new Date(); | |
return (now.getFullYear() + '-' + | |
now.getMonth() + '-' + | |
now.getDate() + ' ' + | |
now.getHours() + ':' + | |
now.getMinutes() + ':' + | |
now.getSeconds()).white; | |
}; | |
var _isValid = function (lvl) { | |
// console.log(_tag, param, param >= parseInt(lvl)); | |
return parseInt(lvl) > -1 && param >= lvl; | |
}; | |
return { | |
/** | |
* Verbose | |
* @param context | |
* @param msg | |
* @param object | |
* @return {*} | |
*/ | |
verbose: function (context, msg, object) { | |
if (msg && typeof msg !== 'string') return this.error('logger', '"msg" parameter must be a string'); | |
if (_isValid(5)) { | |
console.log('VERB'.bgWhite.black, _getDate(), ('[' + _tag + '.' + context + ']').white); | |
if (msg) | |
console.log(msg.white, typeof(object) !== 'undefined' ? object : ''); | |
} | |
}, | |
/** | |
* Verbose Raw | |
* @param msg | |
* @param object | |
* @return {*} | |
*/ | |
verboseRaw: function (msg, object) { | |
if (msg && typeof msg !== 'string') return this.error('logger', '"msg" parameter must be a string'); | |
if (_isValid(5) && msg) | |
console.log(msg.white, typeof(object) !== 'undefined' ? object : ''); | |
}, | |
/** | |
* Debug | |
* @param context | |
* @param msg | |
* @param object | |
* @return {*} | |
*/ | |
debug: function (context, msg, object) { | |
if (msg && typeof msg !== 'string') return this.error('logger', '"debug" parameter must be a string'); | |
if (_isValid(4)) { | |
console.log('DEBUG'.bgWhite.black, _getDate(), ('[' + _tag + '.' + context + ']').white); | |
if (msg) | |
console.log(msg.white, typeof(object) !== 'undefined' ? object : ''); | |
} | |
}, | |
/** | |
* Debug Raw | |
* @param msg | |
* @param object | |
* @return {*} | |
*/ | |
debugRaw: function (msg, object) { | |
if (msg && typeof msg !== 'string') return this.error('logger', '"debug" parameter must be a string'); | |
if (_isValid(4) && msg) | |
console.log(msg.white, typeof(object) !== 'undefined' ? object : ''); | |
}, | |
/** | |
* Info | |
* @param context | |
* @param msg | |
* @param object | |
* @return {*} | |
*/ | |
info: function (context, msg, object) { | |
if (msg && typeof msg !== 'string') return this.error('logger', '"info" parameter must be a string'); | |
if (_isValid(3)) { | |
console.log('INFO'.bgWhite.black, _getDate(), ('[' + _tag + '.' + context + ']').white); | |
if (msg) | |
console.log(msg.white, typeof(object) !== 'undefined' ? object : ''); | |
} | |
}, | |
/** | |
* Info Raw | |
* @param msg | |
* @param object | |
* @return {*} | |
*/ | |
infoRaw: function (msg, object) { | |
if (msg && typeof msg !== 'string') return this.error('logger', '"info" parameter must be a string'); | |
if (_isValid(3) && msg) | |
console.log(msg.white, typeof(object) !== 'undefined' ? object : ''); | |
}, | |
/** | |
* Valid | |
* @param context | |
* @param msg | |
* @param object | |
* @return {*} | |
*/ | |
valid: function (context, msg, object) { | |
if (msg && typeof msg !== 'string') return this.error('logger', '"valid" parameter must be a string'); | |
if (_isValid(2)) { | |
console.log('VALID'.bgGreen.white, _getDate(), ('[' + _tag + '.' + context + ']').green); | |
if (msg) | |
console.log(msg.green, typeof(object) !== 'undefined' ? object : ''); | |
} | |
}, | |
/** | |
* Valid Raw | |
* @param msg | |
* @param object | |
* @return {*} | |
*/ | |
validRaw: function (msg, object) { | |
if (msg && typeof msg !== 'string') return this.error('logger', '"valid" parameter must be a string'); | |
if (_isValid(2) && msg) | |
console.log(msg.green, typeof(object) !== 'undefined' ? object : ''); | |
}, | |
/** | |
* Warn | |
* @param context | |
* @param warning | |
* @param object | |
* @return {*} | |
*/ | |
warn: function (context, warning, object) { | |
if (warning && typeof warning !== 'string') return this.error('logger', '"warning" parameter must be a string'); | |
if (_isValid(1)) { | |
console.log('WARN'.bgYellow.white, _getDate(), ('[' + _tag + '.' + context + ']').yellow); | |
if (warning) | |
console.log(warning.yellow, typeof(object) !== 'undefined' ? object : ''); | |
} | |
}, | |
/** | |
* Warn Raw | |
* @param warning | |
* @param object | |
* @return {*} | |
*/ | |
warnRaw: function (warning, object) { | |
if (warning && typeof warning !== 'string') return this.error('logger', '"warning" parameter must be a string'); | |
if (_isValid(1) && warning) | |
console.log(warning.yellow, typeof(object) !== 'undefined' ? object : ''); | |
}, | |
/** | |
* Error | |
* @param context | |
* @param error | |
* @param object | |
* @return {*} | |
*/ | |
error: function (context, error, object) { | |
if (error && typeof error !== 'string') return this.error('logger', '"error" parameter must be a string'); | |
if (_isValid(0)) { | |
console.error('ERROR'.bgRed.white, _getDate(), ('[' + _tag + '.' + context + ']').red); | |
if (error) | |
console.error(error.red, typeof(object) !== 'undefined' ? object : ''); | |
} | |
}, | |
/** | |
* Error Raw | |
* @param error | |
* @param object | |
* @return {*} | |
*/ | |
errorRaw: function (error, object) { | |
if (error && typeof error !== 'string') return this.error('logger', '"error" parameter must be a string'); | |
if (_isValid(0) && error) | |
console.error(error.red, typeof(object) !== 'undefined' ? object : ''); | |
} | |
}; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment