-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #55 from Deathgarden-Rebirth/log-cleanup
New Command: Log file clean up
- Loading branch information
Showing
4 changed files
with
73 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters