Skip to content

Commit 991d274

Browse files
authored
feat(logs): add documentation for Symfony logging (#14780)
1 parent fbeba60 commit 991d274

File tree

8 files changed

+107
-11
lines changed

8 files changed

+107
-11
lines changed

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

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,6 @@ sidebar_order: 5600
77

88
With Sentry Structured Logs, you can send text-based log information from your applications to Sentry. Once in Sentry, these logs can be viewed alongside relevant errors, searched by text-string, or searched using their individual attributes.
99

10-
<Alert title="Looking for Symfony?">
11-
12-
Let us know what you would like to see on GitHub: [Symfony Logs](https://github.com/getsentry/sentry-symfony/issues/925).
13-
14-
</Alert>
15-
1610
## Requirements
1711

1812
<PlatformContent includePath="logs/requirements" />

docs/platforms/php/guides/symfony/configuration/symfony-options.mdx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,8 @@ sentry:
3737
- "Symfony\\Component\\HttpKernel\\Exception\\BadRequestHttpException"
3838
ignore_transactions:
3939
- "GET /health"
40+
enable_logs: true
41+
before_send_log: "sentry.callback.before_send_log"
4042
before_send: "sentry.callback.before_send"
4143
before_send_transaction: "sentry.callback.before_send_transaction"
4244
trace_propagation_targets:

docs/product/explore/logs/getting-started/index.mdx

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -215,6 +215,11 @@ To set up Sentry Logs, use the links below for supported SDKs. After it's been s
215215
### PHP
216216

217217
- <LinkWithPlatformIcon platform="php" label="PHP" url="/platforms/php/logs/" />
218+
- <LinkWithPlatformIcon
219+
platform="php.symfony"
220+
label="Symfony"
221+
url="/platforms/php/guides/symfony/logs/"
222+
/>
218223
- <LinkWithPlatformIcon
219224
platform="php.laravel"
220225
label="Laravel"
@@ -281,11 +286,6 @@ We're actively working on adding Log functionality to additional SDKs. Check out
281286
label="Elixir"
282287
url="https://github.com/getsentry/sentry-elixir/issues/886"
283288
/>
284-
- <LinkWithPlatformIcon
285-
platform="php.symfony"
286-
label="Symfony"
287-
url="https://github.com/getsentry/sentry-symfony/issues/925"
288-
/>
289289
- <LinkWithPlatformIcon
290290
platform="unity"
291291
label="Unity"
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
The Symfony SDK automatically sets several default attributes on all log entries to provide context and improve debugging:
2+
3+
<Include name="logs/default-attributes/core" />
4+
5+
<Include name="logs/default-attributes/message-template" />
6+
7+
<Include name="logs/default-attributes/server" />
8+
9+
<Include name="logs/default-attributes/user" />
10+
11+
<Include name="logs/default-attributes/integration" />
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
#### before_send_log
2+
3+
To filter logs, or update them before they are sent to Sentry, you can use the `before_send_log` option.
4+
5+
```yaml {filename:config/packages/sentry.yaml}
6+
sentry:
7+
options:
8+
before_send_log: "sentry.callback.before_send_log"
9+
10+
services:
11+
sentry.callback.before_send_log:
12+
class: 'App\Service\Sentry'
13+
factory: ['@App\Service\Sentry', 'getBeforeSendLog']
14+
```
15+
16+
The service needed for the `before_send_log` option can be implemented as follows:
17+
18+
```php {filename:src/Service/Sentry.php}
19+
<?php
20+
21+
namespace App\Service;
22+
23+
class Sentry
24+
{
25+
public function getBeforeSendLog(): callable
26+
{
27+
return function (\Sentry\Logs\Log $log): ?\Sentry\Logs\Log {
28+
if ($log->getLevel() === \Sentry\Logs\LogLevel::info()) {
29+
// Filter out all info logs
30+
return null;
31+
}
32+
33+
return $log;
34+
};
35+
}
36+
}
37+
```
38+
39+
<Alert>
40+
41+
Learn more in [Callables in Symfony Options](/platforms/php/guides/symfony/configuration/symfony-options/#callables).
42+
43+
</Alert>
44+
45+
The `before_send_log` function receives a log object, and should return the log object if you want it to be sent to Sentry, or `null` if you want to discard it.
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Logs for Symfony are supported in Sentry Symfony SDK version `5.4.0` and above.
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
To configure Sentry logging, you need to add the Monolog handler to your configuration:
2+
3+
```yaml {filename: config/packages/monolog.yaml}
4+
monolog:
5+
handlers:
6+
sentry_logs:
7+
type: service
8+
id: Sentry\SentryBundle\Monolog\LogsHandler
9+
```
10+
11+
Configure the service and choose a minimum log level:
12+
13+
```yaml {filename: config/packages/sentry.yaml}
14+
services:
15+
Sentry\SentryBundle\Monolog\LogsHandler:
16+
arguments:
17+
- !php/const Monolog\Logger::INFO
18+
```
19+
20+
Enable the logs option:
21+
22+
```yaml {filename: config/packages/sentry.yaml}
23+
sentry:
24+
options:
25+
enable_logs: true
26+
```
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
Once you have configured the Sentry log handler, you can use your regular `LoggerInterface`. It will send logs to Sentry:
2+
3+
```php
4+
$this->logger->info("This is an info message");
5+
$this->logger->warning('User {id} failed to login.', ['id' => $user->id]);
6+
$this->logger->error("This is an error message");
7+
```
8+
9+
You can pass additional attributes directly to the logging functions. These properties will be sent to Sentry, and can be searched from within the Logs UI, and even added to the Logs views as a dedicated column.
10+
11+
```php
12+
$this->logger->error('Something went wrong', [
13+
'user_id' => $userId,
14+
'action' => 'update_profile',
15+
'additional_data' => $data,
16+
]);
17+
```

0 commit comments

Comments
 (0)