SwiftyBeaver for Apple

Integrate SwiftyBeaver with Sentry to capture and send structured logs.

The SentrySwiftyBeaver package provides a SwiftyBeaver destination that forwards log entries to Sentry's structured logging system. Once configured, any logs emitted through SwiftyBeaver 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
  • SwiftyBeaver 2.0.0 or above

Add the package via Swift Package Manager:

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

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

By default, the integration uses precompiled Sentry binary xcframeworks. If you need to build Sentry from source, enable the SentryFromSource trait:

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

Initialize the Sentry SDK, then add SentryDestination to SwiftyBeaver. SDK 9.27.0+ doesn't require an enable flag for logs forwarded by this integration:

Copied
import Sentry
import SwiftyBeaver
import SentrySwiftyBeaver

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

let log = SwiftyBeaver.self
let sentryDestination = SentryDestination()
log.addDestination(sentryDestination)

Use SwiftyBeaver's minLevel property on the destination to control which messages are sent to Sentry:

Copied
let sentryDestination = SentryDestination()
sentryDestination.minLevel = .error
log.addDestination(sentryDestination)

Copied
log.info("User logged in")
log.error("Payment failed")
log.warning("API rate limit approaching")

The destination supports SwiftyBeaver's context parameter for additional metadata.

When context is a [String: Any] dictionary, each key-value pair is added as an individual Sentry log attribute prefixed with swiftybeaver.context.:

Copied
log.info("User action", context: [
    "userId": "12345",
    "action": "purchase",
    "amount": 99.99
])
// Produces attributes: swiftybeaver.context.userId, swiftybeaver.context.action, etc.

For non-dictionary contexts, the value is converted to a single string attribute swiftybeaver.context:

Copied
log.info("Test message", context: "simple string context")

SwiftyBeaver levels are automatically mapped to Sentry log levels:

SwiftyBeaver LevelSentry Log Level
.verbose.trace
.debug.debug
.info.info
.warning.warn
.error.error
.critical.fatal
.fault.fatal

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

AttributeDescription
sentry.origin"auto.logging.swiftybeaver"
swiftybeaver.levelThe original SwiftyBeaver level (as raw value)
swiftybeaver.threadThe thread identifier
code.file.pathThe source file path
code.function.nameThe function name
code.line.numberThe line number
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").