Last active
December 9, 2015 19:28
-
-
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(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); | |
} | |
} | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Read more about it here: http://udidu.blogspot.co.il/2012/12/override-console-functions.html