6090508bad
- Add curly braces to all if/else/for/while statements - Fix indentation and trailing spaces - Auto-fixed 372 linting errors using eslint --fix - Remaining issues are warnings only (non-null assertions, explicit any types)
45 lines
1.0 KiB
TypeScript
45 lines
1.0 KiB
TypeScript
/** Simple log-level utility.
|
|
*
|
|
* Default level is `info`, which suppresses `debug` output.
|
|
* Set to `debug` (via config or `setLevel()`) to see all messages.
|
|
*/
|
|
|
|
export type LogLevel = 'debug' | 'info' | 'warn' | 'error' | 'silent';
|
|
|
|
const LEVELS: Record<LogLevel, number> = {
|
|
debug: 0,
|
|
info: 1,
|
|
warn: 2,
|
|
error: 3,
|
|
silent: 4,
|
|
};
|
|
|
|
let currentLevel: LogLevel = 'info';
|
|
|
|
export function setLogLevel(level: LogLevel): void {
|
|
currentLevel = level;
|
|
}
|
|
|
|
export function getLogLevel(): LogLevel {
|
|
return currentLevel;
|
|
}
|
|
|
|
function shouldLog(level: LogLevel): boolean {
|
|
return LEVELS[level] >= LEVELS[currentLevel];
|
|
}
|
|
|
|
export const logger = {
|
|
debug(...args: unknown[]): void {
|
|
if (shouldLog('debug')) {console.debug(...args);}
|
|
},
|
|
info(...args: unknown[]): void {
|
|
if (shouldLog('info')) {console.log(...args);}
|
|
},
|
|
warn(...args: unknown[]): void {
|
|
if (shouldLog('warn')) {console.warn(...args);}
|
|
},
|
|
error(...args: unknown[]): void {
|
|
if (shouldLog('error')) {console.error(...args);}
|
|
},
|
|
};
|