Skip to main content

Sentry

The LoglySentry product connects Logly to Sentry. It starts the Sentry SDK including crash detection and registers a sink that forwards log messages to Sentry. The Logly core keeps no Sentry dependency.

Adding the LoglySentry package​

LoglySentry sits behind the Sentry package trait, which is off by default. Enable the trait where you declare the dependency and add both products, as this excerpt from Package.swift shows:

Package.swift
dependencies: [
.package(url: "https://github.com/Flexible-Universe/Logly", from: "1.1.0",
traits: ["Sentry"]),
],
targets: [
.target(
name: "MyApp",
dependencies: [
.product(name: "Logly", package: "Logly"),
.product(name: "LoglySentry", package: "Logly"),
]
),
]

Without the trait the LoglySentry module is empty. LoglySentry.bootstrap and SentrySink do not exist then and the compiler reports an error. With the trait, Swift Package Manager fetches sentry-cocoa from version 9.29.0.

Activating LoglySentry​

To activate LoglySentry, set the Sentry DSN and call bootstrap once at startup:

import SwiftUI
import Logly
import LoglySentry

@main
struct MyApp: App {
init() {
LoggerConfiguration.shared.sentryURL = "https://<key>@<org>.ingest.sentry.io/<project>"
LoglySentry.bootstrap()
}

var body: some Scene {
WindowGroup { ContentView() }
}
}

The LoglySentry bootstrap does the following:

  1. It reads LoggerConfiguration.shared.sentryURL.
  2. If the value is nil or empty, nothing happens.
  3. If Sentry is already running (SentrySDK.isEnabled), nothing happens either. Calling it several times is therefore harmless.
  4. Otherwise it starts the Sentry SDK with these options:
OptionValue
dsnValue of sentryURL
enableCrashHandlertrue
sendDefaultPiifalse
tracesSampleRate0.0 (no performance tracing)
beforeSendYour scrub closure, if provided
  1. Finally it registers a SentrySink in the Logly configuration.

How to switch Sentry on or off per environment in Logly:

#if DEBUG
LoggerConfiguration.shared.sentryURL = nil // Sentry stays off
#else
LoggerConfiguration.shared.sentryURL = Bundle.main.object(forInfoDictionaryKey: "SentryDSN") as? String
#endif
LoglySentry.bootstrap()

Changing sentryURL later has no effect on a Sentry instance that is already running.

What becomes what?​

The following table shows what LoglySentry sends to Sentry for each Logly level, with and without error::

Logly levelWith error:Without error:
.faultException eventMessage event with level fatal
.errorException eventMessage event with level error
.warning–Breadcrumb with level warning
.info, .debug–Breadcrumb with level info

Logly attaches the category to every event and breadcrumb: as the category tag on events, and as the breadcrumb's category on breadcrumbs.

Keep in mind with LoglySentry:

  • For an exception event Logly only sends the Error value and the tag. The message text is not sent to Sentry.
  • Breadcrumbs are only created for messages that pass the level filter. With currentLogLevel = .warning there are no breadcrumbs for info and debug.

Passing errors to LoglySentry​

In Logly, pass the real Error so Sentry can group similar errors:

do {
try await syncService.upload(batch)
} catch {
await Logger.custom(category: "Sync").error("Upload failed", error: error)
}

Privacy in LoglySentry: scrub​

In LoglySentry, use scrub to remove personal data before an event is sent. The closure is set as beforeSend. If you return nil, the event is dropped. The following example removes the user and the server name:

import Sentry
import LoglySentry

enum Redaction {
static let scrub: @Sendable (Event) -> Event? = { event in
event.user = nil
event.serverName = nil
return event
}
}

LoglySentry.bootstrap(scrub: Redaction.scrub)

Event is the event type of the Sentry SDK, so you need import Sentry here.

Crash handling with LoglySentry​

When the LoglySentry bootstrap starts Sentry, Sentry handles crash detection. Do not also call LoggerCrashHandler.install() in that case, otherwise two handlers compete for the same signals. This is how you install the LoggerCrashHandler only without Sentry:

LoggerConfiguration.shared.sentryURL = dsn
LoglySentry.bootstrap()

if LoggerConfiguration.shared.sentryURL?.isEmpty ?? true {
LoggerCrashHandler.install() // only without Sentry
}

Starting Sentry yourself and connecting it to Logly​

If you start the Sentry SDK in your own code before bootstrap runs, bootstrap registers no sink. In that case, add the SentrySink to the Logly configuration yourself:

import Sentry
import Logly
import LoglySentry

SentrySDK.start { options in
options.dsn = dsn
// more options of your own
}
LoggerConfiguration.shared.addSink(SentrySink())

Test helpers in LoglySentry​

Two public LoglySentry functions work without the Sentry SDK, as the following example shows:

// Which Sentry action would an event produce?
let event = LogEvent(level: .warning, category: "Network", message: "slow",
error: nil, file: #file, line: #line, timestamp: Date())
SentrySink.plan(for: event)
// .breadcrumb(level: .warning, category: "Network", message: "slow")

// Would bootstrap activate?
LoglySentry.shouldActivate(url: "", alreadyEnabled: false) // false
LoglySentry.shouldActivate(url: "https://…", alreadyEnabled: true) // false