From 83243b9718a8669e98475982d744b8970f90df3d Mon Sep 17 00:00:00 2001 From: Guillaume Wuip Date: Sun, 26 Oct 2014 11:10:23 +0100 Subject: [PATCH] Pretty log for objects --- lib/console2.js | 35 ++++++++++++++++++++++++++++++++++- 1 file changed, 34 insertions(+), 1 deletion(-) diff --git a/lib/console2.js b/lib/console2.js index c12d33a..2fd4473 100644 --- a/lib/console2.js +++ b/lib/console2.js @@ -309,8 +309,41 @@ 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;