From 8a902d92af41b75dd23bdd079598994dc944319f Mon Sep 17 00:00:00 2001 From: Vincent Boon Date: Fri, 4 Mar 2022 14:51:48 +0100 Subject: [PATCH] Group and statistics --- README.md | 9 ++++ config/laravel-error-logger.php | 13 +++++ ...2022_03_04_100000_add_increment_fields.php | 23 +++++++++ src/EventServiceProvider.php | 16 ++++++ src/Events/BeforeErrorCreate.php | 16 ++++++ src/Listeners/IncrementExistingError.php | 51 +++++++++++++++++++ src/Models/Error.php | 23 +++++++++ src/ServiceProvider.php | 5 ++ 8 files changed, 156 insertions(+) create mode 100644 database/migrations/2022_03_04_100000_add_increment_fields.php create mode 100644 src/EventServiceProvider.php create mode 100644 src/Events/BeforeErrorCreate.php create mode 100644 src/Listeners/IncrementExistingError.php diff --git a/README.md b/README.md index 431c101..7efe5d2 100644 --- a/README.md +++ b/README.md @@ -89,6 +89,15 @@ extra attributes compared to `withThrowable`: It is still possible to override these values when chaining them after the throwable. +### Grouping + +Errors will be automatically grouped. In the config you can set an interval for this. +If you don't want to group you can call `dontGroup()`. + +### Hiding from index + +You can hide errors from the index by calling `hideFromIndex()`. + ## License The MIT License (MIT). Please see [License File](LICENSE.md) for more information. diff --git a/config/laravel-error-logger.php b/config/laravel-error-logger.php index 7f28bcc..4a35d21 100644 --- a/config/laravel-error-logger.php +++ b/config/laravel-error-logger.php @@ -4,4 +4,17 @@ 'notification' => [ 'channel' => env('LARAVEL_ERROR_NOTIFICATION_CHANNEL', 'stack'), ], + + /* + |-------------------------------------------------------------------------- + | Increment Interval + |-------------------------------------------------------------------------- + | + | This value is how long error logs should be incremented when equals. + | When the same error is logged multiple times the original logged + | will be incremented. Allowed values: daily, weekly, monthly + | + */ + + 'increment_interval' => 'daily' ]; diff --git a/database/migrations/2022_03_04_100000_add_increment_fields.php b/database/migrations/2022_03_04_100000_add_increment_fields.php new file mode 100644 index 0000000..40f99ba --- /dev/null +++ b/database/migrations/2022_03_04_100000_add_increment_fields.php @@ -0,0 +1,23 @@ +integer('count')->default(1)->after('channel'); + $table->boolean('show_on_index')->default(true)->after('count'); + }); + } + + public function down(): void + { + Schema::table('laravel_errors', function (Blueprint $table): void { + $table->dropColumn('count', 'show_on_index'); + }); + } +} diff --git a/src/EventServiceProvider.php b/src/EventServiceProvider.php new file mode 100644 index 0000000..643ea0c --- /dev/null +++ b/src/EventServiceProvider.php @@ -0,0 +1,16 @@ + [ + IncrementExistingError::class + ] + ]; +} \ No newline at end of file diff --git a/src/Events/BeforeErrorCreate.php b/src/Events/BeforeErrorCreate.php new file mode 100644 index 0000000..3987c07 --- /dev/null +++ b/src/Events/BeforeErrorCreate.php @@ -0,0 +1,16 @@ +error->dontGroup) { + return true; + } + + $start = match (config('laravel-error-logger.increment_interval')) { + 'monthly' => now()->subMonth(), + 'weekly' => now()->subWeek(), + default => now()->startOfDay(), + }; + + $keys = [ + 'group', + 'message', + 'model_id', + 'model_type', + 'details', + 'code' + ]; + + $equalError = Error::query() + ->where($event->error->only($keys)) + ->whereDate('created_at', '>=', $start) + ->orderByDesc('id') + ->first(); + + if ($equalError === null) { + return true; + } + + $equalError->update([ + 'count' => $equalError->count + 1 + ]); + + return false; // Return false to not save this error + } +} \ No newline at end of file diff --git a/src/Models/Error.php b/src/Models/Error.php index 3034e83..8eabb68 100644 --- a/src/Models/Error.php +++ b/src/Models/Error.php @@ -3,7 +3,9 @@ namespace JustBetter\ErrorLogger\Models; use Carbon\Carbon; +use JustBetter\ErrorLogger\Listeners\IncrementExistingError; use JustBetter\ErrorLogger\Concerns\CanTruncate; +use JustBetter\ErrorLogger\Events\BeforeErrorCreate; use Throwable; use Illuminate\Database\Eloquent\Model; use Illuminate\Database\Eloquent\Builder; @@ -25,12 +27,15 @@ class Error extends Model { use CanTruncate; + public bool $dontGroup = false; + protected $table = 'laravel_errors'; protected $guarded = []; protected $casts = [ 'created_at' => 'datetime', + 'group_values' => 'array' ]; protected array $truncate = [ @@ -39,6 +44,10 @@ class Error extends Model 'vendor_trace' => 'text', ]; + protected $dispatchesEvents = [ + 'creating' => BeforeErrorCreate::class + ]; + public function __set($key, $value) { $newValue = $this->canTruncate($key) @@ -143,6 +152,20 @@ public function hasTrace(): bool return $this->trace !== null; } + public function dontGroup(): self + { + $this->dontGroup = true; + + return $this; + } + + public function hideFromIndex(): self + { + $this->show_on_index = false; + + return $this; + } + public function scopeYesterday(Builder $query): Builder { return $query->whereDate('created_at', '=', Carbon::yesterday()->format('Y-m-d')); diff --git a/src/ServiceProvider.php b/src/ServiceProvider.php index 9e338f5..928e086 100644 --- a/src/ServiceProvider.php +++ b/src/ServiceProvider.php @@ -7,6 +7,11 @@ class ServiceProvider extends BaseServiceProvider { + public function register() + { + $this->app->register(EventServiceProvider::class); + } + public function boot(): void { $this