Skip to content

Commit

Permalink
Use util.format directly when composing the final string to log. Make…
Browse files Browse the repository at this point in the history
…s tests pass again.
  • Loading branch information
mikermcneil committed Jun 30, 2014
1 parent 5621dcd commit 6c9cc65
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 19 deletions.
5 changes: 5 additions & 0 deletions lib/defaults.js
Original file line number Diff line number Diff line change
Expand Up @@ -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: {

Expand Down
39 changes: 20 additions & 19 deletions lib/write.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
* Module dependencies.
*/

var _ = require('lodash'),
util = require('util');
var _ = require('lodash');
var util = require('util');



Expand Down Expand Up @@ -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
Expand All @@ -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]);
Expand Down

0 comments on commit 6c9cc65

Please sign in to comment.