Created
December 21, 2023 07:59
-
-
Save typable/7157363c5943504d487706534a28897e to your computer and use it in GitHub Desktop.
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
const STORE_LOG_LEVEL = 'LOG_LEVEL'; | |
const STORE_LOG_MODULE = 'LOG_MODULE'; | |
export enum Level { | |
Debug = 3, | |
Info = 2, | |
Warn = 1, | |
Error = 0, | |
} | |
export function mod(module: string | null) { | |
return function(level: Level, ...args: unknown[]) { | |
if (module === getCurrentModule() || getCurrentModule() === null) { | |
if (level <= getCurrentLevel()) { | |
const [name, color] = getLevelPrefix(level); | |
console.log(`%c[${name}]${module ? ` (${module})` : ''}`, `color: ${color}`, ...args); | |
} | |
} | |
}; | |
} | |
export const log = mod(null); | |
function getCurrentLevel(): Level { | |
const level = localStorage.getItem(STORE_LOG_LEVEL); | |
if (level) { | |
switch (level.toLowerCase()) { | |
case 'debug': | |
return Level.Debug; | |
case 'info': | |
return Level.Info; | |
case 'warn': | |
return Level.Warn; | |
case 'error': | |
return Level.Error; | |
} | |
} | |
return Level.Info; | |
} | |
function getCurrentModule(): string | null { | |
return localStorage.getItem(STORE_LOG_MODULE); | |
} | |
function getLevelPrefix(level: Level): [string, string] { | |
switch (level) { | |
case Level.Debug: | |
return ['DEBUG', 'blue']; | |
case Level.Info: | |
return ['INFO', 'green']; | |
case Level.Warn: | |
return ['WARN', 'orange']; | |
case Level.Error: | |
return ['ERROR', 'red']; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment