- it's possible to catch the browser console log and display it in html element i know this question is asked many times before but what i found is just they are overwrite
console.log()
function likeconsole.log = (masg) => elem.innerHTML = msg
i think that it's nothing it's just add new function with the same name ofconsole.log
but i need to get the value of console after it's gone through it and displayed the error or objects, array, etc.. result - my question is how can i get all the log of the console and display it in html element not just errors but also testing results can anyone help me
Asked
Active
Viewed 1,640 times
0

Mahmoud Y3c
- 94
- 1
- 9
-
Why do you not want to overwrite `console.log`? – Unmitigated Apr 02 '21 at 23:05
-
I don't want to overwrite the `console.log` i just need to get the testing results and error of the `console` – Mahmoud Y3c Apr 02 '21 at 23:06
-
"it's possible to catch the browser console log and display it in html element ..." is an assertion which is not true. The answer to your question lies more with implementing the equivalent of `console` in script. My toolbox version of that is unpublished, but you might try searching for "remote console". – traktor Apr 02 '21 at 23:50
1 Answers
0
You can use this code
console.log = function(...logs)
{
var text = logs.map(e=> JSON.stringify(e)).join(' ');
elem.innerHTML = text;
}
Beware of circular structures. It brings error.
Extended variant
var consoleLog = console.log;
console.log = console.error = console.warn = function(...logs) {
var text = logs.map(e=> {
if (e instanceof Error) return `Error: ${e.message}`;
else if (Array.isArray(e)) return `<ul>${e.map(l => `<li>${l}</li>`).join('')}</ul>`;
// other stuff
else {
try {
return JSON.stringify(e);
} catch(e) {
consoleLog(...logs);
return `${e.message}<br/>See the console for details`;
}
}
}).join(' ');
elem.innerHTML = text;
}

Peter Hutsul
- 111
- 1
- 6
-
How you are understand my question i don't want to overwrite the ` console log ` i want to get all the testing results and errors from the Browser `console.log` and display it inside html element – Mahmoud Y3c Apr 02 '21 at 23:56
-
-
Maybe this answer can help you https://stackoverflow.com/questions/19846078/how-to-read-from-chromes-console-in-javascript/19846113 – Peter Hutsul Apr 03 '21 at 00:36