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.
You can hijack JavaScript functions in the following manner:
oldLog
(for maintainability reasons).message
to your server.apply
is used so we can invoke it onconsole
using the original arguments. Simply callingoldLog(message)
would fail becauselog
depends on its association withconsole
.Update Per zzzzBov's comment below, in IE9
console.log
isn't actually a function sooldLog.apply
would fail. See console.log.apply not working in IE9 for more details.Simple:
You might want to override the whole
console
object to captureconsole.info
,console.warn
and such: