enum LOG_LEVEL { DEBUG, INFO, WARN, ERROR, } let longestScope = 0; let logLevel = LOG_LEVEL.DEBUG; export function setLogLevel(level: LOG_LEVEL) { logLevel = level; } const getPrefix = (scope: string) => `[${scope.padEnd(longestScope, " ")}]`; type LoggerOptions = { enabled?: boolean; }; export function createLogger(scope: string, _options?: LoggerOptions) { longestScope = Math.max(scope.length, longestScope); function debug(...data: unknown[]) { if (logLevel !== LOG_LEVEL.DEBUG) return; console.debug(getPrefix(scope), ...data); } function info(...data: unknown[]) { if (logLevel !== LOG_LEVEL.DEBUG && logLevel !== LOG_LEVEL.INFO) return; console.info(getPrefix(scope), ...data); } function warn(...data: unknown[]) { if ( logLevel !== LOG_LEVEL.DEBUG && logLevel !== LOG_LEVEL.INFO && logLevel !== LOG_LEVEL.WARN ) return; console.warn(getPrefix(scope), ...data); } function error(...data: unknown[]) { console.error(getPrefix(scope), ...data); } return { debug, info, error, warn, }; } const log = createLogger("");