Skip to content

Commit d19473c

Browse files
authored
Document how to use the PHP Monolog integration (#11498)
1 parent 583d61d commit d19473c

File tree

2 files changed

+72
-0
lines changed

2 files changed

+72
-0
lines changed
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
---
2+
title: Monolog
3+
description: "Learn how to enable Sentry's PHP SDK to capture Monolog events."
4+
---
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.
7+
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+
9+
```php
10+
<?php
11+
12+
use Monolog\Level;
13+
use Monolog\Logger;
14+
use Monolog\Handler\StreamHandler;
15+
16+
// Setup the Sentry SDK, this can also be done elsewhere in your application
17+
\Sentry\init([
18+
'dsn' => '___PUBLIC_DSN___'
19+
]);
20+
21+
// Create a Monolog channel with a breadcrumb handler and a Sentry handler
22+
$log = new Logger('sentry');
23+
$log->pushHandler(new \Sentry\Monolog\BreadcrumbHandler(
24+
hub: \Sentry\SentrySdk::getCurrentHub(),
25+
level: Level::Info, // Take note of the level here, messages with that level or higher will be attached to future Sentry events as breadcrumbs
26+
));
27+
$log->pushHandler(new \Sentry\Monolog\Handler(
28+
hub: \Sentry\SentrySdk::getCurrentHub(),
29+
level: Level::Error, // Take note of the level here, messages with that level or higher will be sent to Sentry
30+
bubble: true,
31+
fillExtraContext: false, // Will add a `monolog.context` & `monolog.extra`, key to the event with the Monolog `context` & `extra` values
32+
));
33+
34+
// Log an error:
35+
$log->error('Something bad happened');
36+
37+
// To log an exception you can use the following code:
38+
try {
39+
throw new RuntimeException('Some exception');
40+
} catch (RuntimeException $exception) {
41+
$log->error('Some exception happened', ['exception' => $exception]);
42+
}
43+
```
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
---
2+
title: Monolog
3+
description: "Learn how to enable Sentry's Symfony SDK to capture Monolog events."
4+
---
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.
7+
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+
9+
```yaml
10+
services:
11+
# Configure the breadcrumb handler as a service
12+
Sentry\Monolog\BreadcrumbHandler:
13+
arguments:
14+
- '@Sentry\State\HubInterface'
15+
- !php/const Monolog\Logger::INFO # Configures the level of messages to capture as breadcrumbs
16+
17+
monolog:
18+
handlers:
19+
# Register the breadcrumb handler as a Monolog handler
20+
sentry_breadcrumbs:
21+
type: service
22+
name: sentry_breadcrumbs
23+
id: Sentry\Monolog\BreadcrumbHandler
24+
# Register the handler as a Monolog handler to capture messages as events
25+
sentry:
26+
type: sentry
27+
level: !php/const Monolog\Logger::ERROR # Configures the level of messages to capture as events
28+
hub_id: Sentry\State\HubInterface
29+
```

0 commit comments

Comments
 (0)