4

If possible, I'd like to display JavaScript console output in a log window that I developed for my application. There's a lot of solutions for posting messages to the console, but I couldn't find any that let you capture the console output.

I'm not even sure if this is possible. Is console output stored in an object at some level of the DOM?

Thanks in advance for any hints/suggestions.

JoshG
  • 6,472
  • 2
  • 38
  • 61
  • possible duplicate of [Save the console.log in Chrome to a file](http://stackoverflow.com/questions/7627113/save-the-console-log-in-chrome-to-a-file) –  Mar 04 '15 at 06:56
  • @torazaburo that's not a correct duplicate. This one is about intercepting the logging output, while the question you linked is about saving the output (possibly manually) to a file. – thomaux Mar 04 '15 at 07:01

2 Answers2

3

you can overwrite the console function(s) that you want to use

if(window.console && console.log){
   console.log = function(){
      var args = arguments;
      /* process args to your app */ 
   }
}
charlietfl
  • 170,828
  • 13
  • 121
  • 150
  • If you still want to have regular logging as well, you could do this by saving a reference to `console.log` before overwriting it. – thomaux Mar 04 '15 at 07:00
0

You can overload console.log with a function like the one below, and that function can save the messages in whatever form you'd like, as well as outputting to the console. This particular implementation is a little simplistic though as it only takes one argument, bu can google other examples pretty easily.

var myLog = [];

console.log = function (text) {
    console.info(text)
    myLog.push(text);
}
console.log("abc")
console.info(myLog);
lwalden
  • 813
  • 7
  • 11