Skip to content

Instantly share code, notes, and snippets.

@miguelrk
Created April 2, 2024 15:26
Show Gist options
  • Save miguelrk/36f68c12c9e70d939c2f4a1dd3e90f8c to your computer and use it in GitHub Desktop.
Save miguelrk/36f68c12c9e70d939c2f4a1dd3e90f8c to your computer and use it in GitHub Desktop.
Proxy the globalThis.console to silence selected messages by substring
/**
* 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