qerrors
Version:
Intelligent error handling middleware with AI-powered analysis, environment validation, caching, and production-ready logging. Provides OpenAI-based error suggestions, queue management, retry mechanisms, and comprehensive configuration options for Node.js
80 lines (75 loc) • 3.59 kB
Plain Text
/**
* Enhanced Logging System for Scrooge Payment API
*
* This module provides a comprehensive logging infrastructure that supports structured
* logging, multiple log levels, performance monitoring, and production-ready log
* management. It replaces basic console.log statements with a robust logging system
* designed for payment processing applications that require detailed audit trails.
*
* Architecture rationale:
* - Structured JSON logging for easy parsing by monitoring systems
* - Multiple log levels for appropriate filtering in different environments
* - Performance timing capabilities for monitoring API response times
* - Request correlation IDs for tracking user journeys across microservices
* - Security-aware logging that masks sensitive payment information
* - Environment-specific configuration for development vs production
*
* Business benefits:
* - Improved debugging capabilities reduce incident resolution time
* - Compliance support through comprehensive audit trails
* - Performance monitoring enables proactive optimization
* - Structured logs integrate with monitoring platforms for alerting
* - Security logging helps detect and respond to potential threats
*/
const fs = require('fs');
const path = require('path');
const { isOffline } = require('./offlineMode');
/**
* Log Levels Configuration
*
* Purpose: Defines hierarchical log levels for filtering and routing messages
* Each level has a numeric priority for comparison and filtering operations.
* Higher numbers indicate higher priority/severity levels.
*
* Level usage guidelines:
* - DEBUG: Detailed debugging information for development
* - INFO: General operational messages about system behavior
* - WARN: Warning conditions that should be noted but don't stop operation
* - ERROR: Error conditions that require attention
* - FATAL: Critical errors that may cause system shutdown
* - AUDIT: Security and compliance-related events requiring permanent retention
*/
const LOG_LEVELS = {
DEBUG: { priority: 10, color: '\x1b[36m', name: 'DEBUG' }, // Cyan
INFO: { priority: 20, color: '\x1b[32m', name: 'INFO' }, // Green
WARN: { priority: 30, color: '\x1b[33m', name: 'WARN' }, // Yellow
ERROR: { priority: 40, color: '\x1b[31m', name: 'ERROR' }, // Red
FATAL: { priority: 50, color: '\x1b[35m', name: 'FATAL' }, // Magenta
AUDIT: { priority: 60, color: '\x1b[34m', name: 'AUDIT' } // Blue
};
/**
* Logger Configuration
*
* Purpose: Centralizes logging configuration with environment-specific defaults
* The configuration adapts to different deployment environments while maintaining
* consistent logging behavior across the application.
*
* Configuration rationale:
* - Environment-based log level filtering reduces noise in production
* - File logging for persistence and monitoring system integration
* - Console logging for development debugging and container environments
* - Structured format enables automated log parsing and analysis
*/
class LoggerConfig {
constructor() {
this.logLevel = this.getLogLevel();
this.logDir = process.env.LOG_DIR || './logs';
this.enableFileLogging = process.env.ENABLE_FILE_LOGGING !== 'false';
this.enableConsoleLogging = process.env.ENABLE_CONSOLE_LOGGING !== 'false';
this.enableStructuredLogging = process.env.NODE_ENV === 'production';
this.maxLogFiles = parseInt(process.env.MAX_LOG_FILES) || 30;
this.maxLogSizeMB = parseInt(process.env.MAX_LOG_SIZE_MB) || 100;
this.ensureLogDirectory();
}
/**
* Environment-based Log Level Detection