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

# Set Up Logs | Sentry for macOS

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.

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

Logs for Apple platforms are supported in Sentry Cocoa SDK version `8.55.0` and above. Logs are still experimental and the API may change in the future.

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

To enable logging, set the `enableLogs` option to `true`.

```swift
import Sentry

SentrySDK.start { options in
    options.dsn = "___PUBLIC_DSN___"
    options.enableLogs = true
}
```

Before 9.0.0, this option was experimental:

```swift
import Sentry

SentrySDK.start { options in
    options.dsn = "___PUBLIC_DSN___"
    // Enable logs to be sent to Sentry
    options.experimental.enableLogs = true
}
```

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

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

The `Sentry.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.

```swift
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
])
```

### [String Interpolation (Swift Only)](https://docs.sentry.io/platforms/apple/guides/macos/logs.md#string-interpolation-swift-only)

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}`.

```swift
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
  ]
)
```

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

#### [beforeSendLog](https://docs.sentry.io/platforms/apple/guides/macos/logs.md#beforesendlog)

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

Starting with version 9.0.0, structured logging is no longer experimental. The option moved from `options.experimental.enableLogs` to `options.enableLogs`.

```swift
import Sentry

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

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

Before 9.0.0:

```swift
import Sentry

SentrySDK.start { options in
    options.dsn = "___PUBLIC_DSN___"
    options.experimental.enableLogs = true

    options.beforeSendLog = { log in
        if log.level == .info {
            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)
* `message`: The message to be logged
* `timestamp`: The timestamp of the log
* `attributes`: The attributes of the log

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

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

### [Core Attributes](https://docs.sentry.io/platforms/apple/guides/macos/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/apple/guides/macos/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`.

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

* `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.

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

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.

### [Integration Attributes](https://docs.sentry.io/platforms/apple/guides/macos/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`.

## [Troubleshooting](https://docs.sentry.io/platforms/apple/guides/macos/logs.md#troubleshooting)

### [Missing Logs for Crashes](https://docs.sentry.io/platforms/apple/guides/macos/logs.md#missing-logs-for-crashes)

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](https://github.com/getsentry/sentry-cocoa/issues/5660) this to ensure that all logs are sent, at the latest on the next app restart.
