Zonies-Bot/functions/logger.js
2025-05-12 12:57:59 -05:00

85 lines
4.4 KiB
JavaScript

/**
* base code taken from https://github.com/Mirasaki/logger
*/
const chalk = require('chalk'),
moment = require('moment');
const tagList = {
SYSLOG: chalk.greenBright('[SYSLOG]'),
SYSERR: chalk.redBright('[SYSERR]'),
SUCCESS: chalk.greenBright('[SUCCESS]'),
INFO: chalk.blueBright('[INFO]'),
DEBUG: chalk.magentaBright('[DEBUG]'),
DATA: chalk.yellowBright('[DATA]'),
COMMAND: chalk.whiteBright('[CMD]'),
EVENT: chalk.cyanBright('[EVENT]'),
ERROR: chalk.redBright('[EVENT]'),
WARN: chalk.yellowBright('[WARN]')
};
const longestTagLength = Math.max(...Object.values(tagList).map(t => t.length));
const getTag = (tag) => `${tagList[tag]}${''.repeat(longestTagLength - tagList[tag].length)}`;
const timestamp = () => `${chalk.whiteBright.bold(`[${moment.utc().format('YYYY-MM-DD HH:mm:ss')}]`)}`;
module.exports = {
syslog: (type, str) => console.info(`${timestamp()} ${type ? `${getTag('SYSLOG')} ${chalk.whiteBright.bgBlue.bold(`[${type}]`)}:` : `${getTag('SYSLOG')}:`} ${str}`),
syserr: (type, str) => console.error(`${timestamp()} ${type ? `${getTag('SYSERR')} ${chalk.whiteBright.bgBlue.bold(`[${type}]`)}:` : `${getTag('SYSERR')} :`} ${str}`),
success: (type, str) => console.log(`${timestamp()} ${type ? `${getTag('SUCCESS')} ${chalk.whiteBright.bgBlue.bold(`[${type}]`)}:` : `${getTag('SUCCESS')}:`} ${str}`),
info: (type, str) => console.info(`${timestamp()} ${type ? `${getTag('INFO')} ${chalk.whiteBright.bgBlue.bold(`[${type}]`)}:` : `${getTag('INFO')}:`} ${str}`),
debug: (type, str) => console.log(`${timestamp()} ${type ? `${getTag('DEBUG')} ${chalk.whiteBright.bgBlue.bold(`[${type}]`)}:` : `${getTag('DEBUG')}:`} ${str}`),
data: (type, str) => console.log(`${timestamp()} ${type ? `${getTag('DATA')} ${chalk.whiteBright.bgBlue.bold(`[${type}]`)}:` : `${getTag('DATA')}:`} ${str}`),
command: (type, str) => console.log(`${timestamp()} ${type ? `${getTag('COMMAND')} ${chalk.whiteBright.bgBlue.bold(`[${type}]`)}:` : `${getTag('COMMAND')}:`} ${str}`),
event: (type, str) => console.log(`${timestamp()} ${type ? `${getTag('EVENT')} ${chalk.whiteBright.bgBlue.bold(`[${type}]`)}:` : `${getTag('EVENT')}:`} ${str}`),
error: (type, str) => console.log(`${timestamp()} ${type ? `${getTag('ERROR')} ${chalk.whiteBright.bgBlue.bold(`[${type}]`)}:` : `${getTag('ERROR')}:`} ${str}`),
warn: (type, str) => console.log(`${timestamp()} ${type ? `${getTag('WARN')} ${chalk.whiteBright.bgBlue.bold(`[${type}]`)}:` : `${getTag('WARN')}:`} ${str}`),
startLog: (identifier) => console.log(`${timestamp()} ${getTag('DEBUG')} ${chalk.greenBright('[START]')} ${identifier}`),
endLog: (identifier) => console.log(`${timestamp()} ${getTag('DEBUG')} ${chalk.redBright('[ END ]')} ${identifier}`),
timestamp,
getExecutionTime: (hrtime) => {
const timeSinceHrMs = (
process.hrtime(hrtime)[0] * 1000
+ hrtime[1] / 1000000
).toFixed(2);
return `${chalk.yellowBright(
(timeSinceHrMs / 1000).toFixed(2))
} seconds (${chalk.yellowBright(timeSinceHrMs)} ms)`;
},
printErr: (err) => {
if (!(err instanceof Error)) {
console.error(err)
return;
}
console.error(
!err.stack
? chalk.red(err)
: err.stack
.split('\n')
.map((msg, index) => {
if (index === 0) {
return chalk.red(msg);
}
const isFailedFunctionCall = index === 1;
const traceStartIndex = msg.indexOf('(');
const traceEndIndex = msg.lastIndexOf(')');
const hasTrace = traceStartIndex !== -1;
const functionCall = msg.slice(
msg.indexOf('at') + 3,
hasTrace ? traceStartIndex - 1 : msg.length
);
const trace = msg.slice(traceStartIndex, traceEndIndex + 1);
return ` ${chalk.grey('at')} ${isFailedFunctionCall
? `${chalk.redBright(functionCall)} ${chalk.red.underline(trace)}`
: `${chalk.greenBright(functionCall)} ${chalk.grey(trace)}`
}`;
})
.join('\n')
)
}
};