Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Embed JS Console within website

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?

like image 406
NaN Avatar asked Oct 28 '13 13:10

NaN


People also ask

How do I add a console to my website?

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.

Can I add console log in HTML?

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.

How do I show console in browser?

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).


2 Answers

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.

like image 160
James Allardice Avatar answered Sep 21 '22 21:09

James Allardice


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);
like image 44
davidkonrad Avatar answered Sep 20 '22 21:09

davidkonrad