The usual suggestion (Capturing javascript console.log?) for a js console.log interceptor falls short on two counts, both serious:
(i) all calls in the actual browser console now appear to come from the same line - where the new function is defined - rather then where the call is done
(ii) the intercepted arguments are not yet formatted the way console.log
does (i.e. the %
replacement goodies, %o
, in particular):
(function() {
function send_stuff(){ /* Deal with console arguments. */ }
var oldLog=console.log;
console.log=function(msg) {
oldLog.apply(this,arguments);
send_stuff(Array.prototype.slice.call(arguments).join());
}
}())
console.log("test");
console.log("Hello %s", "Bob");
console.log("Here is an object %o", { stuff: "thing" });
Maybe something better (e.g. something able to capture the actual buffer content of console.log) has been devised.
Edit: To clarify the multiple arguments failure: besides expanding the file/line info, console.log also does some clever replacements - a-la-printf - of % sequences. Of particular interest is the %o sequence, where a deep-ish dump of the subsequent object(s) is expanded, and put to good use by the browser's console window. This cannot be replicated, unless one is willing to basically reimplement its logic (unnecessarily and , in all likelyhood, unefficiently)
Cheers,