-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
a2eb569
commit 8a902d9
Showing
8 changed files
with
156 additions
and
0 deletions.
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
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
23 changes: 23 additions & 0 deletions
23
database/migrations/2022_03_04_100000_add_increment_fields.php
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,23 @@ | ||
<?php | ||
|
||
use Illuminate\Database\Migrations\Migration; | ||
use Illuminate\Database\Schema\Blueprint; | ||
use Illuminate\Support\Facades\Schema; | ||
|
||
class AddIncrementFields extends Migration | ||
{ | ||
public function up(): void | ||
{ | ||
Schema::table('laravel_errors', function (Blueprint $table): void { | ||
$table->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'); | ||
}); | ||
} | ||
} |
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,16 @@ | ||
<?php | ||
|
||
namespace JustBetter\ErrorLogger; | ||
|
||
use Illuminate\Foundation\Support\Providers\EventServiceProvider as ServiceProvider; | ||
use JustBetter\ErrorLogger\Events\BeforeErrorCreate; | ||
use JustBetter\ErrorLogger\Listeners\IncrementExistingError; | ||
|
||
class EventServiceProvider extends ServiceProvider | ||
{ | ||
protected $listen = [ | ||
BeforeErrorCreate::class => [ | ||
IncrementExistingError::class | ||
] | ||
]; | ||
} |
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,16 @@ | ||
<?php | ||
|
||
namespace JustBetter\ErrorLogger\Events; | ||
|
||
use Illuminate\Foundation\Events\Dispatchable; | ||
use Illuminate\Queue\SerializesModels; | ||
use JustBetter\ErrorLogger\Models\Error; | ||
|
||
class BeforeErrorCreate | ||
{ | ||
use Dispatchable, SerializesModels; | ||
|
||
public function __construct(public Error $error) | ||
{ | ||
} | ||
} |
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,51 @@ | ||
<?php | ||
|
||
namespace JustBetter\ErrorLogger\Listeners; | ||
|
||
use JustBetter\ErrorLogger\Events\BeforeErrorCreate; | ||
use JustBetter\ErrorLogger\Models\Error; | ||
|
||
/** | ||
* Check if there is already an error that is the same as the one being reported. | ||
* If so, increment the count | ||
*/ | ||
class IncrementExistingError | ||
{ | ||
public function handle(BeforeErrorCreate $event): bool | ||
{ | ||
if ($event->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 | ||
} | ||
} |
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