Created
February 6, 2024 09:40
-
-
Save ruvasik/31f74b1948d405b1c9662cdf0442fc1c to your computer and use it in GitHub Desktop.
Controlled window.console
This file contains 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
export const log = ((c: Console) => { | |
const log = (...args: unknown[]) => { | |
if (!import.meta.env.PROD) c.log(...(args as any[])); | |
}; | |
log.warn = (...args: unknown[]) => { | |
if (!import.meta.env.PROD) c.warn(...(args as any[])); | |
}; | |
log.debug = (...args: (boolean | number | string | object)[]) => { | |
if (!import.meta.env.PROD) { | |
const formattedArgs = args.map((arg) => { | |
if (typeof arg === "object") { | |
return JSON.stringify(arg); | |
} | |
return arg.toString(); | |
}); | |
c.log.apply(console, ["%c" + formattedArgs.join(" "), "color: red;"]); | |
} | |
}; | |
log.prodWarn = (...args: unknown[]) => { | |
c.warn(...(args as any[])); | |
}; | |
log.error = (...args: unknown[]) => { | |
c.error(...(args as any[])); | |
}; | |
log.info = (...args: unknown[]) => { | |
c.info(...(args as any[])); | |
}; | |
return log; | |
})(console); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment