---
title: "Logs"
description: "Structured logs allow you to send, view and query logs sent from your Symfony applications within Sentry."
url: https://docs.sentry.io/platforms/php/guides/symfony/logs/
---

# Set Up Logs | Sentry for Symfony

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

## [Requirements](https://docs.sentry.io/platforms/php/guides/symfony/logs.md#requirements)

Logs for Symfony are supported in Sentry Symfony SDK version `5.4.0` and above.

## [Setup](https://docs.sentry.io/platforms/php/guides/symfony/logs.md#setup)

To configure Sentry logging, you need to add the Monolog handler to your configuration:

`config/packages/monolog.yaml`

```yaml
monolog:
  handlers:
    sentry_logs:
      type: service
      id: Sentry\SentryBundle\Monolog\LogsHandler
```

Configure the service and choose a minimum log level:

`config/packages/sentry.yaml`

```yaml
services:
  Sentry\SentryBundle\Monolog\LogsHandler:
    arguments:
      - !php/const Monolog\Logger::INFO
```

Enable the logs option:

`config/packages/sentry.yaml`

```yaml
sentry:
  options:
    enable_logs: true
```

## [Usage](https://docs.sentry.io/platforms/php/guides/symfony/logs.md#usage)

Once you have configured the Sentry log handler, you can use your regular `LoggerInterface`. It will send logs to Sentry:

```php
$this->logger->info("This is an info message");
$this->logger->warning('User {id} failed to login.', ['id' => $user->id]);
$this->logger->error("This is an error message");
```

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.

```php
$this->logger->error('Something went wrong', [
    'user_id' => $userId,
    'action' => 'update_profile',
    'additional_data' => $data,
]);
```

## [Automatic Log Flushing](https://docs.sentry.io/platforms/php/guides/symfony/logs.md#automatic-log-flushing)

Symfony already flushes the Sentry log handler when a request or command finishes. If a single request or command can produce a high volume of logs, set `log_flush_threshold` to send partial log batches before it completes.

`config/packages/sentry.yaml`

```yaml
sentry:
  options:
    enable_logs: true
    log_flush_threshold: 5
```

We don't recommend flushing too often, because each flush sends a network request and can add latency. If you expect frequent flushes for a request or command, a local [Relay](https://docs.sentry.io/product/relay/getting-started.md) can help reduce that added latency.

## [Options](https://docs.sentry.io/platforms/php/guides/symfony/logs.md#options)

#### [before\_send\_log](https://docs.sentry.io/platforms/php/guides/symfony/logs.md#before_send_log)

To filter logs, or update them before they are sent to Sentry, you can use the `before_send_log` option.

`config/packages/sentry.yaml`

```yaml
sentry:
  options:
    before_send_log: "sentry.callback.before_send_log"

services:
  sentry.callback.before_send_log:
    class: 'App\Service\Sentry'
    factory: ['@App\Service\Sentry', "getBeforeSendLog"]
```

The service needed for the `before_send_log` option can be implemented as follows:

`src/Service/Sentry.php`

```php
<?php

namespace App\Service;

class Sentry
{
    public function getBeforeSendLog(): callable
    {
        return function (\Sentry\Logs\Log $log): ?\Sentry\Logs\Log {
            if ($log->getLevel() === \Sentry\Logs\LogLevel::info()) {
                // Filter out all info logs
                return null;
            }

            return $log;
        };
    }
}
```

Learn more in [Callables in Symfony Options](https://docs.sentry.io/platforms/php/guides/symfony/configuration/symfony-options.md#callables).

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.

## [Default Attributes](https://docs.sentry.io/platforms/php/guides/symfony/logs.md#default-attributes)

The Symfony SDK automatically sets several default attributes on all log entries to provide context and improve debugging:

### [Core Attributes](https://docs.sentry.io/platforms/php/guides/symfony/logs.md#core-attributes)

* `environment`: The environment set in the SDK if defined. This is sent from the SDK as `sentry.environment`.
* `release`: The release set in the SDK if defined. This is sent from the SDK as `sentry.release`.
* `sdk.name`: The name of the SDK that sent the log. This is sent from the SDK as `sentry.sdk.name`.
* `sdk.version`: The version of the SDK that sent the log. This is sent from the SDK as `sentry.sdk.version`.

### [Message Template Attributes](https://docs.sentry.io/platforms/php/guides/symfony/logs.md#message-template-attributes)

If the log was parameterized, Sentry adds the message template and parameters as log attributes.

* `message.template`: The parameterized template string. This is sent from the SDK as `sentry.message.template`.
* `message.parameter.X`: The parameters to fill the template string. X can either be the number that represent the parameter's position in the template string (`sentry.message.parameter.0`, `sentry.message.parameter.1`, etc) or the parameter's name (`sentry.message.parameter.item_id`, `sentry.message.parameter.user_id`, etc). This is sent from the SDK as `sentry.message.parameter.X`.

### [Server Attributes](https://docs.sentry.io/platforms/php/guides/symfony/logs.md#server-attributes)

* `server.address`: The address of the server that sent the log. Equivalent to `server_name` that gets attached to Sentry errors.

### [User Attributes](https://docs.sentry.io/platforms/php/guides/symfony/logs.md#user-attributes)

If user information is available in the current scope, the following attributes are added to the log:

* `user.id`: The user ID.
* `user.name`: The username.
* `user.email`: The email address.

### [Integration Attributes](https://docs.sentry.io/platforms/php/guides/symfony/logs.md#integration-attributes)

If a log is generated by an SDK integration, the SDK will set additional attributes to help you identify the source of the log.

* `origin`: The origin of the log. This is sent from the SDK as `sentry.origin`.

## [Other Logging Integrations](https://docs.sentry.io/platforms/php/guides/symfony/logs.md#other-logging-integrations)

Available integrations:

* [Monolog](https://docs.sentry.io/platforms/php/integrations/monolog.md#logs)

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