Created
April 29, 2022 20:34
-
-
Save ivikash/9bbee4854b9d9e16303accfa6fe280d2 to your computer and use it in GitHub Desktop.
winston browser 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
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