I want to embed a JS-Console within a website for extended debugging purposes. Are there any libraries or hooks available? How can I catch console.log messages?
To access the developer console in Chrome on Windows, use the menu on the right of the window, and choose Tools > JavaScript console: You'll see the console appear in the bottom part of the screen, and you should see the output from the page, howdy. html , appear in the console.
The console. log() method in HTML is used for writing a message in the console. It indicates an important message during testing of any program.
You can open the Browser Console in one of two ways: from the menu: select “Browser Console” from the Browser Tools submenu in the Firefox Menu (or Tools menu if you display the menu bar or are on macOS). from the keyboard: press Ctrl + Shift + J (or Cmd + Shift + J on a Mac).
How can I catch console.log messages?
You can monkey-patch the real console.log
method and do whatever you like with the input:
var realConsoleLog = console.log;
console.log = function () {
var message = [].join.call(arguments, " ");
// Display the message somewhere... (jQuery example)
$(".output").text(message);
realConsoleLog.apply(console, arguments);
};
Here's a working example. It logs calls to console.log
in the .output
element, as well as in the console like usual.
You can override console.log
<div id="console"></div>
script :
if (window.console) console = {
log: function(){
var output='',
console=document.getElementById('console');
for (var i=0;i<arguments.length;i++) {
output+=arguments[i]+' ';
}
console.innerText+=output+"\n";
}
};
//test
var test=12345;
console.log('test', 'xyz', test);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With