File size: 1,643 Bytes
4df7cf6 2324ed0 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 | const DEFAULT_DISABLED_TYPES = ['env', 'migrate', 'migration'];
function normalizeType(value) {
return String(value || '')
.trim()
.toLowerCase()
.replace(/[^a-z0-9]+/g, '-')
.replace(/^-+|-+$/g, '');
}
function parseTypeList(value) {
return new Set(
String(value || '')
.split(',')
.map(normalizeType)
.filter(Boolean)
);
}
function configuredSets() {
const enabled = parseTypeList(process.env.LOG_ENABLED_TYPES);
const disabled = parseTypeList(process.env.LOG_DISABLED_TYPES || DEFAULT_DISABLED_TYPES.join(','));
return { enabled, disabled };
}
function typeFromMessage(args) {
const first = args[0];
if (typeof first !== 'string') return null;
const match = first.match(/^\[([^\]]+)\]/);
return match ? normalizeType(match[1]) : null;
}
function isTypeEnabled(type) {
if (!type) return true;
const { enabled, disabled } = configuredSets();
if (enabled.has('all') || enabled.has(type)) return true;
if (disabled.has('all') || disabled.has(type)) return false;
return true;
}
function shouldLog(args) {
return isTypeEnabled(typeFromMessage(args));
}
function install() {
if (console.__wifiBizLoggerInstalled) return;
for (const method of ['log', 'warn', 'error', 'info', 'debug']) {
const original = console[method]?.bind(console);
if (!original) continue;
console[method] = (...args) => {
if (shouldLog(args)) {
original(...args);
}
};
}
Object.defineProperty(console, '__wifiBizLoggerInstalled', {
value: true,
enumerable: false,
});
}
module.exports = {
install,
isTypeEnabled,
normalizeType,
};
|