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.0or above - SwiftLog
1.5.0or above
Add the package via Swift Package Manager:
dependencies: [
.package(url: "https://github.com/getsentry/sentry-apple-swift-log", from: "1.0.0")
]
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:
dependencies: [
.package(url: "https://github.com/getsentry/sentry-apple-swift-log", from: "1.0.0", traits: ["SentryFromSource"])
]
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:
import Sentry
import Logging
import SentrySwiftLog
SentrySDK.start { options in
options.dsn = "___PUBLIC_DSN___"
}
LoggingSystem.bootstrap { _ in
SentryLogHandler(logLevel: .info)
}
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:
// Only send warnings and above to Sentry
let handler = SentryLogHandler(logLevel: .warning)
// 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:
var handler = SentryLogHandler(logLevel: .info)
handler.metadata["app_version"] = "1.0.0"
handler.metadata["environment"] = "production"
LoggingSystem.bootstrap { _ in handler }
var handler = SentryLogHandler(logLevel: .info)
handler.metadata["app_version"] = "1.0.0"
handler.metadata["environment"] = "production"
LoggingSystem.bootstrap { _ in handler }
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"])
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 Level | Sentry 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:
| Attribute | Description |
|---|---|
sentry.origin | "auto.logging.swift-log" |
swift-log.level | The original SwiftLog level |
swift-log.source | The log source |
code.file.path | The source file path |
code.function.name | The function name |
code.line.number | The line number |
All SwiftLog metadata is prefixed with swift-log. in Sentry attributes (for example, swift-log.userId).
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").