@waboyz-baileys/shared
Version:
40 lines (39 loc) • 1.53 kB
JavaScript
const COLORS = {
debug: '\x1b[36m',
info: '\x1b[32m',
warn: '\x1b[33m',
error: '\x1b[31m',
silent: '',
reset: '\x1b[0m',
binding: '\x1b[35m',
};
const LEVELS = ['debug', 'info', 'warn', 'error', 'silent'];
export const logger = (debug = true, level = 'debug', bindings = null) => {
const minLevelIdx = LEVELS.indexOf(level);
function format(logLevel, ...args) {
if (!debug)
return;
if (LEVELS.indexOf(logLevel) < minLevelIdx || level === 'silent')
return;
const now = new Date();
const time = now.toLocaleString('en-GB', { hour12: false }).replace(',', '');
const color = COLORS[logLevel] || '';
const bindingColor = COLORS.binding;
const reset = COLORS.reset;
let bindingStr = '';
if (bindings) {
bindingStr = ` ${bindingColor}[${Object.entries(bindings).map(([k, v]) => `${k}:${v}`).join(' ')}]${reset}`;
}
console.log(`${color}[${logLevel.toUpperCase()}] [${time}]${bindingStr}${reset}`, ...args);
}
const loggerInstance = {
level,
child: (childBindings) => logger(debug, level, bindings ? { ...bindings, ...childBindings } : childBindings),
trace: (...args) => format('debug', ...args),
debug: (...args) => format('debug', ...args),
info: (...args) => format('info', ...args),
warn: (...args) => format('warn', ...args),
error: (...args) => format('error', ...args),
};
return loggerInstance;
};