7

The title is pretty self-explanatory..

Is there a way to read whatever's been output to the console.log up until the moment you decide to read it, using Javascript?

Kawd
  • 4,122
  • 10
  • 37
  • 68
  • 1
    Not standard, no. One could monkey-patch `console.log` to use an internal queue (and also write-through) - or perhaps look into browser/extension specific support (e.g. any Firebug hook?). – user2246674 Sep 19 '13 at 21:42
  • 1
    Maybe this could help you move in the right direction? http://stackoverflow.com/questions/601363/reading-the-firebug-console-in-javascript – Sandeep Bansal Sep 19 '13 at 21:43
  • @user2246674 I think your comment is good enough to be an answer. – Geeky Guy Sep 19 '13 at 21:45

1 Answers1

7

You can make a proxy around it, such as :

(function(win){
    var ncon = win.console;

    var con = win.console = {
        backlog: []
    };

    for(var k in ncon) {
        if(typeof ncon[k] === 'function') {
            con[k] = (function(fn) {
                return function() {
                    con.backlog.push([new Date(), fn, arguments]);
                    ncon[fn].apply(ncon, arguments);
                };
            })(k);
        }
    }
})(window);
Jean-François Beauchamp
  • 5,485
  • 8
  • 43
  • 77
OneOfOne
  • 95,033
  • 20
  • 184
  • 185
  • But I have to be quick enough to override the console before some script for example starts using it, otherwise I'll lose whatever that script logged up until the moment I start overriding the console. Also, if a script uses "delete console;" my override will be lost, correct? So I'll need to set an interval to keep overriding the console, hoping that no logs are lost between those intervals, right? – Kawd Sep 19 '13 at 21:52
  • I don't think you can actually loop over the properties of `console`. – gen_Eric Sep 19 '13 at 21:53
  • @SproutCoder: `delete console;` is not a common line of code. If a script did that, I'd be worried what else it was doing. I wouldn't waste your time with an interval. As for being "quick enough", just load this as the 1st script on your page, that way you can be sure it's first. – gen_Eric Sep 19 '13 at 21:54
  • `for(var k in console) if(typeof console[k] === 'function') console.log(k)` seems to work at least in chrome. – OneOfOne Sep 19 '13 at 21:54
  • @OneOfOne: It wasn't working for me before for some reason. Worked this time :) – gen_Eric Sep 19 '13 at 21:55
  • @SproutCoder at any given point only one piece of code will be running, unless you're using something async (xhr mainly), if you add this piece of code before anything else, you will be fine. – OneOfOne Sep 19 '13 at 21:56
  • I can certainly override the console like so : `var arrayLog = []; console.log = function (x) {arrayLog.push(x);};`. However, if at some point during the logging process a script uses `delete console;` my assignment will be lost so I'll need to `var grabConsole = setInterval(function (){console.log = function (x) {arrayLog.push(x);};},30);` – Kawd Sep 19 '13 at 21:56
  • @RocketHazmat I was checking `typeof k` instead of `typeof ncon[k]`, my bad. – OneOfOne Sep 19 '13 at 21:57
  • @SproutCoder: Like I said, I really wouldn't be worried about `delete console;`. – gen_Eric Sep 19 '13 at 21:57
  • @SproutCoder if a script tries to delete console, something is very wrong. – OneOfOne Sep 19 '13 at 21:57
  • I work with web analytics and I come across asynchronous tags all the time.I've never seen external tags use delete console to behonest, but I've come across websites that use it just because they don't like it I guess, but you're right I can't be paranoid about it – Kawd Sep 19 '13 at 21:59