Search code examples
javascriptconsoleconsole.log

Creating another instance of the javascript console


Is it possible to create another instance of the javascript console and use it at one's own discretion, such as placing content filed to console.log() in a div on your page)?


Solution

  • See this answer, which you could implement by grabbing the value of the stack and writing it to a div on your page.

    <div id="log"></div>
    

    var logBackup = console.log;
    var logMessages = [];
    
    console.log = function() {
        logMessages.push.apply(logMessages, arguments);
        document.getElementById('log').innerHTML = "";
        for(var i = 0; i < logMessages.length; i++){
          var pre = document.createElement("pre");
          pre.innerHTML = logMessages[i];
          document.getElementById('log').appendChild(pre);
        }
        logBackup.apply(console, arguments);
    };
    console.log("My name is joe.")
    console.log("My name is tom")
    

    https://jsfiddle.net/yak613/7g6kchox/