Pretty log for objects

This commit is contained in:
Guillaume Wuip 2014-10-26 11:10:23 +01:00
parent 7f65af38a7
commit 83243b9718

View file

@ -309,8 +309,41 @@
result += new Array(offset + this.opt.spaceSize).join(' '); result += new Array(offset + this.opt.spaceSize).join(' ');
} }
result += log.argsString; //It's time to log the args
var args = Array.prototype.slice.call(log.args, 0), //transform args in an array
msg = "", //the log message
delimiter = '\n\n',
multiLines = false; //if the log need multiples lines (ie. object, array)
//Process arg one by one
args.forEach(function (arg) {
//if arg is an object / array
//use multiples lines
if (arg !== null && typeof arg === 'object') {
msg += delimiter;
msg += JSON.stringify(arg, null, 2);
multiLines = true;
//for "normal" args
} else {
if (multiLines) {
msg += delimiter;
}
msg += arg + "";
}
});
if (multiLines) {
msg += '\n';
msg = msg.replace(/\n/gm, '\n' + new Array(this.opt.spaceSize).join(' '));
}
result += msg;
return result; return result;