Skip to content

Commit 080ab72

Browse files
committed
Add PHP SDK guide on how to enable the Monolog handler
1 parent b3c81f0 commit 080ab72

File tree

1 file changed

+43
-0
lines changed

1 file changed

+43
-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+
```

0 commit comments

Comments
 (0)