Skip to content

Commit d7ab8df

Browse files
authored
Ensure we always flush logs after the app terminates (#1052)
1 parent 938f9e9 commit d7ab8df

File tree

1 file changed

+10
-0
lines changed

1 file changed

+10
-0
lines changed

src/Sentry/Laravel/ServiceProvider.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace Sentry\Laravel;
44

5+
use Sentry\Logs\Logs;
56
use Illuminate\Contracts\Config\Repository;
67
use Illuminate\Contracts\Container\BindingResolutionException;
78
use Illuminate\Contracts\Events\Dispatcher;
@@ -163,6 +164,15 @@ protected function bindEvents(): void
163164
if (isset($userConfig['send_default_pii']) && $userConfig['send_default_pii'] !== false) {
164165
$handler->subscribeAuthEvents($dispatcher);
165166
}
167+
168+
if (isset($userConfig['enable_logs']) && $userConfig['enable_logs'] === true && method_exists($this->app, 'terminating')) {
169+
// Listen to the terminating event to flush the logs before the application ends
170+
// This ensures that all logs are sent to Sentry even if the application ends unexpectedly
171+
// We need to check for method existence here for Lumen since this method was only introduced in Lumen 9.1.4
172+
$this->app->terminating(static function () {
173+
Logs::getInstance()->flush();
174+
});
175+
}
166176
} catch (BindingResolutionException $e) {
167177
// If we cannot resolve the event dispatcher we also cannot listen to events
168178
}

0 commit comments

Comments
 (0)