Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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?

share|improve this question
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? stackoverflow.com/questions/601363/… –  Sandeep Bansal Sep 19 '13 at 21:43
    
@user2246674 I think your comment is good enough to be an answer. –  Renan Sep 19 '13 at 21:45

1 Answer 1

up vote 6 down vote accepted

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);
                };
            })(fn);
        }
    }
})(window);
share|improve this answer
    
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? –  Sprout Coder Sep 19 '13 at 21:52
    
I don't think you can actually loop over the properties of console. –  Rocket Hazmat 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. –  Rocket Hazmat 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 :) –  Rocket Hazmat Sep 19 '13 at 21:55

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.