2025-06-29 08:50:03 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
namespace App\Providers;
|
|
|
|
|
|
2025-06-29 18:33:18 +02:00
|
|
|
use App\Events\ExceptionOccurred;
|
|
|
|
|
use App\Listeners\LogExceptionToDatabase;
|
|
|
|
|
use App\LogLevelEnum;
|
2025-07-05 18:26:04 +02:00
|
|
|
use Error;
|
2025-06-29 18:33:18 +02:00
|
|
|
use Illuminate\Contracts\Debug\ExceptionHandler;
|
2025-06-29 09:48:45 +02:00
|
|
|
use Illuminate\Support\Facades\Event;
|
2025-06-29 08:50:03 +02:00
|
|
|
use Illuminate\Support\ServiceProvider;
|
2025-07-05 18:26:04 +02:00
|
|
|
use InvalidArgumentException;
|
2025-06-29 18:33:18 +02:00
|
|
|
use Throwable;
|
2025-06-29 08:50:03 +02:00
|
|
|
|
|
|
|
|
class AppServiceProvider extends ServiceProvider
|
|
|
|
|
{
|
|
|
|
|
public function register(): void
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function boot(): void
|
|
|
|
|
{
|
2025-06-29 18:33:18 +02:00
|
|
|
Event::listen(
|
|
|
|
|
ExceptionOccurred::class,
|
|
|
|
|
LogExceptionToDatabase::class,
|
|
|
|
|
);
|
|
|
|
|
|
2025-08-03 20:59:09 +02:00
|
|
|
Event::listen(
|
|
|
|
|
\App\Events\NewArticleFetched::class,
|
|
|
|
|
\App\Listeners\ValidateArticleListener::class,
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
Event::listen(
|
|
|
|
|
\App\Events\ArticleApproved::class,
|
|
|
|
|
\App\Listeners\PublishApprovedArticle::class,
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
Event::listen(
|
|
|
|
|
\App\Events\ArticleReadyToPublish::class,
|
|
|
|
|
\App\Listeners\PublishArticle::class,
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
Event::listen(
|
|
|
|
|
\App\Events\ExceptionLogged::class,
|
|
|
|
|
\App\Listeners\LogExceptionToDatabase::class,
|
|
|
|
|
);
|
|
|
|
|
|
2025-06-29 18:33:18 +02:00
|
|
|
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) {
|
2025-07-05 18:26:04 +02:00
|
|
|
$exception instanceof Error => LogLevelEnum::CRITICAL,
|
|
|
|
|
$exception instanceof InvalidArgumentException => LogLevelEnum::WARNING,
|
2025-06-29 18:33:18 +02:00
|
|
|
default => LogLevelEnum::ERROR,
|
|
|
|
|
};
|
2025-06-29 08:50:03 +02:00
|
|
|
}
|
|
|
|
|
}
|