WizdomWeb/app/Utils/Logger.php

160 lines
4.6 KiB
PHP

<?php
namespace WizdomNetworks\WizeWeb\Utils;
use WizdomNetworks\WizeWeb\Utils\ErrorHandler;
use Exception;
/**
* Logger Utility (v6)
*
* A utility class for logging messages to a file with debugging support.
* Supports different log levels and ensures error tracking for debugging.
*
* ## 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
{
protected static string $logFile;
protected static bool $debugEnabled;
/**
* Initializes Logger settings.
*/
public static function init(): void
{
$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;
}
/**
* 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
{
$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 (Exception $e) {
ErrorHandler::exception($e);
error_log("[ERROR] Logging to file failed: " . $e->getMessage());
}
}
/**
* 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);
}
/**
* Logs an informational message.
*
* @param string $message The log message.
*/
public static function info(string $message): void
{
self::log('INFO', $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 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 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);
error_log("[CRITICAL]: " . $message);
}
}
// Initialize Logger on class load
Logger::init();