Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Bind event handler to "console.log" JavaScript event

Tags:

javascript

My script sends text to the console output from several places in Javascript (see examples), how do I bind an event handler function to the log function itself so that a function is executed each time the event is triggered?

try {
    //some code
} catch(e) {
    console.log("error: "+e)
}

function x(n) {
    //some code
    console.log(str)
}
like image 742
PMG Avatar asked Apr 25 '26 21:04

PMG


1 Answers

I would override console.log (but store it in a different variable, in case we want to use it) like so:

var nativeLog = console.log.bind(console) //store native function

console.log = function(text){ //override
    nativeLog("<<<" + text)
}

Abbreviating console.log in JavaScript

like image 198
TastySpaceApple Avatar answered Apr 27 '26 10:04

TastySpaceApple



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!