kist
Version:
Package Pipeline Processor
152 lines (151 loc) • 5.62 kB
JavaScript
;
// ============================================================================
// Import
// ============================================================================
Object.defineProperty(exports, "__esModule", { value: true });
exports.Logger = void 0;
const LoggerStyles_1 = require("./LoggerStyles");
// ============================================================================
// Class
// ============================================================================
/**
* Logger class for handling console messages.
* Singleton instance ensures consistent logging behavior throughout the
* application.
*/
class Logger {
// Constructor
// ========================================================================
/**
* Private constructor to enforce singleton pattern.
* @param logLevel - The log level for controlling log output.
*/
constructor(logLevel = "info") {
this.logLevel = logLevel;
}
// Singleton Methods
// ========================================================================
/**
* Retrieves the singleton instance of Logger.
* Initializes a non-verbose Logger instance if it hasn't been explicitly
* initialized.
*
* @returns The Logger instance.
*/
static getInstance(logLevel = "info") {
if (!Logger.instance) {
// Default to "info" level if not initialized explicitly
Logger.instance = new Logger(logLevel);
}
return Logger.instance;
}
/**
* Resets the Logger instance.
* Useful for testing or reinitializing the Logger during runtime.
*/
static resetInstance() {
Logger.instance = null;
}
// Logging Methods
// ========================================================================
/**
* Logs a message with a specific level if it meets the current log level.
*
* @param level - The log level (e.g., "INFO", "WARN", "ERROR").
* @param context - The context or class name where the log originates.
* @param message - The message content to log.
* @param fgStyle - The foreground color style to apply to the log level.
* @param bgStyle - The background color style to apply to the log level (default is reset).
*/
log(level, context, message, fgStyle, bgStyle = LoggerStyles_1.LoggerStyles.Reset) {
if (this.shouldLog(level)) {
const formattedMessage = `${fgStyle}${bgStyle}[${level.toUpperCase()}]${LoggerStyles_1.LoggerStyles.Reset} [${LoggerStyles_1.LoggerStyles.Cyan}${context}${LoggerStyles_1.LoggerStyles.Reset}] ${message}`;
console[level === "error" ? "error" : level === "warn" ? "warn" : "log"](formattedMessage);
}
}
/**
* Determines if a log should be displayed based on the current log level.
*
* @param level - The level of the log being checked.
* @returns True if the log should be displayed, otherwise false.
*/
shouldLog(level) {
const levels = ["debug", "info", "warn", "error"];
return levels.indexOf(level) >= levels.indexOf(this.logLevel);
}
/**
* Logs an informational message.
*
* @param context - The originating class or module.
* @param message - The informational message to log.
*/
logInfo(context, message) {
this.log("info", context, message, LoggerStyles_1.LoggerStyles.Blue);
}
/**
* Logs a warning message.
*
* @param context - The originating class or module.
* @param message - The warning message to log.
*/
logWarn(context, message) {
this.log("warn", context, message, LoggerStyles_1.LoggerStyles.Yellow);
}
/**
* Logs an error message.
*
* @param context - The originating class or module.
* @param message - The error message to log.
* @param error - (Optional) Additional error details.
*/
logError(context, message, error) {
const formattedMessage = this.formatError(message, error);
this.log("error", context, formattedMessage, LoggerStyles_1.LoggerStyles.Red, LoggerStyles_1.LoggerStyles.BgYellow);
}
/**
* Logs a debug message.
*
* @param context - The originating class or module.
* @param message - The debug message to log.
*/
logDebug(context, message) {
this.log("debug", context, message, LoggerStyles_1.LoggerStyles.Magenta);
}
// Utility Methods
// ========================================================================
/**
* Sets the log level dynamically.
*
* @param level - The log level to set (e.g., "debug", "info").
*/
setLogLevel(level) {
this.logLevel = level;
}
/**
* Formats an error message for logging.
* Combines a base message with additional error details if available.
*
* @param message - The base error message.
* @param error - Additional error information, such as an Error object.
* @returns A formatted string combining the message and error details.
*/
formatError(message, error) {
if (error instanceof Error) {
return `${message}: ${error.message}`;
}
else if (typeof error === "string") {
return `${message}: ${error}`;
}
else if (error) {
return `${message}: ${JSON.stringify(error)}`;
}
return message;
}
}
exports.Logger = Logger;
// Parameters
// ========================================================================
/**
* Singleton instance
*/
Logger.instance = null;