Merge pull request #55 from Deathgarden-Rebirth/log-cleanup

New Command: Log file clean up
This commit is contained in:
Vari 2025-01-06 11:15:20 +01:00 committed by GitHub
commit 8c68572494
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 73 additions and 1 deletions

View File

@ -0,0 +1,65 @@
<?php
namespace App\Console\Commands;
use Carbon\Carbon;
use DirectoryIterator;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\Storage;
use Str;
class CleanupLogs extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'app:cleanup-logs';
/**
* The console command description.
*
* @var string
*/
protected $description = 'Cleanup old log files.';
// Delete files older than this number of days.
const FILE_DAYS_OLD = 14;
// Delete session files older than this number of days.
const SESSION_FILE_DAYS_OLD = 7;
/**
* Execute the console command.
*/
public function handle()
{
$disk = Storage::disk('logs');
$files = $disk->allFiles();
$now = Carbon::now();
// Delete older modified files
foreach ($files as $file) {
$lastModified = Carbon::createFromTimestamp($disk->lastModified($file));
if(Str::startsWith($file, 'sessions/'))
$shouldDelete = $now->diff($lastModified)->total('days') * -1 > self::SESSION_FILE_DAYS_OLD;
else
$shouldDelete = $now->diff($lastModified)->total('days') * -1 > self::FILE_DAYS_OLD;
if($shouldDelete)
$disk->delete($file);
}
//Loop over all directories to delete empty ones.
$directories = $disk->allDirectories();
foreach ($directories as $directory) {
$files = $disk->allFiles($directory);
if(count($files) === 0)
$disk->deleteDirectory($directory);
}
}
}

View File

@ -16,6 +16,7 @@ protected function schedule(Schedule $schedule): void
$schedule->command('matchmaking:process')->everyTwentySeconds();
$schedule->command('matchmaking:cleanup')->everyThirtySeconds();
$schedule->command('app:generate-timed-challenges')->daily();
$schedule->command('app:cleanup-logs')->daily();
}
/**

View File

@ -44,6 +44,12 @@
'throw' => false,
],
'logs' => [
'driver' => 'local',
'root' => storage_path('logs'),
'throw' => false,
],
'patches' => [
'driver' => 'local',
'root' => storage_path('app/patches'),

View File

@ -59,7 +59,7 @@
],
'single' => [
'driver' => 'single',
'driver' => 'daily',
'path' => storage_path('logs/laravel.log'),
'level' => env('LOG_LEVEL', 'debug'),
'replace_placeholders' => true,