Last active
February 8, 2025 07:38
-
-
Save peshala-prabhapoorna/190c6cd99288d2d85ff401a54bee7b82 to your computer and use it in GitHub Desktop.
Selectively redirecting `console.log()` output to an element in the page.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<div id="output"></div> | |
<script src="redirectLog.js"></script> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* 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