Skip to content

Instantly share code, notes, and snippets.

@ivikash
Created April 29, 2022 20:34
Show Gist options
  • Save ivikash/9bbee4854b9d9e16303accfa6fe280d2 to your computer and use it in GitHub Desktop.
Save ivikash/9bbee4854b9d9e16303accfa6fe280d2 to your computer and use it in GitHub Desktop.
winston browser console
import { LogEntry } from "winston";
import TransportStream from "winston-transport";
export type LoggerMethodsKeys =
| "error"
| "warn"
| "info"
| "http"
| "verbose"
| "debug"
| "silly";
export const LoggerMethods: Record<
LoggerMethodsKeys,
"error" | "info" | "warn" | "log" | "debug"
> = {
error: "error",
warn: "warn",
info: "info",
http: "log",
verbose: "log",
debug: "debug",
silly: "log",
};
interface loggerIcon {
icon: string;
fallbackIcon: string;
}
const LoggerIcons: Record<keyof typeof LoggerMethods, loggerIcon> = {
error: { icon: "🚨", fallbackIcon: "✖" },
warn: { icon: "❗", fallbackIcon: "⚠" },
info: { icon: "🤖", fallbackIcon: "i" },
http: { icon: "⛔", fallbackIcon: "⬢" },
verbose: { icon: "🟡", fallbackIcon: "▼" },
debug: { icon: "🪲", fallbackIcon: "☼" },
silly: { icon: "🙂", fallbackIcon: "☺" },
};
export class BrowserConsole extends TransportStream {
// eslint-disable-next-line class-methods-use-this
log(logEntry: LogEntry, next: () => void) {
const logMethod =
LoggerMethods[logEntry.level as keyof typeof LoggerMethods];
const logIcon = LoggerIcons[logEntry.level as keyof typeof LoggerIcons];
if (logMethod && logIcon) {
console[logMethod as typeof LoggerMethods[LoggerMethodsKeys]](
`${logIcon.icon} ${logIcon.fallbackIcon} [${logEntry.namespace}]`,
logEntry
);
} else {
console.warn("[ERROR] Invalid Logger Method Invoked");
}
next();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment