How to wrap console.log in-place with a prefix

There is a npm module called console-stamp that is pretty nice and simple - whenever you print using the global ‘console’ methods it prefixes everything with a timestamp in whatever format you specify. Well, I was curious how to do this and it is something I would describe as easy but not simple unless you are very familiar with “Function.prototype.apply”. The method I’m going to show you is actually adapted from Secrets of the JavaScript Ninja. It is a simple two step process:

  1. create a “wrap” method that will call a given method with a “wrapper” method you define.
  2. Run the “wrap” method on the function you want to wrap and define the wrapper you will apply around it.

(JSBin example of the following code)

// you will call like: wrap(console, 'info', fn);
function wrap(object, method, wrapper) {
  // get reference to target method off the function (original method)
  var fn = object[method];
  // replace it with this new method:
  return object[method] = function() {
    // apply the wrapper
    // "[fn.bind(this)]" will actually just be the original method that will
    // be an argument in your wrapper
    return wrapper.apply(this, [fn.bind(this)].concat(
        // "arguments" will be args you call the wrapped method with.
        Array.prototype.slice.call(arguments))
      );
    };
}

wrap(console, 'info', function(original) {
    // The first thing in arguments will be the original method,
    // so let get rid of it in our copy
    var args = Array.prototype.slice.call(arguments, 1);

    // Apply the original method with the prefix as the first arg and the
    // info message as the following args
    original.apply(this, ['test info prefix: '].concat(args));
});

// This will print --> "test info prefix: test info message"
console.info('test info message');
Written on January 9, 2016