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?

3
  • 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?). Commented Sep 19, 2013 at 21:42
  • 1
    Maybe this could help you move in the right direction? stackoverflow.com/questions/601363/… Commented Sep 19, 2013 at 21:43
  • @user2246674 I think your comment is good enough to be an answer. Commented Sep 19, 2013 at 21:45

1 Answer 1

6

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); 
Sign up to request clarification or add additional context in comments.

11 Comments

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?
I don't think you can actually loop over the properties of console.
@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.
for(var k in console) if(typeof console[k] === 'function') console.log(k) seems to work at least in chrome.
@OneOfOne: It wasn't working for me before for some reason. Worked this time :)
|