From a7a81294a2ef53ede1b0e60f220b317e143011eb Mon Sep 17 00:00:00 2001 From: overplayed Date: Fri, 7 Feb 2025 13:22:34 -0500 Subject: [PATCH] unsuccessful slider rework --- app/Utils/Logger.php | 107 +++++++++++++++++++++++++++++-------------- 1 file changed, 72 insertions(+), 35 deletions(-) diff --git a/app/Utils/Logger.php b/app/Utils/Logger.php index df5b49d..7e69fa8 100644 --- a/app/Utils/Logger.php +++ b/app/Utils/Logger.php @@ -3,67 +3,111 @@ namespace WizdomNetworks\WizeWeb\Utils; use WizdomNetworks\WizeWeb\Utils\ErrorHandler; +use Exception; /** - * Logger Utility + * Logger Utility (v6) * - * A utility class for logging messages to a file with environment-based debug control. + * A utility class for logging messages to a file with debugging support. + * Supports different log levels and ensures error tracking for debugging. * - * Supports different log levels and dynamic debugging based on an environment variable. + * ## Features: + * - Debugging mode can be explicitly set per module or globally. + * - Logs errors, warnings, and informational messages. + * - Captures exceptions and handles them via `ErrorHandler`. + * - Logs critical errors and warnings to the system error log. + * + * ## Methods: + * - `log($level, $message)`: Logs a message with a given log level. + * - `debug($message)`: Logs a debug-level message when debugging is enabled. + * - `info($message)`: Logs an informational message. + * - `warning($message)`: Logs a warning message (also logs to system error log). + * - `error($message)`: Logs an error message (also logs to system error log). + * - `critical($message)`: Logs a critical error message (also logs to system error log). + * - `enableDebug() / disableDebug()`: Toggles debugging for the current module. + * - `init()`: Initializes the logger settings. */ class Logger { - /** - * @var string The path to the log file. - */ protected static string $logFile; - - /** - * @var bool Flag to enable or disable debug logging. - */ protected static bool $debugEnabled; /** - * Initialize Logger settings from environment variables. + * Initializes Logger settings. */ public static function init(): void { - - self::$logFile = $_ENV['LOG_DIRECTORY'] . "app.log" ?: __DIR__ . '/../../logs/app.log'; - //self::$debugEnabled = isset($_ENV['APP_DEBUG']) && filter_var($_ENV['APP_DEBUG'], FILTER_VALIDATE_BOOL); + $logDirectory = $_ENV['LOG_DIRECTORY'] ?? __DIR__ . '/../../logs'; + self::$logFile = rtrim($logDirectory, '/') . '/app.log'; + + if (!is_dir($logDirectory) || !is_writable($logDirectory)) { + error_log("[ERROR] Logger directory is not writable: " . $logDirectory); + } + + if ($_ENV['APP_DEBUG'] === 'true'){ + self::$debugEnabled = true; + } + else{ + self::$debugEnabled = false; + } + //self::$debugEnabled = false; // Default to false, can be toggled per module + error_log("[INIT] Logger Debug Enabled: " . (self::$debugEnabled ? 'true' : 'false')); + error_log("[INIT] Logger File Path: " . self::$logFile); + } + + /** + * Enables debugging for the current module. + */ + public static function enableDebug(): void + { self::$debugEnabled = true; } /** - * Logs a message to the log file. + * Disables debugging for the current module. + */ + public static function disableDebug(): void + { + self::$debugEnabled = false; + } + + /** + * Returns debugging status of the app/module + */ + public static function isDebugEnabled(): bool + { + return self::$debugEnabled; + } + + /** + * Logs a message. * * @param string $level The log level (DEBUG, INFO, WARNING, ERROR, CRITICAL). * @param string $message The log message. */ public static function log(string $level, string $message): void { - if ($level === 'DEBUG' && !self::$debugEnabled) { - return; // Skip debug logs if debugging is disabled - } - $timestamp = date('Y-m-d H:i:s'); $formattedMessage = "[$timestamp] [$level]: $message" . PHP_EOL; - + try { file_put_contents(self::$logFile, $formattedMessage, FILE_APPEND | LOCK_EX); - } catch (\Throwable $e) { + } catch (Exception $e) { ErrorHandler::exception($e); error_log("[ERROR] Logging to file failed: " . $e->getMessage()); } } /** - * Logs a debug message (only if DEBUG is enabled). + * Logs a debug message if debugging is enabled. * * @param string $message The log message. */ public static function debug(string $message): void { + if (!self::$debugEnabled) { + return; + } self::log('DEBUG', $message); } @@ -78,43 +122,36 @@ class Logger } /** - * Logs a warning message. + * Logs a warning message and sends it to the system error log. * * @param string $message The log message. */ public static function warning(string $message): void { self::log('WARNING', $message); + error_log("[WARNING]: " . $message); } /** - * Logs an error message. + * Logs an error message and sends it to the system error log. * * @param string $message The log message. */ public static function error(string $message): void { self::log('ERROR', $message); + error_log("[ERROR]: " . $message); } /** - * Logs a critical error message. + * Logs a critical error message and sends it to the system error log. * * @param string $message The log message. */ public static function critical(string $message): void { self::log('CRITICAL', $message); - } - - /** - * Sets the log file path dynamically. - * - * @param string $filePath The path to the log file. - */ - public static function setLogFile(string $filePath): void - { - self::$logFile = $filePath; + error_log("[CRITICAL]: " . $message); } }