From 31df406f1e46b3adc13d6f056f64722430c7bbdc Mon Sep 17 00:00:00 2001 From: Yoeri Boven Date: Wed, 9 Oct 2024 20:08:45 +0200 Subject: [PATCH 1/2] wip --- README.md | 3 ++- src/Models/LogMessage.php | 13 +++++++++++ tests/DatabaseLoggingTest.php | 43 +++++++++++++++++++++++++++++++++++ 3 files changed, 58 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 123bdfb..b0f799a 100644 --- a/README.md +++ b/README.md @@ -43,7 +43,8 @@ return [ 'db' => [ 'driver' => 'custom', 'via' => DatabaseLogger::class, - 'connection' => env('LOG_DB_CONNECTION', env('DB_CONNECTION')) + 'connection' => env('LOG_DB_CONNECTION', env('DB_CONNECTION')), + 'days' => 7, ], ] ] diff --git a/src/Models/LogMessage.php b/src/Models/LogMessage.php index 082f367..1e9fa4b 100644 --- a/src/Models/LogMessage.php +++ b/src/Models/LogMessage.php @@ -3,10 +3,13 @@ namespace Yoeriboven\LaravelLogDb\Models; use Illuminate\Database\Eloquent\Casts\AsArrayObject; +use Illuminate\Database\Eloquent\MassPrunable; use Illuminate\Database\Eloquent\Model; class LogMessage extends Model { + use MassPrunable; + /** * The attributes that aren't mass assignable. * @@ -39,4 +42,14 @@ public function __construct(array $attributes = []) $this->setConnection($connection); } + + /** + * Get the prunable model query. + * + * @return \Illuminate\Database\Eloquent\Builder + */ + public function prunable() + { + return static::where('logged_at', '<=', now()->subDays(config('logging.channels.db.days'))); + } } diff --git a/tests/DatabaseLoggingTest.php b/tests/DatabaseLoggingTest.php index 84ede98..aec4532 100644 --- a/tests/DatabaseLoggingTest.php +++ b/tests/DatabaseLoggingTest.php @@ -1,6 +1,7 @@ 'Test message', ], 'custom'); }); + +it('prunes old logs', function () { + config()->set('logging.channels.db.days', 7); + + $message = LogMessage::create([ + 'level' => 200, + 'level_name' => 'INFO', + 'message' => 'Test message', + 'logged_at' => now()->subDays(7), + 'context' => [], + 'extra' => [], + ]); + + Artisan::call('model:prune', [ + '--model' => [ + LogMessage::class, + ] + ]); + + expect($message->fresh())->toBeNull(); +}); + +it('doesnt prune new logs', function () { + config()->set('logging.channels.db.days', 7); + + $message = LogMessage::create([ + 'level' => 200, + 'level_name' => 'INFO', + 'message' => 'Test message', + 'logged_at' => now()->subDays(7)->addSecond(), + 'context' => [], + 'extra' => [], + ]); + + Artisan::call('model:prune', [ + '--model' => [ + LogMessage::class, + ] + ]); + + expect($message->fresh())->not->toBeNull(); +}); \ No newline at end of file From d31e0a619fbb71eca5b177960a32675552747c8f Mon Sep 17 00:00:00 2001 From: yoeriboven Date: Wed, 9 Oct 2024 18:09:05 +0000 Subject: [PATCH 2/2] Fix styling --- tests/DatabaseLoggingTest.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/tests/DatabaseLoggingTest.php b/tests/DatabaseLoggingTest.php index aec4532..1c95ef8 100644 --- a/tests/DatabaseLoggingTest.php +++ b/tests/DatabaseLoggingTest.php @@ -83,7 +83,7 @@ Artisan::call('model:prune', [ '--model' => [ LogMessage::class, - ] + ], ]); expect($message->fresh())->toBeNull(); @@ -104,8 +104,8 @@ Artisan::call('model:prune', [ '--model' => [ LogMessage::class, - ] + ], ]); expect($message->fresh())->not->toBeNull(); -}); \ No newline at end of file +});