diff --git a/src/Sentry/Laravel/ServiceProvider.php b/src/Sentry/Laravel/ServiceProvider.php index 42d51e6e..f50b1a6c 100644 --- a/src/Sentry/Laravel/ServiceProvider.php +++ b/src/Sentry/Laravel/ServiceProvider.php @@ -2,6 +2,7 @@ namespace Sentry\Laravel; +use Sentry\Logs\Logs; use Illuminate\Contracts\Config\Repository; use Illuminate\Contracts\Container\BindingResolutionException; use Illuminate\Contracts\Events\Dispatcher; @@ -163,6 +164,15 @@ protected function bindEvents(): void if (isset($userConfig['send_default_pii']) && $userConfig['send_default_pii'] !== false) { $handler->subscribeAuthEvents($dispatcher); } + + if (isset($userConfig['enable_logs']) && $userConfig['enable_logs'] === true && method_exists($this->app, 'terminating')) { + // Listen to the terminating event to flush the logs before the application ends + // This ensures that all logs are sent to Sentry even if the application ends unexpectedly + // We need to check for method existence here for Lumen since this method was only introduced in Lumen 9.1.4 + $this->app->terminating(static function () { + Logs::getInstance()->flush(); + }); + } } catch (BindingResolutionException $e) { // If we cannot resolve the event dispatcher we also cannot listen to events }