Skip to content

Instantly share code, notes, and snippets.

@peshala-prabhapoorna
Last active February 8, 2025 07:38
Show Gist options
  • Save peshala-prabhapoorna/190c6cd99288d2d85ff401a54bee7b82 to your computer and use it in GitHub Desktop.
Save peshala-prabhapoorna/190c6cd99288d2d85ff401a54bee7b82 to your computer and use it in GitHub Desktop.
Selectively redirecting `console.log()` output to an element in the page.
<div id="output"></div>
<script src="redirectLog.js"></script>
/* Create an instance of Logger class in any scope where console.log should be
overridden and name it `console`. */
class Logger {
log(str) {
const output = document.querySelector('#output');
output.textContent = str;
}
}
const override = () => {
const console = new Logger();
console.log('console.log() overridden: logs to the output element');
};
override();
console.log('as usual: logs to the console');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment