|
|
@@ -0,0 +1,167 @@ |
|
|
|
|
|
|
|
const debug = require('debug'); |
|
|
|
|
|
|
|
|
|
|
|
var ENABLE_STATE_LOG = true; |
|
|
|
var ENABLE_STATE_INFO = true; |
|
|
|
var ENABLE_STATE_DEBUG = true; |
|
|
|
var ENABLE_STATE_WARNING = true; |
|
|
|
var ENABLE_STATE_ERROR = true; |
|
|
|
var ENABLE_STATE_CRITICAL = true; |
|
|
|
|
|
|
|
var LOG_LIST = []; |
|
|
|
|
|
|
|
|
|
|
|
class Log { |
|
|
|
constructor(namespace){ |
|
|
|
this.__ns = namespace; |
|
|
|
this.__d = debug(namespace); |
|
|
|
this.__d.enabled = ENABLE_STATE_LOG; |
|
|
|
this.__fnc = null; |
|
|
|
this.__enabled = ENABLE_STATE_LOG; |
|
|
|
this.__ienabled = ENABLE_STATE_INFO; |
|
|
|
this.__denabled = ENABLE_STATE_DEBUG; |
|
|
|
this.__wenabled = ENABLE_STATE_WARNING; |
|
|
|
this.__eenabled = ENABLE_STATE_ERROR; |
|
|
|
this.__cenabled = ENABLE_STATE_CRITICAL; |
|
|
|
LOG_LIST.push(this); |
|
|
|
} |
|
|
|
|
|
|
|
get namespace(){return this.__ns;} |
|
|
|
|
|
|
|
get enabled(){return this.__enabled;} |
|
|
|
set enabled(e){this.__enabled = (e === true);} |
|
|
|
|
|
|
|
get infoEnabled(){return this.__ienabled;} |
|
|
|
set infoEnabled(e){this.__ienabled = (e === true);} |
|
|
|
|
|
|
|
get debugEnabled(){return this.__denabled;} |
|
|
|
set debugEnabled(e){this.__denabled = (e === true);} |
|
|
|
|
|
|
|
get warningEnabled(){return this.__wenabled;} |
|
|
|
set warningEnabled(e){this.__wenabled = (e === true);} |
|
|
|
|
|
|
|
get errorEnabled(){return this.__eenabled;} |
|
|
|
set errorEnabled(e){this.__eenabled = (e === true);} |
|
|
|
|
|
|
|
get criticalEnabled(){return this.__cenabled;} |
|
|
|
set criticalEnabled(e){this.__cenabled = (e === true);} |
|
|
|
|
|
|
|
set logFunc(f){ |
|
|
|
this.__fnc = f; |
|
|
|
this.__d.log = f; |
|
|
|
} |
|
|
|
|
|
|
|
log(){ |
|
|
|
if (!ENABLE_STATE_LOG || !this.__enabled || arguments.length < 2){return;} |
|
|
|
var type = arguments[0]; |
|
|
|
var args = Array.prototype.slice.call(arguments, 1); |
|
|
|
if (typeof(args[0]) !== 'string'){return;} |
|
|
|
|
|
|
|
switch(type){ |
|
|
|
case "d": |
|
|
|
if (!ENABLE_STATE_DEBUG || !this.__denabled){return;} |
|
|
|
args[0] = "[ DEBUG ]: " + args[0]; |
|
|
|
break; |
|
|
|
case "w": |
|
|
|
if (!ENABLE_STATE_WARNING || !this.__wenabled){return;} |
|
|
|
args[0] = "[ WARNING ]: " + args[0]; |
|
|
|
break; |
|
|
|
case "e": |
|
|
|
if (!ENABLE_STATE_ERROR || !this.__eenabled){return;} |
|
|
|
args[0] = "[ ERROR ]: " + args[0]; |
|
|
|
break; |
|
|
|
case "c": |
|
|
|
if (!ENABLE_STATE_CRITICAL || !this.__cenabled){return;} |
|
|
|
args[0] = "[ CRITICAL ]: " + args[0]; |
|
|
|
break; |
|
|
|
case "i": |
|
|
|
default: |
|
|
|
if (!ENABLE_STATE_INFO || !this.__ienabled){return;} |
|
|
|
args[0] = "[ INFO ]: " + args[0]; |
|
|
|
} |
|
|
|
|
|
|
|
this.__d.apply(this.__d, args); |
|
|
|
} |
|
|
|
|
|
|
|
info(){ |
|
|
|
let args = Array.prototype.slice.call(arguments); |
|
|
|
args.splice(0, 0, 'i'); |
|
|
|
this.log.apply(this, args); |
|
|
|
} |
|
|
|
|
|
|
|
debug(){ |
|
|
|
let args = (Array.prototype.slice(arguments)).splice(0, 0, 'd'); |
|
|
|
this.log.apply(this, args); |
|
|
|
} |
|
|
|
|
|
|
|
warning(){ |
|
|
|
let args = (Array.prototype.slice(arguments)).splice(0, 0, 'w'); |
|
|
|
this.log.apply(this, args); |
|
|
|
} |
|
|
|
|
|
|
|
error(){ |
|
|
|
let args = (Array.prototype.slice(arguments)).splice(0, 0, 'e'); |
|
|
|
this.log.apply(this, args); |
|
|
|
} |
|
|
|
|
|
|
|
critical(){ |
|
|
|
let args = (Array.prototype.slice(arguments)).splice(0, 0, 'c'); |
|
|
|
this.log.apply(this, args); |
|
|
|
} |
|
|
|
|
|
|
|
extend(ens){ |
|
|
|
var nlog = new LOG(this.__ns + ":" + ens); |
|
|
|
nlog.enabled = this.__enabled; |
|
|
|
nlog.infoEnabled = this.__ienabled; |
|
|
|
nlog.debugEnabled = this.__denabled; |
|
|
|
nlog.warningEnabled = this.__wenabled; |
|
|
|
nlog.errorEnabled = this.__eenabled; |
|
|
|
nlog.criticalEnabled = this.__cenabled; |
|
|
|
if (this.__fnc !== null) |
|
|
|
nlog.logFunc = this.__fnc; |
|
|
|
return nlog; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
module.exports = { |
|
|
|
Log:Log, |
|
|
|
enabled: function(){ |
|
|
|
if (arguments.length > 0) |
|
|
|
ENABLE_STATE_LOG = (arguments[0] === true); |
|
|
|
return ENABLE_STATE_LOG; |
|
|
|
}, |
|
|
|
|
|
|
|
infoEnabled: function(){ |
|
|
|
if (arguments.length > 0) |
|
|
|
ENABLE_STATE_INFO = (arguments[0] === true); |
|
|
|
return ENABLE_STATE_INFO; |
|
|
|
}, |
|
|
|
|
|
|
|
debugEnabled: function(){ |
|
|
|
if (arguments.length > 0) |
|
|
|
ENABLE_STATE_DEBUG = (arguments[0] === true); |
|
|
|
return ENABLE_STATE_DEBUG; |
|
|
|
}, |
|
|
|
|
|
|
|
warningEnabled: function(){ |
|
|
|
if (arguments.length > 0) |
|
|
|
ENABLE_STATE_WARNING = (arguments[0] === true); |
|
|
|
return ENABLE_STATE_WARNING; |
|
|
|
}, |
|
|
|
|
|
|
|
errorEnabled: function(){ |
|
|
|
if (arguments.length > 0) |
|
|
|
ENABLE_STATE_ERROR = (arguments[0] === true); |
|
|
|
return ENABLE_STATE_ERROR; |
|
|
|
}, |
|
|
|
|
|
|
|
criticalEnabled: function(){ |
|
|
|
if (arguments.length > 0) |
|
|
|
ENABLE_STATE_CRITICAL = (arguments[0] === true); |
|
|
|
return ENABLE_STATE_CRITICAL; |
|
|
|
} |
|
|
|
}; |
|
|
|
|