85 lines
1.9 KiB
JavaScript
85 lines
1.9 KiB
JavaScript
|
|
const LOG_LEVELS = {
|
||
|
|
debug: 10,
|
||
|
|
info: 20,
|
||
|
|
warn: 30,
|
||
|
|
error: 40,
|
||
|
|
silent: 50
|
||
|
|
};
|
||
|
|
|
||
|
|
const STORAGE_KEY = 'video_socket_log_level';
|
||
|
|
const DEFAULT_LEVEL = 'warn';
|
||
|
|
|
||
|
|
function normalizeLevel(level) {
|
||
|
|
if (!level) {
|
||
|
|
return DEFAULT_LEVEL;
|
||
|
|
}
|
||
|
|
|
||
|
|
const normalized = String(level).toLowerCase();
|
||
|
|
return Object.prototype.hasOwnProperty.call(LOG_LEVELS, normalized)
|
||
|
|
? normalized
|
||
|
|
: DEFAULT_LEVEL;
|
||
|
|
}
|
||
|
|
|
||
|
|
function getConfiguredLevel() {
|
||
|
|
try {
|
||
|
|
const queryLevel = new URLSearchParams(window.location.search).get('logLevel');
|
||
|
|
if (queryLevel) {
|
||
|
|
return normalizeLevel(queryLevel);
|
||
|
|
}
|
||
|
|
} catch (_error) {
|
||
|
|
}
|
||
|
|
|
||
|
|
try {
|
||
|
|
const storageLevel = localStorage.getItem(STORAGE_KEY);
|
||
|
|
if (storageLevel) {
|
||
|
|
return normalizeLevel(storageLevel);
|
||
|
|
}
|
||
|
|
} catch (_error) {
|
||
|
|
}
|
||
|
|
|
||
|
|
return DEFAULT_LEVEL;
|
||
|
|
}
|
||
|
|
|
||
|
|
function shouldLog(level) {
|
||
|
|
return LOG_LEVELS[level] >= LOG_LEVELS[getConfiguredLevel()];
|
||
|
|
}
|
||
|
|
|
||
|
|
function getConsoleMethod(level) {
|
||
|
|
switch (level) {
|
||
|
|
case 'debug':
|
||
|
|
return console.debug;
|
||
|
|
case 'info':
|
||
|
|
return console.info;
|
||
|
|
case 'warn':
|
||
|
|
return console.warn;
|
||
|
|
case 'error':
|
||
|
|
return console.error;
|
||
|
|
default:
|
||
|
|
return console.log;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
function emit(level, scope, args) {
|
||
|
|
if (!shouldLog(level)) {
|
||
|
|
return;
|
||
|
|
}
|
||
|
|
|
||
|
|
const prefix = scope ? `[${scope}]` : '[app]';
|
||
|
|
getConsoleMethod(level)(prefix, ...args);
|
||
|
|
}
|
||
|
|
|
||
|
|
export function createLogger(scope) {
|
||
|
|
return {
|
||
|
|
debug: (...args) => emit('debug', scope, args),
|
||
|
|
info: (...args) => emit('info', scope, args),
|
||
|
|
warn: (...args) => emit('warn', scope, args),
|
||
|
|
error: (...args) => emit('error', scope, args)
|
||
|
|
};
|
||
|
|
}
|
||
|
|
|
||
|
|
export function setBrowserLogLevel(level) {
|
||
|
|
const normalized = normalizeLevel(level);
|
||
|
|
localStorage.setItem(STORAGE_KEY, normalized);
|
||
|
|
return normalized;
|
||
|
|
}
|