Created
April 2, 2024 15:26
-
-
Save miguelrk/36f68c12c9e70d939c2f4a1dd3e90f8c to your computer and use it in GitHub Desktop.
Proxy the globalThis.console to silence selected messages by substring
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
/** | |
* Silence globalThis.console for selected messages by substrings | |
* | |
* @param substringsToSkip {string[]} - substrings to skip | |
* @returns {Proxy} - a proxied console object | |
*/ | |
export const proxyConsole = (...substringsToSkip: string[]) => { | |
return new Proxy(console, { | |
get(target, prop, _receiver) { | |
const method = target[prop as keyof typeof console]; // intercept method calls | |
// deno-lint-ignore no-explicit-any | |
return (...args: any[]) => { | |
const message = args.join(" "); | |
const skip = substringsToSkip.some((s) => message.includes(s)); | |
if (!skip) method.apply(target, args); | |
}; | |
}, | |
}); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment