Skip to content

Instantly share code, notes, and snippets.

@FGRibreau
Created September 6, 2012 11:56
Show Gist options
  • Star 12 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save FGRibreau/3655432 to your computer and use it in GitHub Desktop.
Save FGRibreau/3655432 to your computer and use it in GitHub Desktop.
Add timestamp information to the JavaScript console
/**
* Patch the console methods in order to provide timestamp information
*
* Usage:
* > console.log('ok')
* 2012-09-06T11:52:56.769Z ok true
*
* Note:
* The patch will only be applied with the first call.
*
* Tested with V8 (Google Chrome & NodeJS)
*/
(function(o){
if(o.__ts__){return;}
var slice = Array.prototype.slice;
['log', 'debug', 'info', 'warn', 'error'].forEach(function(f){
var _= o[f];
o[f] = function(){
var args = slice.call(arguments);
args.unshift(new Date().toISOString());
return _.apply(o, args);
};
});
o.__ts__ = true;
})(console);
@pirhoo
Copy link

pirhoo commented Sep 6, 2012

Nice tip !

You should write a modulerequire("nice-console")(console) ;)

module.exports = function(o) {
  if(o.__ts__){return;}
  var slice = Array.prototype.slice;
  ['log', 'debug', 'info', 'warn', 'error'].forEach(function(f){
    var _= o[f];
    o[f] = function(){
      var args = slice.call(arguments);
      args.unshift(new Date().toISOString());
      return _.apply(o, args);
    };
  });
  o.__ts__ = true;
};

@pirhoo
Copy link

pirhoo commented Sep 6, 2012

*trick

@FGRibreau
Copy link
Author

@deitch
Copy link

deitch commented Dec 27, 2012

Brilliant! And I was going to say to put it in npm.

@deitch
Copy link

deitch commented Dec 27, 2012

FYI, I had an issue with tests failing intermittently, was async timing issue, so step-through debugger really won't help, lots of console.log() in lots of places, but hard to see where the timing issue was. This just clarified everything.

@ialphan
Copy link

ialphan commented Feb 15, 2013

This is very helpful, thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment