From 6c9cc6575b5390b621ccb6d9a949b80f5757a8e7 Mon Sep 17 00:00:00 2001 From: Mike McNeil Date: Mon, 30 Jun 2014 09:34:42 -0500 Subject: [PATCH] Use util.format directly when composing the final string to log. Makes tests pass again. --- lib/defaults.js | 5 +++++ lib/write.js | 39 ++++++++++++++++++++------------------- 2 files changed, 25 insertions(+), 19 deletions(-) diff --git a/lib/defaults.js b/lib/defaults.js index 8261306..505e0a9 100644 --- a/lib/defaults.js +++ b/lib/defaults.js @@ -12,9 +12,14 @@ var DEFAULTS = { level: 'info', + //////////////////////////////////////////////////////////// + // Backwards compatibility: + // (should always be true going forward) + // // Whether to use additional `inspect` logic // (if false- just do exactly what `console.log` would do) inspect: true, + //////////////////////////////////////////////////////////// logLevels: { diff --git a/lib/write.js b/lib/write.js index 33962ce..a4a0534 100644 --- a/lib/write.js +++ b/lib/write.js @@ -2,8 +2,8 @@ * Module dependencies. */ -var _ = require('lodash'), - util = require('util'); +var _ = require('lodash'); +var util = require('util'); @@ -33,22 +33,25 @@ module.exports = function (logFn, logAt, options) { var args = Array.prototype.slice.call(arguments); - // First, prepend the log prefix to the first argument - var prefixStr = (options.prefixes && options.prefixes[logAt]) || ''; - if (prefixStr) { - args[0] = args[0] || ''; - } - // TODO: do this here so prefixes still work when `inspect`===false - + ///////////////////////////////////////////////////////////////// + // For backwards-compatibility: + // (options.inspect should always be true going forward) + // + // Note that prefixes and other options will not work with + // `inspect===false`. New features will also not support + // inspect:false. + // // If `options.inspect` is disabled, just call the log fn normally if (!options.inspect) { return logFn.apply(logFn, args); } + ///////////////////////////////////////////////////////////////// + + // For reference on the following impl, see: + // https://github.com/defunctzombie/node-util/blob/master/util.js#L22 - // Compose `str` of all the arguments - // (include the appropriate prefix if specified) + // Combine the arguments passed into the log fn var pieces = []; - var str = prefixStr; _.each(arguments, function(arg) { // Errors @@ -63,15 +66,13 @@ module.exports = function (logFn, logAt, options) { return; } - // Probably shouldn't do this, actually. - // if (typeof arg === 'function') { - // pieces.push(arg.valueOf()); - // return; - // } - pieces.push(arg); }); - str += pieces.join(' '); + + // Compose `str` of all the arguments + // (include the appropriate prefix if specified) + var prefixStr = (options.prefixes && options.prefixes[logAt]) || ''; + var str = prefixStr + util.format.apply(util, pieces); // Call log fn return logFn.apply(logFn, [str]);