All files / src Logger.ts

43.47% Statements 10/23
50% Branches 2/4
9.09% Functions 1/11
45.45% Lines 10/22

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119                                  1x 1x 1x 1x 1x 1x 1x 1x 1x           1x                                                                                                                                                                              
/**
 * Defines the severity levels for log messages.
 * 
 * This enum provides a standardized set of log levels to categorize the importance 
 * and urgency of log messages generated by the application.
 *
 * @enum {number}
 * 
 * @property {number} Emergency - Indicates a system-wide message that immediately jeopardizes the system and may require immediate action.
 * @property {number} Alert - A serious problem that requires immediate attention.
 * @property {number} Critical - A critical condition that, although not immediately life-threatening, requires immediate attention.
 * @property {number} Error - An error condition.
 * @property {number} Warning - A warning condition.
 * @property {number} Notice - A normal but significant condition.
 * @property {number} Informational - Informational messages.
 * @property {number} Debug - Debug-level messages for development and troubleshooting.
 */
export enum LogLevel {
    Emergency = 0,
    Alert = 1,
    Critical = 2,
    Error = 3,
    Warning = 4,
    Notice = 5,
    Informational = 6,
    Debug = 7
}
 
/**
 * A logger utility for standardized logging.
 */
export class Logger {
    private minLogLevel: LogLevel;
 
    /**
     * Creates a new Logger instance.
     * @param minLogLevel - The minimum log level to capture (default is Informational).
     */
    constructor(minLogLevel: LogLevel = LogLevel.Informational) {
        this.minLogLevel = minLogLevel;
    }
 
    /**
     * Logs a message if the specified level meets the minimum log level.
     * @param level - The log level for this message.
     * @param message - The log message.
     * @param context - Additional contextual information (optional).
     */
    private log(level: LogLevel, message: string, context?: Record<string, unknown>): void {
        Iif (level > this.minLogLevel) return;
 
        const logEntry = {
            timestamp: new Date().toISOString(),
            level: LogLevel[level].toLowerCase(),
            message,
            context,
        };
 
        // Output as JSON for easy parsing
        console.log(JSON.stringify(logEntry));
    }
 
    /**
     * Logs an emergency message (level 0).
     */
    public logEmergency(message: string, context?: Record<string, unknown>): void {
        this.log(LogLevel.Emergency, message, context);
    }
 
    /**
     * Logs an alert message (level 1).
     */
    public logAlert(message: string, context?: Record<string, unknown>): void {
        this.log(LogLevel.Alert, message, context);
    }
 
    /**
     * Logs a critical message (level 2).
     */
    public logCritical(message: string, context?: Record<string, unknown>): void {
        this.log(LogLevel.Critical, message, context);
    }
 
    /**
     * Logs an error message (level 3).
     */
    public logError(message: string, context?: Record<string, unknown>): void {
        this.log(LogLevel.Error, message, context);
    }
 
    /**
     * Logs a warning message (level 4).
     */
    public logWarning(message: string, context?: Record<string, unknown>): void {
        this.log(LogLevel.Warning, message, context);
    }
 
    /**
     * Logs a notice message (level 5).
     */
    public logNotice(message: string, context?: Record<string, unknown>): void {
        this.log(LogLevel.Notice, message, context);
    }
 
    /**
     * Logs an informational message (level 6).
     */
    public logInfo(message: string, context?: Record<string, unknown>): void {
        this.log(LogLevel.Informational, message, context);
    }
 
    /**
     * Logs a debug message (level 7).
     */
    public logDebug(message: string, context?: Record<string, unknown>): void {
        this.log(LogLevel.Debug, message, context);
    }
}