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

Group and statistics #3

Merged
merged 1 commit into from
Mar 4, 2022
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
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
13 changes: 13 additions & 0 deletions config/laravel-error-logger.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'
];
23 changes: 23 additions & 0 deletions database/migrations/2022_03_04_100000_add_increment_fields.php
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');
});
}
}
16 changes: 16 additions & 0 deletions src/EventServiceProvider.php
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
]
];
}
16 changes: 16 additions & 0 deletions src/Events/BeforeErrorCreate.php
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)
{
}
}
51 changes: 51 additions & 0 deletions src/Listeners/IncrementExistingError.php
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
}
}
23 changes: 23 additions & 0 deletions src/Models/Error.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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 = [
Expand All @@ -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)
Expand Down Expand Up @@ -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'));
Expand Down
5 changes: 5 additions & 0 deletions src/ServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,11 @@

class ServiceProvider extends BaseServiceProvider
{
public function register()
{
$this->app->register(EventServiceProvider::class);
}

public function boot(): void
{
$this
Expand Down