meocord
Version:
Decorator-based Discord bot framework built on discord.js. Brings NestJS-style controllers, dependency injection, guards, and testing utilities to bot development — with a full CLI and TypeScript-first design.
73 lines (70 loc) • 2.35 kB
JavaScript
import { inspect } from 'node:util';
import dayjs from 'dayjs';
import utc from 'dayjs/plugin/utc.js';
import timezone from 'dayjs/plugin/timezone.js';
import { loadMeoCordConfig } from '../util/meocord-config-loader.util.js';
import chalk from 'chalk';
dayjs.extend(utc);
dayjs.extend(timezone);
class Logger {
log(...args) {
this.logWithContext('log', args);
}
info(...args) {
this.logWithContext('log', args);
}
warn(...args) {
this.logWithContext('warn', args);
}
error(...args) {
this.logWithContext('error', args);
}
debug(...args) {
this.logWithContext('debug', args);
}
verbose(...args) {
this.logWithContext('log', args);
}
formatMessage(message, logType) {
if (typeof message === 'object' && message !== null) {
return inspect(message, {
showHidden: true,
depth: null,
colors: true,
compact: false,
showProxy: true
});
}
return (this.colorMap[logType] || ((msg)=>msg))(message);
}
logWithContext(logLevel, messages) {
if (messages.length === 0) return;
const config = loadMeoCordConfig();
const logType = logLevel.toUpperCase();
const applyColor = this.colorMap[logType] || ((msg)=>msg);
const formattedMessages = messages.map((message)=>this.formatMessage(message, logType));
const coloredAppName = config?.appName ? applyColor(chalk.bold(`[${config.appName}]`)) : undefined;
const timestamp = chalk.bold(dayjs().format('dddd, MMMM D, YYYY HH:mm:ss [UTC]Z'));
const coloredLogLevel = applyColor(chalk.bold(`[${logType}]`));
const coloredContext = this.context ? chalk.yellow.bold(`[${this.context}]`) : '';
const logTexts = [
coloredAppName,
timestamp,
coloredLogLevel,
coloredContext,
...formattedMessages
].filter((log)=>!!log);
console[logLevel](...logTexts);
}
constructor(context){
this.context = context;
this.colorMap = {
LOG: chalk.green,
INFO: chalk.cyan,
WARN: chalk.yellow,
ERROR: chalk.red,
DEBUG: chalk.magenta
};
}
}
export { Logger };