Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Showing console errors and alerts in a div inside the page

I'm building a debugging tool for my web app and I need to show console errors in a div. I know I can use my own made console like object and use it, but for future use I need to send all console errors to window. Actually I want to catch console events.

like image 293
Mohsen Avatar asked Jul 06 '11 23:07

Mohsen


People also ask

Which method is used to display messages to the console?

console.info() : The console.info() is a method in JavaScript that is used to display the important messages to the console.

Is alert the same as console log?

} if you see in my 3 statement i.e alert(“Let's go down the first road!”); we are using 'alert' here to write a statement. But, when it comes to the 6th statement we are instructed to use 'console. log'. We use 'console.


2 Answers

Here's a way using closure, containing the old console log function in the scope of the new one.

console.log = (function (old_function, div_log) {      return function (text) {         old_function(text);         div_log.value += text;     }; } (console.log.bind(console), document.getElementById("error-log"))); 
like image 28
MST Avatar answered Oct 05 '22 17:10

MST


To keep the console working:

if (typeof console  != "undefined")      if (typeof console.log != 'undefined')         console.olog = console.log;     else         console.olog = function() {};  console.log = function(message) {     console.olog(message);     $('#debugDiv').append('<p>' + message + '</p>'); }; console.error = console.debug = console.info =  console.log 
like image 129
jzilla Avatar answered Oct 05 '22 18:10

jzilla