Set Up Logs
Structured logs allow you to send, view and query logs sent from your Laravel applications within Sentry.
With Sentry Structured Logs, you can send text based log information from your Laravel applications to Sentry. Once in Sentry, these logs can be viewed alongside relevant errors, searched by text-string, or searched using their individual attributes.
Logs for Laravel are supported in Sentry Laravel SDK version 4.15.0
and above.
To configure Sentry as a log channel, add the following config to the channels
section in config/logging.php
. If this file does not exist, run php artisan config:publish logging
to publish it.
config/logging.php
'channels' => [
// ...
'sentry' => [
'driver' => 'sentry',
],
],
After you configured the Sentry log channel, you can configure your app to both log to a log file and to Sentry by modifying the log stack:
.env
# ...
LOG_STACK=single,sentry
# ...
Optionally, you can set the logging level:
config/logging.php
'channels' => [
// ...
'sentry' => [
'driver' => 'sentry',
// The minimum logging level at which this handler will be triggered
// Available levels: debug, info, notice, warning, error, critical, alert, emergency
'level' => env('LOG_LEVEL', 'error'),
],
// ...
],
Once you have configured Sentry as a log channel, you can use Laravel's built-in logging functionality to send logs to Sentry:
use Illuminate\Support\Facades\Log;
// Log to all channels in the stack (including Sentry)
Log::info('This is an info message');
Log::warning('User {id} failed to login.', ['id' => $user->id]);
Log::error('This is an error message');
// Log directly to the Sentry channel
Log::channel('sentry')->error('This will only go to Sentry');
You can pass additional attributes directly to the logging functions. These properties will be sent to Sentry, and can be searched from within the Logs UI, and even added to the Logs views as a dedicated column.
use Illuminate\Support\Facades\Log;
Log::error('Something went wrong', [
'user_id' => auth()->id(),
'action' => 'update_profile',
'additional_data' => $data,
]);
To filter logs, or update them before they are sent to Sentry, you can use the before_send_log
option.
config/sentry.php
// ...
'before_send_log' => function (\Sentry\Logs\Log $log): ?\Sentry\Logs\Log {
if ($log->getLevel() === \Sentry\Logs\LogLevel::info()) {
// Filter out all info logs
return null;
}
return $log;
},
// ...
]);
Learn more in Closures and Config Caching.
The before_send_log
function receives a log object, and should return the log object if you want it to be sent to Sentry, or null
if you want to discard it.
Our documentation is open source and available on GitHub. Your contributions are welcome, whether fixing a typo (drat!) or suggesting an update ("yeah, this would be better").