added log rotation. fix use class

This commit is contained in:
O K
2025-12-07 14:13:06 +02:00
parent bbe4168168
commit a2b7a68af0
3 changed files with 29 additions and 5 deletions

View File

@@ -3,13 +3,38 @@
class BotLogger class BotLogger
{ {
const LOG_FILE = _PS_ROOT_DIR_ . '/var/logs/botlimiter_ban.log'; const LOG_FILE = _PS_ROOT_DIR_ . '/var/logs/botlimiter_ban.log';
const MAX_SIZE = 10485760; // 10 MB
public static function logBan($ip, $reason) public static function logBan($ip, $reason)
{ {
// 1. Check file size before writing (The Safety Valve)
if (file_exists(self::LOG_FILE) && filesize(self::LOG_FILE) > self::MAX_SIZE) {
self::rotateLog();
}
$date = date('Y-m-d H:i:s'); $date = date('Y-m-d H:i:s');
$message = sprintf("[%s] [IP:%s] [REASON:%s]" . PHP_EOL, $date, $ip, $reason); $message = sprintf("[%s] [IP:%s] [REASON:%s]" . PHP_EOL, $date, $ip, $reason);
// Append to log file // 2. Append to log file
file_put_contents(self::LOG_FILE, $message, FILE_APPEND | LOCK_EX); file_put_contents(self::LOG_FILE, $message, FILE_APPEND | LOCK_EX);
} }
}
/**
* Rotates the log:
* 1. Deletes the .old file
* 2. Renames current .log to .old
* 3. Current logging continues in new empty file
*/
private static function rotateLog()
{
$backup_file = self::LOG_FILE . '.old';
// Remove ancient backup
if (file_exists($backup_file)) {
@unlink($backup_file);
}
// Rename current to backup
@rename(self::LOG_FILE, $backup_file);
}
}

View File

@@ -1,5 +1,4 @@
<?php <?php
use PrestaShop\PrestaShop\Core\Crypto\PhpEncryption;
class FilterTrapRule implements RuleInterface class FilterTrapRule implements RuleInterface
{ {

View File

@@ -1,5 +1,5 @@
<?php <?php
use PrestaShop\PrestaShop\Core\Crypto\PhpEncryption;
class BotLimiterVerifyModuleFrontController extends ModuleFrontController class BotLimiterVerifyModuleFrontController extends ModuleFrontController
{ {