|
1 | 1 | --- |
2 | 2 | title: Monolog |
3 | | -description: "Learn how to enable Sentry's PHP SDK to capture Monolog events." |
| 3 | +description: "Learn how to enable Sentry's PHP SDK to capture Monolog events and logs." |
4 | 4 | --- |
5 | 5 |
|
6 | | -When using [Monolog](https://github.com/Seldaek/monolog) you can configure a [breadcrumb](../../enriching-events/breadcrumbs/) handler to capture Monolog messages as breadcrumbs and a handler that captures messages as events in Sentry. |
| 6 | +When using [Monolog](https://github.com/Seldaek/monolog) you can configure handlers to capture Monolog messages in several ways: |
| 7 | + |
| 8 | +- **Logs Handler** - Send structured logs to Sentry for search and analysis |
| 9 | +- **Event Handler** - Capture messages as error events in Sentry |
| 10 | +- **Breadcrumb Handler** - Record messages as breadcrumbs attached to future events |
| 11 | + |
7 | 12 | 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. |
8 | 13 |
|
| 14 | +## Logs |
| 15 | + |
| 16 | +<Alert level="info"> |
| 17 | +Available in SDK version 4.12.0 and above. |
| 18 | +</Alert> |
| 19 | + |
| 20 | +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. |
| 21 | + |
| 22 | +```php |
| 23 | +<?php |
| 24 | + |
| 25 | +use Monolog\Level; |
| 26 | +use Monolog\Logger; |
| 27 | + |
| 28 | +// Setup the Sentry SDK with logs enabled |
| 29 | +\Sentry\init([ |
| 30 | + 'dsn' => '___PUBLIC_DSN___', |
| 31 | + 'enable_logs' => true, // Required for logs to be sent to Sentry |
| 32 | +]); |
| 33 | + |
| 34 | +// Create a Monolog channel with a logs handler |
| 35 | +$log = new Logger('app'); |
| 36 | +$log->pushHandler(new \Sentry\Monolog\LogsHandler( |
| 37 | + hub: \Sentry\SentrySdk::getCurrentHub(), |
| 38 | + level: Level::Info, // Minimum level to send to Sentry logs |
| 39 | +)); |
| 40 | + |
| 41 | +// Send logs to Sentry |
| 42 | +$log->info('User logged in', [ |
| 43 | + 'user_id' => 12345, |
| 44 | + 'email' => 'user@example.com', |
| 45 | + 'login_method' => 'password', |
| 46 | +]); |
| 47 | + |
| 48 | +$log->warning('API rate limit approaching', [ |
| 49 | + 'endpoint' => '/api/users', |
| 50 | + 'requests_remaining' => 10, |
| 51 | + 'window_seconds' => 60, |
| 52 | +]); |
| 53 | + |
| 54 | +// Flush to ensure logs are sent |
| 55 | +\Sentry\logger()->flush(); |
| 56 | +``` |
| 57 | + |
| 58 | +The context array passed to Monolog methods becomes searchable attributes in the Sentry logs interface. |
| 59 | + |
| 60 | +## Events |
| 61 | + |
9 | 62 | ```php |
10 | 63 | <?php |
11 | 64 |
|
|
0 commit comments