Skip to content

Commit 94060a3

Browse files
cleptricLitarnus
andauthored
PHP Monolog logs (#14781)
closes [#1893](getsentry/sentry-php#1893) closes PHP-22 --------- Co-authored-by: Martin Linzmayer <[email protected]>
1 parent 9f39058 commit 94060a3

File tree

4 files changed

+92
-2
lines changed

4 files changed

+92
-2
lines changed

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

Lines changed: 56 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,65 @@
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+
<Alert level="info" title="Sentry Logs">
7+
8+
Enable the Sentry Logs feature with `\Sentry\init(['enable_logs' => true])` to unlock Sentry's full logging power. With Sentry Logs, you can search, filter, and analyze logs from across your entire application in one place.
9+
10+
</Alert>
11+
12+
When using [Monolog](https://github.com/Seldaek/monolog) you can configure handlers to capture Monolog messages in several ways:
13+
14+
- **Logs Handler** - Send structured logs to Sentry
15+
- **Event Handler** - Capture messages as error events in Sentry
16+
- **Breadcrumb Handler** - Record messages as breadcrumbs attached to future events
17+
718
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.
819

20+
## Logs
21+
22+
<Alert level="info">
23+
Available in SDK version 4.12.0 and above.
24+
</Alert>
25+
26+
To send structured logs to Sentry, use the `\Sentry\Monolog\LogsHandler`.
27+
28+
```php
29+
<?php
30+
31+
use Monolog\Logger;
32+
use Sentry\Logs\LogLevel;
33+
34+
\Sentry\init([
35+
'dsn' => '___PUBLIC_DSN___',
36+
'enable_logs' => true, // Enable Sentry logging
37+
]);
38+
39+
// Create a Monolog channel with a logs handler
40+
$logger = new Logger('sentry_logs');
41+
$logger->pushHandler(new \Sentry\Monolog\LogsHandler(
42+
LogLevel::info(), // Minimum level to send logs
43+
));
44+
45+
// Send logs to Sentry
46+
$logger->info('User logged in', [
47+
'user_id' => 12345,
48+
'email' => '[email protected]',
49+
'login_method' => 'password',
50+
]);
51+
52+
$logger->warning('API rate limit approaching', [
53+
'endpoint' => '/api/users',
54+
'requests_remaining' => 10,
55+
'window_seconds' => 60,
56+
]);
57+
```
58+
59+
The context array passed to Monolog methods becomes searchable attributes in the Sentry logs interface.
60+
61+
## Events & Breadcrumbs
62+
963
```php
1064
<?php
1165

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,10 @@ With Sentry Structured Logs, you can send text-based log information from your a
1919

2020
<PlatformContent includePath="logs/usage" />
2121

22+
## Integrations
23+
24+
<PlatformContent includePath="logs/integrations" />
25+
2226
## Options
2327

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

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)