123 lines
3.0 KiB
PHP
123 lines
3.0 KiB
PHP
<?php
|
|
|
|
namespace WizdomNetworks\WizeWeb\Utils;
|
|
|
|
use WizdomNetworks\WizeWeb\Utils\ErrorHandler;
|
|
|
|
/**
|
|
* Logger Utility
|
|
*
|
|
* A utility class for logging messages to a file with environment-based debug control.
|
|
*
|
|
* Supports different log levels and dynamic debugging based on an environment variable.
|
|
*/
|
|
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.
|
|
*/
|
|
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);
|
|
self::$debugEnabled = true;
|
|
}
|
|
|
|
/**
|
|
* Logs a message to the log file.
|
|
*
|
|
* @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) {
|
|
ErrorHandler::exception($e);
|
|
error_log("[ERROR] Logging to file failed: " . $e->getMessage());
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Logs a debug message (only if DEBUG is enabled).
|
|
*
|
|
* @param string $message The log message.
|
|
*/
|
|
public static function debug(string $message): void
|
|
{
|
|
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.
|
|
*
|
|
* @param string $message The log message.
|
|
*/
|
|
public static function warning(string $message): void
|
|
{
|
|
self::log('WARNING', $message);
|
|
}
|
|
|
|
/**
|
|
* Logs an error message.
|
|
*
|
|
* @param string $message The log message.
|
|
*/
|
|
public static function error(string $message): void
|
|
{
|
|
self::log('ERROR', $message);
|
|
}
|
|
|
|
/**
|
|
* Logs a critical error message.
|
|
*
|
|
* @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;
|
|
}
|
|
}
|
|
|
|
// Initialize Logger on class load
|
|
Logger::init();
|