Reputation: 9522
I'm trying to intercept console.log and write it to an array to access the log programatically. This should be cross browser compliant.
window["log"]=[];
var logger = console.log;
console.log = function() {
window["log"].push({arguments});
// neither seems to output as original
logger( arguments );
logger.call ( arguments );
logger.call ( console, arguments );
logger.apply ( arguments );
}
My problem is that logger( arguments)
always writes an array to console and not the original message. How can I make console output the original message with original line numbers and file?
Upvotes: 0
Views: 571
Reputation: 1074276
You're looking for:
logger.apply(console, arguments);
...which calls the original function with this
set to console
and with arguments
spread out as discrete arguments.
Side note: If you want to be broadly-compatible, you'll need to replace the ES2015+ code here:
window["log"].push({arguments});
with ES5-compatible code:
window["log"].push({arguments: arguments});
Upvotes: 2