Javascript: console.log to html
JavascriptJavascript Problem Overview
I would like to write the console.log output to a div layer.
For example:
document.write(console.log(5+1)); //Incorrect, random example
Can someone give me a solution to my problem?
Thank you.
EDIT:
what i meant is, for example:
console.log("hi");
and it shows the output "hi" on the screen.
Note: An example: http://labs.codecademy.com/#:workspace
Javascript Solutions
Solution 1 - Javascript
You can override the default implementation of console.log()
(function () {
var old = console.log;
var logger = document.getElementById('log');
console.log = function (message) {
if (typeof message == 'object') {
logger.innerHTML += (JSON && JSON.stringify ? JSON.stringify(message) : message) + '<br />';
} else {
logger.innerHTML += message + '<br />';
}
}
})();
Demo: Fiddle
Solution 2 - Javascript
Slight improvement on @arun-p-johny answer:
In html,
<pre id="log"></pre>
In js,
(function () {
var old = console.log;
var logger = document.getElementById('log');
console.log = function () {
for (var i = 0; i < arguments.length; i++) {
if (typeof arguments[i] == 'object') {
logger.innerHTML += (JSON && JSON.stringify ? JSON.stringify(arguments[i], undefined, 2) : arguments[i]) + '<br />';
} else {
logger.innerHTML += arguments[i] + '<br />';
}
}
}
})();
Start using:
console.log('How', true, new Date());
Solution 3 - Javascript
I come a bit late with a more advanced version of Arun P Johny's answer. His solution doesn't handle multiple console.log()
arguments and doesn't give an access to the original function.
Here's my version:
(function (logger) {
console.old = console.log;
console.log = function () {
var output = "", arg, i;
for (i = 0; i < arguments.length; i++) {
arg = arguments[i];
output += "<span class=\"log-" + (typeof arg) + "\">";
if (
typeof arg === "object" &&
typeof JSON === "object" &&
typeof JSON.stringify === "function"
) {
output += JSON.stringify(arg);
} else {
output += arg;
}
output += "</span> ";
}
logger.innerHTML += output + "<br>";
console.old.apply(undefined, arguments);
};
})(document.getElementById("logger"));
// Testing
console.log("Hi!", {a:3, b:6}, 42, true);
console.log("Multiple", "arguments", "here");
console.log(null, undefined);
console.old("Eyy, that's the old and boring one.");
body {background: #333;}
.log-boolean,
.log-undefined {color: magenta;}
.log-object,
.log-string {color: orange;}
.log-number {color: cyan;}
<pre id="logger"></pre>
I took it a tiny bit further and added a class to each log so you can color it. It outputs all arguments as seen in the Chrome console. You also have access to the old log via console.old()
.
Here's a minified version of the script above to paste inline, just for you:
<script>
!function(o){console.old=console.log,console.log=function(){var n,e,t="";for(e=0;e<arguments.length;e++)t+='<span class="log-'+typeof(n=arguments[e])+'">',"object"==typeof n&&"object"==typeof JSON&&"function"==typeof JSON.stringify?t+=JSON.stringify(n):t+=n,t+="</span> ";o.innerHTML+=t+"<br>",console.old.apply(void 0,arguments)}}
(document.body);
</script>
Replace document.body
in the parentheses with whatever element you wish to log into.
Solution 4 - Javascript
A little late to the party, but I took @Hristiyan Dodov's answer a bit further still.
All console methods are now rewired and in case of overflowing text, an optional autoscroll to bottom is included. Colors are now based on the logging method rather than the arguments.
rewireLoggingToElement(
() => document.getElementById("log"),
() => document.getElementById("log-container"), true);
function rewireLoggingToElement(eleLocator, eleOverflowLocator, autoScroll) {
fixLoggingFunc('log');
fixLoggingFunc('debug');
fixLoggingFunc('warn');
fixLoggingFunc('error');
fixLoggingFunc('info');
function fixLoggingFunc(name) {
console['old' + name] = console[name];
console[name] = function(...arguments) {
const output = produceOutput(name, arguments);
const eleLog = eleLocator();
if (autoScroll) {
const eleContainerLog = eleOverflowLocator();
const isScrolledToBottom = eleContainerLog.scrollHeight - eleContainerLog.clientHeight <= eleContainerLog.scrollTop + 1;
eleLog.innerHTML += output + "<br>";
if (isScrolledToBottom) {
eleContainerLog.scrollTop = eleContainerLog.scrollHeight - eleContainerLog.clientHeight;
}
} else {
eleLog.innerHTML += output + "<br>";
}
console['old' + name].apply(undefined, arguments);
};
}
function produceOutput(name, args) {
return args.reduce((output, arg) => {
return output +
"<span class=\"log-" + (typeof arg) + " log-" + name + "\">" +
(typeof arg === "object" && (JSON || {}).stringify ? JSON.stringify(arg) : arg) +
"</span> ";
}, '');
}
}
setInterval(() => {
const method = (['log', 'debug', 'warn', 'error', 'info'][Math.floor(Math.random() * 5)]);
console[method](method, 'logging something...');
}, 200);
#log-container { overflow: auto; height: 150px; }
.log-warn { color: orange }
.log-error { color: red }
.log-info { color: skyblue }
.log-log { color: silver }
.log-warn, .log-error { font-weight: bold; }
<div id="log-container">
<pre id="log"></pre>
</div>
Solution 5 - Javascript
Create an ouput
<div id="output"></div>
Write to it using JavaScript
var output = document.getElementById("output");
output.innerHTML = "hello world";
If you would like it to handle more complex output values, you can use JSON.stringify
var myObj = {foo: "bar"};
output.innerHTML = JSON.stringify(myObj);
Solution 6 - Javascript
This post has helped me a lot, and after a few iterations, this is what we use.
The idea is to post log messages and errors to HTML, for example if you need to debug JS and don't have access to the console.
You do need to change 'console.log' with 'logThis', as it is not recommended to change native functionality.
What you'll get:
- A plain and simple 'logThis' function that will display strings and objects along with current date and time for each line
- A dedicated window on top of everything else. (show it only when needed)
- Can be used inside '.catch' to see relevant errors from promises.
- No change of default console.log behavior
- Messages will appear in the console as well.
function logThis(message) {
// if we pass an Error object, message.stack will have all the details, otherwise give us a string
if (typeof message === 'object') {
message = message.stack || objToString(message);
}
console.log(message);
// create the message line with current time
var today = new Date();
var date = today.getFullYear() + '-' + (today.getMonth() + 1) + '-' + today.getDate();
var time = today.getHours() + ':' + today.getMinutes() + ':' + today.getSeconds();
var dateTime = date + ' ' + time + ' ';
//insert line
document.getElementById('logger').insertAdjacentHTML('afterbegin', dateTime + message + '<br>');
}
function objToString(obj) {
var str = 'Object: ';
for (var p in obj) {
if (obj.hasOwnProperty(p)) {
str += p + '::' + obj[p] + ',\n';
}
}
return str;
}
const object1 = {
a: 'somestring',
b: 42,
c: false
};
logThis(object1)
logThis('And all the roads we have to walk are winding, And all the lights that lead us there are blinding')
#logWindow {
overflow: auto;
position: absolute;
width: 90%;
height: 90%;
top: 5%;
left: 5%;
right: 5%;
bottom: 5%;
background-color: rgba(0, 0, 0, 0.5);
z-index: 20;
}
<div id="logWindow">
<pre id="logger"></pre>
</div>
Thanks this answer too, JSON.stringify() didn't work for this.