forked from jahway603/knomp
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
82 lines
2.0 KiB
82 lines
2.0 KiB
var dateFormat = require('dateformat');
|
|
var colors = require('colors');
|
|
|
|
|
|
var severityToColor = function(severity, text) {
|
|
switch(severity) {
|
|
case 'special':
|
|
return text.cyan.underline;
|
|
case 'debug':
|
|
return text.green;
|
|
case 'info':
|
|
return text.green;
|
|
case 'warning':
|
|
return text.yellow;
|
|
case 'error':
|
|
return text.red;
|
|
default:
|
|
console.log("Unknown severity " + severity);
|
|
return text.italic;
|
|
}
|
|
};
|
|
|
|
var severityValues = {
|
|
'debug': 1,
|
|
'info' : 2,
|
|
'warning': 3,
|
|
'error': 4,
|
|
'special': 5
|
|
};
|
|
|
|
var PoolLogger = function(configuration) {
|
|
|
|
var logLevelInt = severityValues[configuration.logLevel];
|
|
var logColors = configuration.logColors;
|
|
|
|
var log = function(severity, system, component, text, subcat) {
|
|
|
|
if (severityValues[severity] < logLevelInt) {
|
|
return;
|
|
}
|
|
|
|
if (subcat) {
|
|
var realText = subcat;
|
|
var realSubCat = text;
|
|
text = realText;
|
|
subcat = realSubCat;
|
|
}
|
|
|
|
var entryDesc = dateFormat(new Date(), 'yyyy-mm-dd HH:MM:ss') + ' [' + system + ']\t';
|
|
if (logColors) {
|
|
entryDesc = severityToColor(severity, entryDesc);
|
|
|
|
var logString = entryDesc + ('[' + component + '] ').italic;
|
|
|
|
if (subcat)
|
|
logString += ('(' + subcat + ') ').bold.grey;
|
|
|
|
logString += text.grey;
|
|
} else {
|
|
var logString = entryDesc + '[' + component + '] ';
|
|
|
|
if (subcat)
|
|
logString += '(' + subcat + ') ';
|
|
|
|
logString += text;
|
|
}
|
|
|
|
console.log(logString);
|
|
};
|
|
|
|
// public
|
|
var _this = this;
|
|
Object.keys(severityValues).forEach(function(logType) {
|
|
_this[logType] = function() {
|
|
var args = Array.prototype.slice.call(arguments, 0);
|
|
args.unshift(logType);
|
|
log.apply(this, args);
|
|
};
|
|
});
|
|
};
|
|
|
|
module.exports = PoolLogger;
|
|
|