1

Is there a way to fire an event on every console.log() which will include either the parameters passed in the log function or the data returned by it? What I want to do is track every console.log execution and pass the information in a div of the page.

I don't mind whether it's a JavaScript or a JQuery answer since I'm using both in this project.

Adonis K. Kakoulidis
  • 4,951
  • 6
  • 34
  • 43

2 Answers2

3
<script language="javascript">
    // cache a reference to the log method on the console object
    var _consoleLog = console.log;

    // replace the existing log method on the console object
    console.log = function() {
            // your custom action
        alert(arguments[0]);

            // pass control to the cached log method
        return _consoleLog.apply(console, arguments);
    };

    // quick test
    console.log("hello");
</script>

This will do the trick

web_bod
  • 5,728
  • 1
  • 17
  • 25
0

Override jQuery functions

Try something like this:

var doSomethingElse = function(data) {
  $("#output").html(data);
};
var f = function() { 
  console.log(this); 
  doSomethingElse(this);
};
f.apply("Hello World", null); 
Community
  • 1
  • 1