Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adds support for Laravel pruning #27

Merged
merged 2 commits into from
Oct 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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,
],
]
]
Expand Down
13 changes: 13 additions & 0 deletions src/Models/LogMessage.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down Expand Up @@ -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')));
}
}
43 changes: 43 additions & 0 deletions tests/DatabaseLoggingTest.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<?php

use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Facades\Artisan;
use Illuminate\Support\Facades\Log;
use Yoeriboven\LaravelLogDb\DatabaseLogger;
use Yoeriboven\LaravelLogDb\Models\LogMessage;
Expand Down Expand Up @@ -66,3 +67,45 @@
'message' => '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();
});