Set Up Logs for visionOS

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

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 Apple platforms are supported in Sentry Cocoa SDK version 8.55.0 and above. Starting with version 9.0.0, structured logging is no longer experimental and is part of the stable API.

Manual logging is available on all supported Apple platforms, including watchOS. Starting with SDK 9.27.0, calls to SentrySDK.logger don't require an enable flag. Earlier versions require the option described in Setup.

Initialize the SDK, then use SentrySDK.logger to send logs. In SDK 9.27.0 and later, manual logging doesn't require an enable flag.

import Sentry

SentrySDK.start { options in
    options.dsn = "___PUBLIC_DSN___"
}

For older SDK versions, add the appropriate option inside the initialization block:

SDK VersionSwiftObjective-C
9.0.0–9.26.xoptions.enableLogs = trueoptions.enableLogs = YES;
8.55.0–8.xoptions.experimental.enableLogs = trueoptions.experimental.enableLogs = YES;

In SDK 9.27.0+, enableLogs is deprecated. Setting it to false doesn't drop logs sent through SentrySDK.logger or integrations that forward to it. Use beforeSendLog to filter logs.

Once the feature is enabled on the SDK and the SDK is initialized, you can send logs using the SentrySDK.logger APIs.

The SentrySDK.logger namespace exposes six methods that you can use to log messages at different log levels: trace, debug, info, warn, error, and fatal. Supported types for attributes are Strings, Int, Double, and Bool.

import Sentry

let logger = SentrySDK.logger

// Log messages without attributes
logger.trace("Starting database connection")
logger.debug("Cache miss for user")
logger.info("Updated profile")

// Log messages with attributes
logger.trace("Starting database connection", attributes: ["database": "users"])
logger.debug("Cache miss for user", attributes: ["userId": 123])
logger.info("Updated profile", attributes: ["profileId": 345])
logger.warn("Rate limit reached for endpoint", attributes: [
    "endpoint": "/api/results/",
    "isEnterprise": false
])
logger.error("Failed to process payment", attributes: [
    "orderId": "order_123",
    "amount": 99.99
])
logger.fatal("Database connection pool exhausted", attributes: [
    "database": "users",
    "activeConnections": 100
])

Swift supports automatic extraction of interpolated values as attributes. When you use string interpolation in your log messages, supported types (Strings, Int, Double, and Bool) are automatically extracted and added as attributes with the key format sentry.message.parameter.{index}.

let userId = "user_123"
let orderCount = 5
let isPremium = true
let totalAmount = 99.99

// String interpolation automatically extracts values as attributes
logger.info("User \(userId) placed \(orderCount) orders, premium: \(isPremium), total: $\(totalAmount)")

// This is equivalent to manually specifying attributes:
let message = "User \(userId) placed \(orderCount) orders, premium: \(isPremium), total: $\(totalAmount)"
logger.info(
  message,
  attributes: [
      "sentry.message.template": "User {0} placed {1} orders, premium: {2}, total: {3}",
      "sentry.message.parameter.0": userId,
      "sentry.message.parameter.1": orderCount,
      "sentry.message.parameter.2": isPremium,
      "sentry.message.parameter.3": totalAmount
  ]
)

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

In SDK 9.27.0+, manual logging doesn't require an enable flag. To drop logs, return nil from beforeSendLog; setting enableLogs to false doesn't drop them. For older SDKs, also enable logging as described in Setup.

import Sentry

SentrySDK.start { options in
    options.dsn = "___PUBLIC_DSN___"

    options.beforeSendLog = { log in
        if log.level == .info {
            // Filter out all info logs
            return nil
        }
        return log
    }
}

The beforeSendLog function receives a log object, and should return the log object if you want it to be sent to Sentry, or nil if you want to discard it.

The log object has the following properties:

  • level: The log level (trace, debug, info, warn, error, fatal)
  • body: The log message content
  • timestamp: The timestamp of the log
  • attributes: The attributes of the log

Instead of many thin logs that are hard to correlate, emit one comprehensive log per operation with all relevant context.

This makes debugging dramatically faster — one query returns everything about a specific order, user, or request.

// ❌ Scattered thin logs
SentrySDK.logger.info(message: "Starting checkout")
SentrySDK.logger.info(message: "Validating cart")
SentrySDK.logger.info(message: "Processing payment")
SentrySDK.logger.info(message: "Checkout complete")

// ✅ One wide event with full context
SentrySDK.logger.info(message: "Checkout completed", attributes: [
    "order_id": order.id,
    "user_id": user.id,
    "user_tier": user.subscription,
    "cart_value": cart.total,
    "item_count": cart.items.count,
    "payment_method": "stripe",
    "duration_ms": Int(Date().timeIntervalSince(startTime) * 1000)
])

Add attributes that help you prioritize and debug:

  • User context — tier, account age, lifetime value
  • Transaction data — order value, item count
  • Feature state — active feature flags
  • Request metadata — endpoint, method, duration

This lets you filter logs by high-value customers or specific features.

SentrySDK.logger.info(message: "API request completed", attributes: [
    // User context
    "user_id": user.id,
    "user_tier": user.plan, // "free" | "pro" | "enterprise"
    "account_age_days": user.ageDays,

    // Request data
    "endpoint": "/api/orders",
    "method": "POST",
    "duration_ms": 234,

    // Business context
    "order_value": 149.99,
    "feature_flags": ["new-checkout", "discount-v2"]
])

Pick a naming convention and stick with it across your codebase. Inconsistent names make queries impossible.

Recommended: Use snake_case for custom attributes and dot-notation to namespace related groups (e.g., user.id, order.value). This matches the convention used by Sentry's own default attributes.

// ❌ Inconsistent naming
["user": "123"]
["userId": "123"]
["user_id": "123"]
["UserID": "123"]

// ✅ Consistent dot-namespaced attributes
SentrySDK.logger.info(message: "Order processed", attributes: [
    "user.id": "123",
    "order.id": "456",
    "order.cart_value": 99.99,
    "order.item_count": 3
])

The Apple SDKs 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.
  • 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.

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.

  • user.id: The user ID. Maps to id in the User payload, which is set by default by the SDKs.

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

  • user.name: The username. Maps to username in the User payload.
  • user.email: The email address. Maps to email in the User payload.

If the log was parameterized (like with Sentry.logger().error("A %s log message", "formatted");), Sentry adds the message template and parameters as log 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.

Logs can get lost in certain crash scenarios, if the SDK can not send the logs before the app terminates. We are currently working on improving this to ensure that all logs are sent, at the latest on the next app restart.

Available integrations:

If there's an integration you would like to see, open a new issue on GitHub.

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