Skip to content

Commit

Permalink
Merge pull request #55 from Deathgarden-Rebirth/log-cleanup
Browse files Browse the repository at this point in the history
New Command: Log file clean up
  • Loading branch information
Vari7921 authored Jan 6, 2025
2 parents e77eec4 + ad01c63 commit 8c68572
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 1 deletion.
65 changes: 65 additions & 0 deletions dist/app/Console/Commands/CleanupLogs.php
Original file line number Diff line number Diff line change
@@ -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);
}
}
}
1 change: 1 addition & 0 deletions dist/app/Console/Kernel.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}

/**
Expand Down
6 changes: 6 additions & 0 deletions dist/config/filesystems.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,12 @@
'throw' => false,
],

'logs' => [
'driver' => 'local',
'root' => storage_path('logs'),
'throw' => false,
],

'patches' => [
'driver' => 'local',
'root' => storage_path('app/patches'),
Expand Down
2 changes: 1 addition & 1 deletion dist/config/logging.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@
],

'single' => [
'driver' => 'single',
'driver' => 'daily',
'path' => storage_path('logs/laravel.log'),
'level' => env('LOG_LEVEL', 'debug'),
'replace_placeholders' => true,
Expand Down

0 comments on commit 8c68572

Please sign in to comment.