UNPKG

@wgtechlabs/log-engine

Version:

A lightweight, security-first logging utility with automatic data redaction for Node.js applications - the first logging library with built-in PII protection.

137 lines 6.65 kB
"use strict"; /** * Core message formatting functionality * Handles the main log message formatting with colors, timestamps, and levels */ Object.defineProperty(exports, "__esModule", { value: true }); exports.MessageFormatter = void 0; const types_1 = require("../types/index.cjs"); const colors_1 = require("./colors.cjs"); const timestamp_1 = require("./timestamp.cjs"); const data_formatter_1 = require("./data-formatter.cjs"); /** * Core message formatter class * Provides the main formatting functionality for log messages */ class MessageFormatter { /** * Formats a log message with timestamp, level indicator, and appropriate coloring * Creates a structured log entry: [ISO_TIMESTAMP][LOCAL_TIME][LEVEL]: message [data] * @param level - The log level to format for * @param message - The message content to format * @param data - Optional data object to include in the log output * @param formatConfig - Optional format configuration to control element inclusion * @returns Formatted string with ANSI colors and timestamps */ static format(level, message, data, formatConfig) { // Merge provided format configuration with the default configuration const config = { ...MessageFormatter.DEFAULT_FORMAT_CONFIG, ...formatConfig }; // Build timestamp string conditionally let timestamp = ''; if (config.includeIsoTimestamp || config.includeLocalTime) { const { isoTimestamp, timeString } = (0, timestamp_1.getTimestampComponents)(); if (config.includeIsoTimestamp && config.includeLocalTime) { // Both timestamps included timestamp = (0, timestamp_1.formatTimestamp)(isoTimestamp, timeString, colors_1.colorScheme); } else if (config.includeIsoTimestamp) { // Only ISO timestamp timestamp = `${colors_1.colorScheme.timestamp}[${isoTimestamp}]${colors_1.colors.reset}`; } else if (config.includeLocalTime) { // Only local time timestamp = `${colors_1.colorScheme.timeString}[${timeString}]${colors_1.colors.reset}`; } } const levelName = MessageFormatter.getLevelName(level); const levelColor = MessageFormatter.getLevelColor(level); const coloredLevel = `${levelColor}[${levelName}]${colors_1.colors.reset}`; // Format the base message (level is always included as per requirements) let formattedMessage = `${timestamp}${coloredLevel}: ${message}`; // Append data if provided if (data !== undefined) { const dataString = (0, data_formatter_1.formatData)(data); const styledData = (0, data_formatter_1.styleData)(dataString, colors_1.colorScheme); formattedMessage += styledData; } // Always reset colors at the end of the entire log line return formattedMessage + colors_1.colors.reset; } /** * Formats a Log Engine system message with [LOG ENGINE] prefix instead of log levels * Used for internal messages like deprecation warnings that should be distinguished from user logs * @param message - The system message content to format * @param formatConfig - Optional format configuration to control element inclusion * @returns Formatted string with ANSI colors, timestamps, and LOG ENGINE prefix */ static formatSystemMessage(message, formatConfig) { // Merge provided format configuration with the default configuration const config = { ...MessageFormatter.DEFAULT_FORMAT_CONFIG, ...formatConfig }; // Build timestamp string conditionally let timestamp = ''; if (config.includeIsoTimestamp || config.includeLocalTime) { const { isoTimestamp, timeString } = (0, timestamp_1.getTimestampComponents)(); if (config.includeIsoTimestamp && config.includeLocalTime) { // Both timestamps included timestamp = (0, timestamp_1.formatTimestamp)(isoTimestamp, timeString, colors_1.colorScheme); } else if (config.includeIsoTimestamp) { // Only ISO timestamp timestamp = `${colors_1.colorScheme.timestamp}[${isoTimestamp}]${colors_1.colors.reset}`; } else if (config.includeLocalTime) { // Only local time timestamp = `${colors_1.colorScheme.timeString}[${timeString}]${colors_1.colors.reset}`; } } const coloredLogEngine = `${colors_1.colorScheme.system}[LOG ENGINE]${colors_1.colors.reset}`; const coloredMessage = `${colors_1.colorScheme.system}${message}${colors_1.colors.reset}`; return `${timestamp}${coloredLogEngine}: ${coloredMessage}`; } /** * Converts LogLevel enum to human-readable string * @param level - The LogLevel to convert * @returns String representation of the log level */ static getLevelName(level) { switch (level) { case types_1.LogLevel.DEBUG: return 'DEBUG'; case types_1.LogLevel.INFO: return 'INFO'; case types_1.LogLevel.WARN: return 'WARN'; case types_1.LogLevel.ERROR: return 'ERROR'; case types_1.LogLevel.LOG: return 'LOG'; default: return 'UNKNOWN'; } } /** * Maps LogLevel to appropriate ANSI color code * Colors help quickly identify message severity in console output * @param level - The LogLevel to get color for * @returns ANSI color escape sequence */ static getLevelColor(level) { switch (level) { case types_1.LogLevel.DEBUG: return colors_1.colors.magenta; // Purple for debug info case types_1.LogLevel.INFO: return colors_1.colors.blue; // Blue for general info case types_1.LogLevel.WARN: return colors_1.colors.yellow; // Yellow for warnings case types_1.LogLevel.ERROR: return colors_1.colors.red; // Red for errors case types_1.LogLevel.LOG: return colors_1.colors.green; // Green for always-on log messages default: return colors_1.colors.white; // White for unknown levels } } } exports.MessageFormatter = MessageFormatter; /** * Default format configuration to avoid object recreation on every call */ MessageFormatter.DEFAULT_FORMAT_CONFIG = { includeIsoTimestamp: true, includeLocalTime: true }; //# sourceMappingURL=message-formatter.js.map