78 lines
2.3 KiB
PHP
78 lines
2.3 KiB
PHP
<?php
|
|
|
|
|
|
namespace App\Shell;
|
|
|
|
use App\Utility\ManageLog;
|
|
use App\View\Helper\CommonHelper;
|
|
use Cake\Datasource\ConnectionManager;
|
|
use Cake\Log\Log;
|
|
|
|
|
|
class LogsCleaningShell extends AppShell {
|
|
|
|
public function initialize() {}
|
|
public function main() {
|
|
|
|
$this->out('Start');
|
|
|
|
$this->logCleaning();
|
|
|
|
$this->out('Cleaning completed');
|
|
|
|
$this->out('End');
|
|
}
|
|
|
|
private function logCleaning() {
|
|
$cli_logData['action'] = "DELETED_LOG_FILES_FROM_15_DAYS_AGO_AND_FILE_SIZE_IN_BYTES";
|
|
$logDirectory = LOGS; // Path to logs directory
|
|
|
|
$main_interval = TOTAL_DAYS;
|
|
$total_file_delete_at_a_time = TOTAL_FILE_DELETE_AT_A_TIME;
|
|
$fileSize = 0;
|
|
$deleteFileCount = 0;
|
|
// Get current time and time 15 days ago
|
|
$currentDateTime = date('Y-m-d');
|
|
$fifteenDaysAgo = date('Y-m-d', strtotime('-15 days'));
|
|
// Get list of log files
|
|
$files = scandir($logDirectory);
|
|
// Iterate through each file
|
|
foreach ($files as $file) {
|
|
// Check if it's a log file
|
|
if ((strpos($file, '.log') !== false) && ($deleteFileCount>=0 && $deleteFileCount<$total_file_delete_at_a_time)) {
|
|
$filePath = $logDirectory . DS . $file;
|
|
$fileCreationTime = date('Y-m-d',filemtime($filePath)); // Get file creation time
|
|
$interval = $this->dateDiffInDays($currentDateTime,$fileCreationTime);
|
|
|
|
// Check if file was created exactly 15 days ago
|
|
if ($interval>$main_interval && $file != "cli-debug.log") {
|
|
// Get the file size
|
|
$fileSize = $fileSize + filesize($filePath);
|
|
// Remove the file
|
|
unlink($filePath);
|
|
|
|
$deleteFileCount++;
|
|
}
|
|
}
|
|
}
|
|
$cli_logData['file_size'] = $fileSize;
|
|
$cli_logData['deleted_file_count'] = $deleteFileCount;
|
|
$this->cronjobLog($cli_logData);
|
|
$this->out('Log cleanup complete.');
|
|
}
|
|
|
|
private function dateDiffInDays($to_date, $from_date) {
|
|
|
|
$day = 0;
|
|
// Calculating the difference in timestamps
|
|
$diff = strtotime($from_date) - strtotime($to_date);
|
|
|
|
// 1 day = 24 hours
|
|
// 24 * 60 * 60 = 86400 seconds
|
|
|
|
$day = abs(round($diff / 86400));
|
|
return (int)$day;
|
|
}
|
|
|
|
}
|