Skip to content

Instantly share code, notes, and snippets.

@typable
Created December 21, 2023 07:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save typable/7157363c5943504d487706534a28897e to your computer and use it in GitHub Desktop.
Save typable/7157363c5943504d487706534a28897e to your computer and use it in GitHub Desktop.
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