unsuccessful slider rework
This commit is contained in:
parent
df8929f65b
commit
a7a81294a2
|
|
@ -3,67 +3,111 @@
|
||||||
namespace WizdomNetworks\WizeWeb\Utils;
|
namespace WizdomNetworks\WizeWeb\Utils;
|
||||||
|
|
||||||
use WizdomNetworks\WizeWeb\Utils\ErrorHandler;
|
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
|
class Logger
|
||||||
{
|
{
|
||||||
/**
|
|
||||||
* @var string The path to the log file.
|
|
||||||
*/
|
|
||||||
protected static string $logFile;
|
protected static string $logFile;
|
||||||
|
|
||||||
/**
|
|
||||||
* @var bool Flag to enable or disable debug logging.
|
|
||||||
*/
|
|
||||||
protected static bool $debugEnabled;
|
protected static bool $debugEnabled;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Initialize Logger settings from environment variables.
|
* Initializes Logger settings.
|
||||||
*/
|
*/
|
||||||
public static function init(): void
|
public static function init(): void
|
||||||
{
|
{
|
||||||
|
$logDirectory = $_ENV['LOG_DIRECTORY'] ?? __DIR__ . '/../../logs';
|
||||||
self::$logFile = $_ENV['LOG_DIRECTORY'] . "app.log" ?: __DIR__ . '/../../logs/app.log';
|
self::$logFile = rtrim($logDirectory, '/') . '/app.log';
|
||||||
//self::$debugEnabled = isset($_ENV['APP_DEBUG']) && filter_var($_ENV['APP_DEBUG'], FILTER_VALIDATE_BOOL);
|
|
||||||
|
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;
|
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 $level The log level (DEBUG, INFO, WARNING, ERROR, CRITICAL).
|
||||||
* @param string $message The log message.
|
* @param string $message The log message.
|
||||||
*/
|
*/
|
||||||
public static function log(string $level, string $message): void
|
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');
|
$timestamp = date('Y-m-d H:i:s');
|
||||||
$formattedMessage = "[$timestamp] [$level]: $message" . PHP_EOL;
|
$formattedMessage = "[$timestamp] [$level]: $message" . PHP_EOL;
|
||||||
|
|
||||||
try {
|
try {
|
||||||
file_put_contents(self::$logFile, $formattedMessage, FILE_APPEND | LOCK_EX);
|
file_put_contents(self::$logFile, $formattedMessage, FILE_APPEND | LOCK_EX);
|
||||||
} catch (\Throwable $e) {
|
} catch (Exception $e) {
|
||||||
ErrorHandler::exception($e);
|
ErrorHandler::exception($e);
|
||||||
error_log("[ERROR] Logging to file failed: " . $e->getMessage());
|
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.
|
* @param string $message The log message.
|
||||||
*/
|
*/
|
||||||
public static function debug(string $message): void
|
public static function debug(string $message): void
|
||||||
{
|
{
|
||||||
|
if (!self::$debugEnabled) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
self::log('DEBUG', $message);
|
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.
|
* @param string $message The log message.
|
||||||
*/
|
*/
|
||||||
public static function warning(string $message): void
|
public static function warning(string $message): void
|
||||||
{
|
{
|
||||||
self::log('WARNING', $message);
|
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.
|
* @param string $message The log message.
|
||||||
*/
|
*/
|
||||||
public static function error(string $message): void
|
public static function error(string $message): void
|
||||||
{
|
{
|
||||||
self::log('ERROR', $message);
|
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.
|
* @param string $message The log message.
|
||||||
*/
|
*/
|
||||||
public static function critical(string $message): void
|
public static function critical(string $message): void
|
||||||
{
|
{
|
||||||
self::log('CRITICAL', $message);
|
self::log('CRITICAL', $message);
|
||||||
}
|
error_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;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue