Skip to content

Instantly share code, notes, and snippets.

@uditalias
Last active December 9, 2015 19:28
Show Gist options
  • Save uditalias/4317413 to your computer and use it in GitHub Desktop.
Save uditalias/4317413 to your computer and use it in GitHub Desktop.
A good way to override the `console.log` function in the browser, Yet preserved the original functionality of the function. In this example I want to send any `console.log` call to my Node.js/Socket.io server.
(function(){
//saving the original console.log function
var preservedConsoleLog = console.log;
//overriding console.log function
console.log = function() {
//we can't just call to `preservedConsoleLog` function,
//that will throw an error (TypeError: Illegal invocation)
//because we need the function to be inside the
//scope of the `console` object so we going to use the
//`apply` function
preservedConsoleLog.apply(console, arguments);
//and lastly, my addition to the `console.log` function
if(application.socket){
application.socket.emit('console.log', arguments);
}
}
})();
@uditalias
Copy link
Author

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