Skip to content

Instantly share code, notes, and snippets.

@aktau
Created May 10, 2013 10:05
Show Gist options
  • Save aktau/5553553 to your computer and use it in GitHub Desktop.
Save aktau/5553553 to your computer and use it in GitHub Desktop.
Prepend time logging to console, supports multiple arguments
function takeOverConsole(){
var console = window.console;
if (!console) return;
function intercept(method){
var original = console[method];
console[method] = function(){
// do sneaky stuff
var now = new Date();
var currentDate = [ '[' + now.toUTCString() + '/' + now.getUTCMilliseconds() + '] ' ];
var args = Array.prototype.slice.call(arguments);
if (original.apply){
// Do this for normal browsers
original.apply(console, currentDate.concat(args));
}else{
// Do this for IE
var message = Array.prototype.slice.apply(arguments).join(' ');
original(message);
}
}
}
var methods = ['log', 'warn', 'error'];
for (var i = 0; i < methods.length; i++)
intercept(methods[i]);
}
takeOverConsole();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment