Skip to content

Instantly share code, notes, and snippets.

@bbottema
Created June 7, 2018 19:06
Show Gist options
  • Save bbottema/4b76f32a0ac3ff043613e0c2ec682e95 to your computer and use it in GitHub Desktop.
Save bbottema/4b76f32a0ac3ff043613e0c2ec682e95 to your computer and use it in GitHub Desktop.
Console log to HTML
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Log to HTML</title>
<link rel="icon" href="data:;base64,=">
<script>
(function(eleLocator) {
fixLoggingFunc('log');
fixLoggingFunc('debug');
fixLoggingFunc('warn');
fixLoggingFunc('error');
fixLoggingFunc('info');
function fixLoggingFunc(name) {
console['old' + name] = console[name];
console[name] = function() {
let 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>&nbsp;";
}
eleLocator().innerHTML += output + "<br>";
console['old' + name].apply(undefined, arguments);
};
}
})(() => document.getElementById("log"));
</script>
<style>
body {background: #333;}
.log-boolean,
.log-undefined {color: magenta;}
.log-object,
.log-string {color: orange;}
.log-number {color: cyan;}
</style>
</head>
<body class="language-java">
<pre id="log"></pre>
<script>
console.debug("Hi!", {a:3, b:6}, 42, true);
console.info("Multiple", "arguments", "here");
console.warn(null, undefined);
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment