Possible Duplicate:
Intercept calls to console.log in Chrome
Can I extend the console object (for rerouting the logging) in javascript?
When my JS app writes to the console.log
, I want to capture that log message so that I can AJAX that log output to the server. How do I do that?
The code that writes to the log is from external services, which is why I can't just ajax it directly.
Right click on the object in console and click Store as a global variable. the output will be something like temp1. type in console copy(temp1) paste to your favorite text editor.
The console. log() is a function in JavaScript which is used to print any kind of variables defined before in it or to just print any message that needs to be displayed to the user. Syntax: console.
The only notable difference between the two is that, when printing an object, console. log gives special treatment to HTML elements, while console. dir displays everything as plain objects.
You can hijack JavaScript functions in the following manner:
(function(){ var oldLog = console.log; console.log = function (message) { // DO MESSAGE HERE. oldLog.apply(console, arguments); }; })();
oldLog
(for maintainability reasons).message
to your server.apply
is used so we can invoke it on console
using the original arguments. Simply calling oldLog(message)
would fail because log
depends on its association with console
.
Update Per zzzzBov's comment below, in IE9 console.log
isn't actually a function so oldLog.apply
would fail. See console.log.apply not working in IE9 for more details.
Simple:
function yourCustomLog(msg) { //send msg via AJAX } window.console.log = yourCustomLog;
You might want to override the whole console
object to capture console.info
, console.warn
and such:
window.console = { log : function(msg) {...}, info : function(msg) {...}, warn : function(msg) {...}, //... }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With