SwiftLog for visionOS

Integrate Apple's SwiftLog with Sentry to capture and send structured logs.

The SentrySwiftLog package provides a SwiftLog LogHandler that forwards log entries to Sentry's structured logging system. Once configured, any logs emitted through SwiftLog are automatically captured and sent to Sentry with full context, including metadata, source location, and log levels.

  • Sentry Cocoa SDK 9.27.0 or above
  • SwiftLog 1.5.0 or above

Add the package via Swift Package Manager:

Copied
dependencies: [
    .package(url: "https://github.com/getsentry/sentry-apple-swift-log", from: "1.0.0")
]

Or add it in Xcode via File > Add Package Dependencies using the URL https://github.com/getsentry/sentry-apple-swift-log.

By default, the integration uses precompiled Sentry binary xcframeworks. If you need to build Sentry from source (for example, for custom architectures), enable the SentryFromSource trait:

Copied
dependencies: [
    .package(url: "https://github.com/getsentry/sentry-apple-swift-log", from: "1.0.0", traits: ["SentryFromSource"])
]

Initialize the Sentry SDK, then register SentryLogHandler as your SwiftLog bootstrap. SDK 9.27.0+ doesn't require an enable flag for logs forwarded by this integration:

Copied
import Sentry
import Logging
import SentrySwiftLog

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

LoggingSystem.bootstrap { _ in
    SentryLogHandler(logLevel: .info)
}

Set the minimum log level for messages to be sent to Sentry. Messages below this threshold are filtered out:

Copied
// Only send warnings and above to Sentry
let handler = SentryLogHandler(logLevel: .warning)

Add metadata that will be included with all log entries. Handler-level metadata is merged with per-message metadata, with per-message values taking precedence:

Copied
var handler = SentryLogHandler(logLevel: .info)
handler.metadata["app_version"] = "1.0.0"
handler.metadata["environment"] = "production"

LoggingSystem.bootstrap { _ in handler }

Copied
import Logging

let logger = Logger(label: "com.example.app")

logger.info("User logged in", metadata: ["userId": "12345"])
logger.error("Payment failed", metadata: ["errorCode": "500"])
logger.warning("API rate limit approaching", metadata: ["remaining": "10"])

SwiftLog levels are automatically mapped to Sentry log levels:

SwiftLog LevelSentry Log Level
.trace.trace
.debug.debug
.info.info
.notice.info
.warning.warn
.error.error
.critical.fatal

The handler automatically includes the following attributes with every log entry:

AttributeDescription
sentry.origin"auto.logging.swift-log"
swift-log.levelThe original SwiftLog level
swift-log.sourceThe log source
code.file.pathThe source file path
code.function.nameThe function name
code.line.numberThe line number

All SwiftLog metadata is prefixed with swift-log. in Sentry attributes (for example, swift-log.userId).

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