added log rotation. fix use class
This commit is contained in:
@@ -3,13 +3,38 @@
|
||||
class BotLogger
|
||||
{
|
||||
const LOG_FILE = _PS_ROOT_DIR_ . '/var/logs/botlimiter_ban.log';
|
||||
const MAX_SIZE = 10485760; // 10 MB
|
||||
|
||||
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');
|
||||
$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);
|
||||
}
|
||||
|
||||
/**
|
||||
* 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);
|
||||
}
|
||||
}
|
||||
@@ -1,5 +1,4 @@
|
||||
<?php
|
||||
use PrestaShop\PrestaShop\Core\Crypto\PhpEncryption;
|
||||
|
||||
class FilterTrapRule implements RuleInterface
|
||||
{
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
<?php
|
||||
use PrestaShop\PrestaShop\Core\Crypto\PhpEncryption;
|
||||
|
||||
|
||||
class BotLimiterVerifyModuleFrontController extends ModuleFrontController
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user