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 PHP are supported in Sentry PHP SDK version 4.12.0 and above.

To enable logging, you need to initialize the SDK with the enable_logs option set to true.

Copied
\Sentry\init([
    'dsn' => 'https://examplePublicKey@o0.ingest.sentry.io/0',
    // Enable logs to be sent to Sentry
    'enable_logs' => true,
]);

// Somewhere at the end of your execution, you should flush the logger to send pending logs to Sentry.
\Sentry\logger()->flush();

Once the feature is enabled on the SDK and the SDK is initialized, you can send logs using the logger() function.

The logger() function exposes six methods that you can use to log messages at different log levels: trace, debug, info, warn, error, and fatal.

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
\Sentry\logger()->info('A simple log message');
\Sentry\logger()->info('A message with a parameter that says %s', values: ['hello']);
\Sentry\logger()->warn('This is a warning log with attributes.', attributes: [
  'attribute1' => 'string',
  'attribute2' => 1,
  'attribute3' => 1.0,
  'attribute4' => true,
]);

// Somewhere at the end of your execution, you should flush the logger to send pending logs to Sentry.
\Sentry\logger()->flush();

If you are using logs in long running CLI tasks, we recommend periodically calling \Sentry\logger()->flush() to keep memory pressure low and avoid any logs being discarded to due size-limits.

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

Copied
\Sentry\init([
    'dsn' => 'https://examplePublicKey@o0.ingest.sentry.io/0',
    // Enable logs to be sent to Sentry
    'enable_logs' => true,
    'before_send_log' => 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 PHP 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").