logger improvements - allow multiple args

This commit is contained in:
Raghu Rajagopalan 2016-03-28 13:13:11 +05:30
parent 8befe916fb
commit 5cb44d55ff
1 changed files with 21 additions and 7 deletions

View File

@ -57,7 +57,9 @@ function formatTime(timestamp) {
function Log(level) {
this.level = level;
this.log = function(str, level) {
this.log = function() {
var args = Array.prototype.slice.call(arguments);
var level = args.shift();
var logLevel = this.level;
if(typeof logLevel === 'undefined'){
logLevel = defaultLevel;
@ -65,30 +67,42 @@ function Log(level) {
if (logLevel <= level) {
if (typeof console !== 'undefined') { //eslint-disable-line no-console
if (typeof console.log !== 'undefined') { //eslint-disable-line no-console
return console.log('[' + formatTime(new Date()) + '] ' , str); //eslint-disable-line no-console
//return console.log('[' + formatTime(new Date()) + '] ' , str); //eslint-disable-line no-console
args.unshift('[' + formatTime(new Date()) + '] ');
console.log.apply(console, args);
}
}
}
};
this.trace = function(str) {
this.log(str, LEVELS.trace);
var args = Array.prototype.slice.call(arguments);
args.unshift(LEVELS.trace);
this.log.apply(this, args);
};
this.debug = function(str) {
this.log(str, LEVELS.debug);
var args = Array.prototype.slice.call(arguments);
args.unshift(LEVELS.debug);
this.log.apply(this, args);
};
this.info = function(str) {
this.log(str, LEVELS.info);
var args = Array.prototype.slice.call(arguments);
args.unshift(LEVELS.info);
this.log.apply(this, args);
};
this.warn = function(str) {
this.log(str, LEVELS.warn);
var args = Array.prototype.slice.call(arguments);
args.unshift(LEVELS.warn);
this.log.apply(this, args);
};
this.error = function(str) {
this.log(str, LEVELS.error);
var args = Array.prototype.slice.call(arguments);
args.unshift(LEVELS.error);
this.log.apply(this, args);
};
}