Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Document how to use the PHP Monolog integration #11498

Merged
merged 2 commits into from
Oct 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 43 additions & 0 deletions docs/platforms/php/common/integrations/monolog.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
---
title: Monolog
description: "Learn how to enable Sentry's PHP SDK to capture Monolog events."
---

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.
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.

```php
<?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' => '___PUBLIC_DSN___'
]);

// 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]);
}
```
29 changes: 29 additions & 0 deletions docs/platforms/php/guides/symfony/integrations/monolog.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
---
title: Monolog
description: "Learn how to enable Sentry's Symfony SDK to capture Monolog events."
---

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.
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.

```yaml
services:
# Configure the breadcrumb handler as a service
Sentry\Monolog\BreadcrumbHandler:
arguments:
- '@Sentry\State\HubInterface'
- !php/const Monolog\Logger::INFO # Configures the level of messages to capture as breadcrumbs

monolog:
handlers:
# Register the breadcrumb handler as a Monolog handler
sentry_breadcrumbs:
type: service
name: sentry_breadcrumbs
id: Sentry\Monolog\BreadcrumbHandler
# Register the handler as a Monolog handler to capture messages as events
sentry:
type: sentry
level: !php/const Monolog\Logger::ERROR # Configures the level of messages to capture as events
hub_id: Sentry\State\HubInterface
```
Loading