Skip to content

Instantly share code, notes, and snippets.

@ruvasik
Created February 6, 2024 09:40
Show Gist options
  • Save ruvasik/31f74b1948d405b1c9662cdf0442fc1c to your computer and use it in GitHub Desktop.
Save ruvasik/31f74b1948d405b1c9662cdf0442fc1c to your computer and use it in GitHub Desktop.
Controlled window.console
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