50 lines
1.3 KiB
PHP
50 lines
1.3 KiB
PHP
<?php
|
|
|
|
namespace App\Providers;
|
|
|
|
use App\Enums\LogLevelEnum;
|
|
use App\Events\ExceptionOccurred;
|
|
use App\Listeners\LogExceptionToDatabase;
|
|
use Error;
|
|
use Illuminate\Contracts\Debug\ExceptionHandler;
|
|
use Illuminate\Support\Facades\Event;
|
|
use Illuminate\Support\ServiceProvider;
|
|
use InvalidArgumentException;
|
|
use Throwable;
|
|
|
|
class AppServiceProvider extends ServiceProvider
|
|
{
|
|
public function register(): void
|
|
{
|
|
}
|
|
|
|
public function boot(): void
|
|
{
|
|
Event::listen(
|
|
ExceptionOccurred::class,
|
|
LogExceptionToDatabase::class,
|
|
);
|
|
|
|
Event::listen(
|
|
\App\Events\NewArticleFetched::class,
|
|
\App\Listeners\ValidateArticleListener::class,
|
|
);
|
|
|
|
|
|
app()->make(ExceptionHandler::class)
|
|
->reportable(function (Throwable $e) {
|
|
$level = $this->mapExceptionToLogLevel($e);
|
|
|
|
ExceptionOccurred::dispatch($e, $level, $e->getMessage(), []);
|
|
});
|
|
}
|
|
|
|
private function mapExceptionToLogLevel(Throwable $exception): LogLevelEnum
|
|
{
|
|
return match (true) {
|
|
$exception instanceof Error => LogLevelEnum::CRITICAL,
|
|
$exception instanceof InvalidArgumentException => LogLevelEnum::WARNING,
|
|
default => LogLevelEnum::ERROR,
|
|
};
|
|
}
|
|
}
|