Skip to content

Commit 16c4d50

Browse files
committed
wip
1 parent c130359 commit 16c4d50

File tree

5 files changed

+97
-2
lines changed

5 files changed

+97
-2
lines changed

docs/platforms/php/common/integrations/monolog.mdx

Lines changed: 55 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,64 @@
11
---
22
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."
44
---
55

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+
712
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.
813

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+
962
```php
1063
<?php
1164

docs/platforms/php/common/logs/index.mdx

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,10 @@ Let us know what you would like to see on GitHub: [Symfony Logs](https://github.
2525

2626
<PlatformContent includePath="logs/usage" />
2727

28+
## Integrations
29+
30+
<PlatformContent includePath="logs/integrations" />
31+
2832
## Options
2933

3034
<PlatformContent includePath="logs/options" />
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
Available integrations:
2+
- [Monolog](/platforms/php/integrations/monolog/#logs)
3+
4+
If there's an integration you would like to see, open a [new issue on GitHub](https://github.com/getsentry/sentry-php/issues/new/choose).
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,7 @@
11
Logs for PHP are supported in Sentry PHP SDK version `4.12.0` and above.
2+
3+
## Monolog Integration
4+
5+
To use logs with Monolog, you'll need:
6+
- Sentry PHP SDK version `4.12.0` and above
7+
- Monolog version `^2.0` or `^3.0`

platform-includes/logs/setup/php.mdx

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,3 +10,31 @@ To enable logging, you need to initialize the SDK with the `enable_logs` option
1010
// Somewhere at the end of your execution, you should flush the logger to send pending logs to Sentry.
1111
\Sentry\logger()->flush();
1212
```
13+
14+
## Using with Monolog
15+
16+
If you're already using [Monolog](https://github.com/Seldaek/monolog), you can use the `\Sentry\Monolog\LogsHandler` to send logs directly through your existing logging setup:
17+
18+
```php
19+
use Monolog\Level;
20+
use Monolog\Logger;
21+
22+
\Sentry\init([
23+
'dsn' => '___PUBLIC_DSN___',
24+
'enable_logs' => true,
25+
]);
26+
27+
$log = new Logger('app');
28+
$log->pushHandler(new \Sentry\Monolog\LogsHandler(
29+
hub: \Sentry\SentrySdk::getCurrentHub(),
30+
level: Level::Info,
31+
));
32+
33+
// Your existing Monolog usage will now send logs to Sentry
34+
$log->info('Application started');
35+
36+
// Don't forget to flush
37+
\Sentry\logger()->flush();
38+
```
39+
40+
For more details, see the [Monolog integration documentation](/platforms/php/integrations/monolog/#logs).

0 commit comments

Comments
 (0)