83

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.

BuZZ-dEE
  • 6,075
  • 12
  • 66
  • 96
bevanb
  • 8,201
  • 10
  • 53
  • 90

2 Answers2

166

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);
    };
})();
  1. Line 1 wraps your function in a closure so no other functions have direct access to oldLog (for maintainability reasons).
  2. Line 2 captures the original method.
  3. Line 3 creates a new function.
  4. Line 4 is where you send message to your server.
  5. Line 5 is invokes the original method as it would have been handled originally.

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.

Community
  • 1
  • 1
Brian Nickel
  • 26,890
  • 5
  • 80
  • 110
  • 1
    [Your code will have issues in IE because `console.log` isn't actually a `Function` instance in IE](http://stackoverflow.com/questions/5538972/console-log-apply-not-working-in-ie9). – zzzzBov Jan 08 '14 at 14:31
  • @zzzzBov Interesting. I assume assigning `console._oldLog = console.log; console._oldLog(message);` would work, but if `console` itself is a hosted object, no changes are guaranteed to stick. Unfortunately, I don't have IE to test this anymore. – Brian Nickel Jan 08 '14 at 16:19
  • yea, you could still call `_oldLog` like that. `oldLog = Function.prototype.bind.call(console.log, console)` works for IE9+, but then needs a polyfill for `Function.prototype.bind` for IE8 and below. – zzzzBov Jan 08 '14 at 16:30
  • 13
    The problem with this method is that we can not capture the log line, it always sends the same line. It would be possible to work around this problem? Thanks – Protomen Oct 16 '14 at 18:48
  • @GuilhermeNascimento raises a good point. How do you preserve the original log line? – rex Mar 15 '16 at 11:57
  • @armensg90 Take a look at this http://stackoverflow.com/a/32928812/3620727 – Doglas Jul 04 '16 at 17:37
  • Sending `message` to server won't send whole log line if multiple arguments are logged this way `console.log("Server is listening at port", server.address().port);` (only `Server is listening at port` will be in `message`) – AntonK Jan 04 '23 at 12:08
27

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) {...},
  //...
}
scraaappy
  • 2,830
  • 2
  • 19
  • 29
Tomasz Nurkiewicz
  • 334,321
  • 69
  • 703
  • 674