diff --git a/lib/console.js b/lib/console.js index 857199cf5c9..8f09c5aab6a 100644 --- a/lib/console.js +++ b/lib/console.js @@ -3,8 +3,10 @@ var writeError = process.binding('stdio').writeError; // console object var formatRegExp = /%[sdj]/g; function format(f) { + var util = require('util'); + if (typeof f !== 'string') { - var objects = [], util = require('util'); + var objects = []; for (var i = 0; i < arguments.length; i++) { objects.push(util.inspect(arguments[i])); } @@ -16,15 +18,19 @@ function format(f) { var args = arguments; var str = String(f).replace(formatRegExp, function(x) { switch (x) { - case '%s': return args[i++]; - case '%d': return +args[i++]; + case '%s': return String(args[i++]); + case '%d': return Number(args[i++]); case '%j': return JSON.stringify(args[i++]); default: return x; } }); - for (var len = args.length; i < len; ++i) { - str += ' ' + args[i]; + for (var len = args.length, x = args[i]; i < len; x = args[++i]) { + if (x === null || typeof x !== 'object') { + str += ' ' + x; + } else { + str += ' ' + util.inspect(x); + } } return str; }