Monolog
Learn how to enable Sentry's PHP SDK to capture Monolog events and logs.
When using Monolog you can configure handlers to capture Monolog messages in several ways:
- Logs Handler - Send structured logs to Sentry for search and analysis
- Event Handler - Capture messages as error events in Sentry
- Breadcrumb Handler - Record messages as breadcrumbs attached to future events
The breadcrumb handler will not send anything to Sentry directly, it only records breadcrumbs that will be attached to any event or exception sent to Sentry.
Available in SDK version 4.12.0 and above.
To send structured logs to Sentry, use the \Sentry\Monolog\LogsHandler
. This handler sends log entries to Sentry's structured logs feature, where they can be searched, filtered, and analyzed.
Copied
<?php
use Monolog\Level;
use Monolog\Logger;
// Setup the Sentry SDK with logs enabled
\Sentry\init([
'dsn' => 'https://examplePublicKey@o0.ingest.sentry.io/0',
'enable_logs' => true, // Required for logs to be sent to Sentry
]);
// Create a Monolog channel with a logs handler
$log = new Logger('app');
$log->pushHandler(new \Sentry\Monolog\LogsHandler(
hub: \Sentry\SentrySdk::getCurrentHub(),
level: Level::Info, // Minimum level to send to Sentry logs
));
// Send logs to Sentry
$log->info('User logged in', [
'user_id' => 12345,
'email' => 'user@example.com',
'login_method' => 'password',
]);
$log->warning('API rate limit approaching', [
'endpoint' => '/api/users',
'requests_remaining' => 10,
'window_seconds' => 60,
]);
// Flush to ensure logs are sent
\Sentry\logger()->flush();
The context array passed to Monolog methods becomes searchable attributes in the Sentry logs interface.
Copied
<?php
use Monolog\Level;
use Monolog\Logger;
use Monolog\Handler\StreamHandler;
// Setup the Sentry SDK, this can also be done elsewhere in your application
\Sentry\init([
'dsn' => 'https://examplePublicKey@o0.ingest.sentry.io/0'
]);
// Create a Monolog channel with a breadcrumb handler and a Sentry handler
$log = new Logger('sentry');
$log->pushHandler(new \Sentry\Monolog\BreadcrumbHandler(
hub: \Sentry\SentrySdk::getCurrentHub(),
level: Level::Info, // Take note of the level here, messages with that level or higher will be attached to future Sentry events as breadcrumbs
));
$log->pushHandler(new \Sentry\Monolog\Handler(
hub: \Sentry\SentrySdk::getCurrentHub(),
level: Level::Error, // Take note of the level here, messages with that level or higher will be sent to Sentry
bubble: true,
fillExtraContext: false, // Will add a `monolog.context` & `monolog.extra`, key to the event with the Monolog `context` & `extra` values
));
// Log an error:
$log->error('Something bad happened');
// To log an exception you can use the following code:
try {
throw new RuntimeException('Some exception');
} catch (RuntimeException $exception) {
$log->error('Some exception happened', ['exception' => $exception]);
}
Was this helpful?
Help improve this content
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").
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").