71 lines
1.8 KiB
PHP
71 lines
1.8 KiB
PHP
<?php
|
|
if (!defined("_PS_VERSION_")) {
|
|
exit();
|
|
}
|
|
|
|
class EcomZoneLogger
|
|
{
|
|
private static $logFile = 'ecomzone.log';
|
|
private static $maxLogSize = 10485760; // 10MB
|
|
|
|
public static function log($message, $level = "INFO", $context = [])
|
|
{
|
|
try {
|
|
$logDir = _PS_MODULE_DIR_ . 'ecomzone/log/';
|
|
if (!is_dir($logDir)) {
|
|
mkdir($logDir, 0755, true);
|
|
}
|
|
|
|
$timestamp = date('Y-m-d H:i:s');
|
|
$contextStr = !empty($context) ? json_encode($context) : '';
|
|
|
|
$logMessage = sprintf(
|
|
"[%s] %s: %s %s\n",
|
|
$timestamp,
|
|
$level,
|
|
$message,
|
|
$contextStr
|
|
);
|
|
|
|
file_put_contents(
|
|
$logDir . self::$logFile,
|
|
$logMessage,
|
|
FILE_APPEND
|
|
);
|
|
} catch (Exception $e) {
|
|
// Fallback to PrestaShop's logger
|
|
PrestaShopLogger::addLog(
|
|
"EcomZone: $message",
|
|
($level === 'ERROR' ? 3 : 1)
|
|
);
|
|
}
|
|
}
|
|
|
|
private static function initLogFile()
|
|
{
|
|
if (!file_exists(self::$logFile)) {
|
|
$logDir = dirname(self::$logFile);
|
|
if (!is_dir($logDir)) {
|
|
mkdir($logDir, 0755, true);
|
|
}
|
|
touch(self::$logFile);
|
|
chmod(self::$logFile, 0666);
|
|
}
|
|
}
|
|
|
|
private static function rotateLogIfNeeded()
|
|
{
|
|
if (!file_exists(self::$logFile)) {
|
|
return;
|
|
}
|
|
|
|
if (filesize(self::$logFile) > self::$maxLogSize) {
|
|
$backup = self::$logFile . "." . date("Y-m-d-H-i-s") . ".bak";
|
|
rename(self::$logFile, $backup);
|
|
touch(self::$logFile);
|
|
chmod(self::$logFile, 0666);
|
|
}
|
|
}
|
|
}
|
|
|