Skip to content

Commit 42d53ee

Browse files
stayallivecleptric
andauthored
Document the Logs integration for PHP (#13916)
Co-authored-by: Michael Hoffmann <michael.hoffmann@sentry.io>
1 parent 1c1358a commit 42d53ee

File tree

7 files changed

+107
-5
lines changed

7 files changed

+107
-5
lines changed

docs/platforms/php/common/configuration/options.mdx

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -211,6 +211,20 @@ This function is called with an SDK-specific check-in event object, and can retu
211211

212212
</SdkOption>
213213

214+
## Logs Options
215+
216+
<SdkOption name="enable_logs" type='bool' defaultValue='false'>
217+
218+
This option enables the logging integration, which allows the SDK to capture logs and send them to Sentry. This is disabled by default.
219+
220+
</SdkOption>
221+
222+
<SdkOption name="before_send_log" type='function (\Sentry\Logs\Log $log): ?\Sentry\Logs\Log'>
223+
224+
This function is called with an SDK-specific log object, and can return a modified log event object, or `null` to skip reporting the event.
225+
226+
</SdkOption>
227+
214228
## Transport Options
215229

216230
Transports are used to send events to Sentry. Transports can be customized to some degree to better support highly specific deployments.
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
---
2+
title: Set Up Logs
3+
sidebar_title: Logs
4+
description: "Structured logs allow you to send, view and query logs sent from your applications within Sentry."
5+
sidebar_order: 5600
6+
---
7+
8+
<Include name="feature-stage-beta-logs.mdx" />
9+
10+
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.
11+
12+
<Alert title="Looking for Laravel or Symfony?">
13+
14+
Let us know what you would like to see on GitHub: [Laravel Logs](https://github.com/getsentry/sentry-php/issues/999) or [Symfony Logs](https://github.com/getsentry/sentry-symfony/issues/925).
15+
16+
</Alert>
17+
18+
## Requirements
19+
20+
<PlatformContent includePath="logs/requirements" />
21+
22+
## Setup
23+
24+
<PlatformContent includePath="logs/setup" />
25+
26+
## Usage
27+
28+
<PlatformContent includePath="logs/usage" />
29+
30+
## Options
31+
32+
<PlatformContent includePath="logs/options" />

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

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,14 @@ To set up Sentry Logs, use the links below for supported SDKs. After it's been s
199199
url="/platforms/dart/guides/flutter/logs/"
200200
/>
201201

202+
### PHP
203+
204+
- <LinkWithPlatformIcon
205+
platform="php"
206+
label="PHP"
207+
url="/platforms/dart/guides/php/logs/"
208+
/>
209+
202210
### Python
203211

204212
- <LinkWithPlatformIcon
@@ -223,11 +231,6 @@ To set up Sentry Logs, use the links below for supported SDKs. After it's been s
223231

224232
We're actively working on adding Log functionality to additional SDKs. Check out these GitHub issues for the latest updates:
225233

226-
- <LinkWithPlatformIcon
227-
platform="php"
228-
label="PHP"
229-
url="https://github.com/getsentry/sentry-php/issues/1824"
230-
/>
231234
- <LinkWithPlatformIcon
232235
platform="cocoa"
233236
label="Cocoa (iOS)"
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
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+
```php
6+
\Sentry\init([
7+
'dsn' => '___PUBLIC_DSN___',
8+
// Enable logs to be sent to Sentry
9+
'enable_logs' => true,
10+
'before_send_log' => function (\Sentry\Logs\Log $log): ?\Sentry\Logs\Log {
11+
if ($log->getLevel() === \Sentry\Logs\LogLevel::info()) {
12+
// Filter out all info logs
13+
return null;
14+
}
15+
16+
return $log;
17+
},
18+
]);
19+
```
20+
21+
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 PHP are supported in Sentry PHP SDK version `4.12.0` and above.

platform-includes/logs/setup/php.mdx

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
To enable logging, you need to initialize the SDK with the `enable_logs` option set to `true`.
2+
3+
```php
4+
\Sentry\init([
5+
'dsn' => '___PUBLIC_DSN___',
6+
// Enable logs to be sent to Sentry
7+
'enable_logs' => true,
8+
]);
9+
10+
// Somewhere at the end of you execution, you should flush the logger to send pending logs to Sentry.
11+
\Sentry\logger()->flush();
12+
```

platform-includes/logs/usage/php.mdx

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
Once the feature is enabled on the SDK and the SDK is initialized, you can send logs using the `logger()` function.
2+
3+
The `logger()` function exposes six methods that you can use to log messages at different log levels: `trace`, `debug`, `info`, `warn`, `error`, and `fatal`.
4+
5+
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.
6+
7+
```php
8+
\Sentry\logger()->info('A simple log message');
9+
\Sentry\logger()->info('A message with a parameter that says %s', values: ['hello']);
10+
\Sentry\logger()->warn('This is a warning log with attributes.', attributes: [
11+
'attribute1' => 'string',
12+
'attribute2' => 1,
13+
'attribute3' => 1.0,
14+
'attribute4' => true,
15+
]);
16+
17+
// Somewhere at the end of you execution, you should flush the logger to send pending logs to Sentry.
18+
\Sentry\logger()->flush();
19+
```

0 commit comments

Comments
 (0)