diff --git a/core/utils/logger.js b/core/utils/logger.js index e0f8d4c..c1c42e4 100644 --- a/core/utils/logger.js +++ b/core/utils/logger.js @@ -1,47 +1,86 @@ -const chalk = require('chalk'); +/** + * A map of hardcoded ANSI escape codes for colors. + * Using these codes, we can style terminal output without any external dependencies. + */ +const ANSI_COLORS = { + RESET: '\x1b[0m', + GREEN_BRIGHT: '\x1b[92m', + YELLOW_BRIGHT: '\x1b[93m', + RED_BRIGHT: '\x1b[91m', + BLUE_BRIGHT: '\x1b[94m', + CYAN_BRIGHT: '\x1b[96m', + MAGENTA_BRIGHT: '\x1b[95m', + WHITE_BRIGHT: '\x1b[97m', + GRAY: '\x1b[90m', +}; class Logger { constructor(moduleName = 'APP') { this.moduleName = moduleName; - this.moduleColor = this._getModuleColor(moduleName); + // Store the raw color code for the module + this.moduleColorCode = this._getModuleColorCode(moduleName); } - _getModuleColor(moduleName) { + /** + * Hashes the module name to select a consistent color from a predefined list. + * @param {string} moduleName - The name of the module. + * @returns {string} The ANSI color code for the module. + */ + _getModuleColorCode(moduleName) { const colors = [ - chalk.default.cyanBright, - chalk.default.magentaBright, - chalk.default.yellowBright, - chalk.default.blueBright, - chalk.default.greenBright, - chalk.default.redBright, - chalk.default.whiteBright, - chalk.default.gray, - chalk.default.blackBright + ANSI_COLORS.CYAN_BRIGHT, + ANSI_COLORS.MAGENTA_BRIGHT, + ANSI_COLORS.YELLOW_BRIGHT, + ANSI_COLORS.BLUE_BRIGHT, + ANSI_COLORS.GREEN_BRIGHT, + ANSI_COLORS.RED_BRIGHT, + ANSI_COLORS.WHITE_BRIGHT, + ANSI_COLORS.GRAY, ]; let hash = 0; + if (moduleName.length === 0) return colors[0]; for (let i = 0; i < moduleName.length; i++) { hash = moduleName.charCodeAt(i) + ((hash << 5) - hash); + hash = hash & hash; // Convert to 32bit integer } const index = Math.abs(hash % colors.length); return colors[index]; } + /** + * Formats the log message with module and level-specific colors. + * @param {string} level - The log level (e.g., 'INFO', 'WARN'). + * @param {string} message - The main log message. + * @param {...any} args - Additional arguments to be logged. + * @returns {string} The fully formatted and colored log string. + */ _formatMessage(level, message, ...args) { - const coloredModuleName = this.moduleColor(`[${this.moduleName}]`); - let formattedMessage = `${coloredModuleName} ${message}`; + const coloredModuleName = `${this.moduleColorCode}[${this.moduleName}]${ANSI_COLORS.RESET}`; + // Join the main message and any additional arguments + const fullMessageContent = [message, ...args].join(' '); + + let levelColorCode; switch (level) { case 'INFO': - return chalk.default.greenBright(formattedMessage, ...args); + levelColorCode = ANSI_COLORS.GREEN_BRIGHT; + break; case 'WARN': - return chalk.default.yellowBright(formattedMessage, ...args); + levelColorCode = ANSI_COLORS.YELLOW_BRIGHT; + break; case 'ERROR': - return chalk.default.redBright(formattedMessage, ...args); + levelColorCode = ANSI_COLORS.RED_BRIGHT; + break; case 'DEBUG': - return chalk.default.blueBright(formattedMessage, ...args); + levelColorCode = ANSI_COLORS.BLUE_BRIGHT; + break; default: - return formattedMessage; + // For unknown levels, don't color the message part + return `${coloredModuleName} ${fullMessageContent}`; } + + const coloredMessage = `${levelColorCode}${fullMessageContent}${ANSI_COLORS.RESET}`; + return `${coloredModuleName} ${coloredMessage}`; } info(message, ...args) { @@ -57,7 +96,8 @@ class Logger { } debug(message, ...args) { - if (process.env.NODE_ENV === 'development') { // Only show debug logs in development + // Only show debug logs in development environment + if (process.env.NODE_ENV === 'development') { console.log(this._formatMessage('DEBUG', message, ...args)); } }