Set Up Logs

Structured logs allow you to send, view and query logs sent from your applications within Sentry.

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.

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

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

config/packages/monolog.yaml
Copied
monolog:
  handlers:
    sentry_logs:
      type: service
      id: Sentry\SentryBundle\Monolog\LogsHandler

Configure the service and choose a minimum log level:

config/packages/sentry.yaml
Copied
services:
  Sentry\SentryBundle\Monolog\LogsHandler:
    arguments:
      - !php/const Monolog\Logger::INFO

Enable the logs option:

config/packages/sentry.yaml
Copied
sentry:
  options:
    enable_logs: true

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

Copied
$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.

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

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

config/packages/sentry.yaml
Copied
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
Copied
<?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;
        };
    }
}

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.

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

  • 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.
  • trace.parent_span_id: The span ID of the span that was active when the log was collected (only set if there was an active span). This is sent from the SDK as sentry.trace.parent_span_id.
  • sdk.name: The name of the SDK that sent the log. This is sent from the SDK as sentry.sdk.name. 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. This is sent from the SDK as sentry.sdk.version.

If the log was paramaterized, 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.address: The address of the server that sent the log. Equivalent to server_name that gets attached to Sentry errors.

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.

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.
Was this helpful?
Help improve this content
Our documentation is open source and available on GitHub. Your contributions are welcome, whether fixing a typo (drat!) or suggesting an update ("yeah, this would be better").