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.

2
  • possible duplicate of Save the console.log in Chrome to a file Commented Mar 4, 2015 at 6: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. Commented Mar 4, 2015 at 7:01

2 Answers 2

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

1 Comment

If you still want to have regular logging as well, you could do this by saving a reference to console.log before overwriting it.
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);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.