Skip to content

Conversation

@denrase
Copy link
Collaborator

@denrase denrase commented Sep 25, 2025

📜 Description

The Sentry swift-log integration.

Setup

  • Adds SentrySwiftLog as a target to Sentry target
  • Adds SentrySwiftLog to Package.swift
  • Add a sample call to the iOS15-SwiftUi app so you can play around with it

Implementation

  • Adds SentryLogHandler
  • sentry-log metadata is mapped to attributes and prefixed with swift-log
  • Other parameters of the log function are set as attributes and

Discussion

Here vs Own Repo

Added the integration to this repo, because it is easier for us to maintain.

On the swift-log repo integrations section is linking to the swift package index with search term swift-log. So for visibility, we'd need to have a separate swift-log-sentry repo.

https://github.com/apple/swift-log?tab=readme-ov-file#available-log-handler-backends
https://swiftpackageindex.com/search?query=swift-log

Distribution

Only distributed through SPM, as swift-log dropped support for CocoaPods. Also, we'll not be able to distribute this as a binary, as swift-log is not supporting that.

Module was not compiled with library evolution support; using it means binary compatibility for can't be guaranteed

💡 Motivation and Context

Closes #5372

💚 How did you test it?

Unit Tests
Sample App

📝 Checklist

You have to check all boxes before merging:

  • I added tests to verify the changes.
  • No new PII added or SDK only sends newly added PII if sendDefaultPII is enabled.
  • I updated the docs if needed.
  • I updated the wizard if needed.
  • Review from the native team if needed.
  • No breaking change or entry added to the changelog.
  • No breaking change for hybrid SDKs or communicated to hybrid SDKs.

@codecov
Copy link

codecov bot commented Sep 25, 2025

❌ 4 Tests Failed:

Tests completed Failed Passed Skipped
3920 4 3916 27
View the top 3 failed test(s) by shortest run time
iOS_SwiftUI_UITests.FeedbackUITests::testWidgetDisplayInSwiftUIApp
Stack Traces | 0s run time
.../iOS-SwiftUI/iOS-SwiftUI-UITests/FeedbackUITests.swift:11 - Failed to get background assertion for target app with pid 75661: Timed out while acquiring background assertion.
iOS_SwiftUI_UITests.LaunchUITests::testCaptureErrorReturnsValidId
Stack Traces | 0s run time
.../iOS-SwiftUI/iOS-SwiftUI-UITests/LaunchUITests.swift:91 - Failed to get background assertion for target app with pid 75707: Timed out while acquiring background assertion.
iOS_SwiftUI_UITests.LaunchUITests::testNoNewTransactionForSecondCallToBody
Stack Traces | 0s run time
.../iOS-SwiftUI/iOS-SwiftUI-UITests/LaunchUITests.swift:91 - Failed to get background assertion for target app with pid 75935: Timed out while acquiring background assertion.
iOS_SwiftUI_UITests.LaunchUITests::testTransactionSpan
Stack Traces | 0s run time
.../iOS-SwiftUI/iOS-SwiftUI-UITests/LaunchUITests.swift:91 - Failed to get background assertion for target app with pid 76234: Timed out while acquiring background assertion.

To view more test analytics, go to the Test Analytics Dashboard
📋 Got 3 mins? Take this short survey to help us improve Test Analytics.

@github-actions
Copy link
Contributor

github-actions bot commented Sep 25, 2025

Performance metrics 🚀

  Plain With Sentry Diff
Startup time 1226.17 ms 1264.55 ms 38.38 ms
Size 23.75 KiB 1.01 MiB 1016.14 KiB

Baseline results on branch: main

Startup times

Revision Plain With Sentry Diff
736bcfb 1229.86 ms 1249.59 ms 19.73 ms
37183fe 1212.33 ms 1238.92 ms 26.59 ms
0ac4c65 1221.75 ms 1251.69 ms 29.94 ms
4c719e2 1206.92 ms 1237.45 ms 30.53 ms
fb611a9 1222.63 ms 1254.79 ms 32.16 ms
d8b2303 1231.53 ms 1249.89 ms 18.35 ms
5db87fa 1218.88 ms 1251.53 ms 32.65 ms
005f255 1218.42 ms 1247.09 ms 28.67 ms
d38165b 1211.41 ms 1242.49 ms 31.08 ms
52588a5 1226.24 ms 1257.22 ms 30.98 ms

App size

Revision Plain With Sentry Diff
736bcfb 23.74 KiB 891.02 KiB 867.28 KiB
37183fe 23.75 KiB 913.63 KiB 889.87 KiB
0ac4c65 23.75 KiB 968.16 KiB 944.41 KiB
4c719e2 23.75 KiB 912.77 KiB 889.02 KiB
fb611a9 23.74 KiB 1022.38 KiB 998.64 KiB
d8b2303 23.75 KiB 908.02 KiB 884.27 KiB
5db87fa 23.75 KiB 926.65 KiB 902.90 KiB
005f255 23.75 KiB 1.01 MiB 1016.13 KiB
d38165b 23.75 KiB 855.37 KiB 831.62 KiB
52588a5 23.75 KiB 1.00 MiB 1005.07 KiB

Previous results on branch: denrase/swift-log-sentry-poc

Startup times

Revision Plain With Sentry Diff
0fd08ab 1225.14 ms 1256.49 ms 31.35 ms
b52b178 1227.86 ms 1255.65 ms 27.79 ms
2bd1339 1223.22 ms 1248.85 ms 25.64 ms
53379e0 1231.19 ms 1269.38 ms 38.20 ms

App size

Revision Plain With Sentry Diff
0fd08ab 23.75 KiB 995.43 KiB 971.68 KiB
b52b178 23.75 KiB 989.97 KiB 966.22 KiB
2bd1339 23.75 KiB 1.01 MiB 1006.48 KiB
53379e0 23.75 KiB 1.00 MiB 1002.40 KiB

@denrase denrase changed the title Structured Logs: swift-log Integration Structured Logs: Add SentrySwiftLog Integration Sep 30, 2025
@denrase denrase marked this pull request as ready for review October 1, 2025 13:25
@denrase denrase requested a review from itaybre as a code owner October 1, 2025 13:26
cursor[bot]

This comment was marked as outdated.

cursor[bot]

This comment was marked as outdated.

Copy link
Contributor

@itaybre itaybre left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good, just a small comment

Comment on lines +53 to +56
# Clean up orphaned commas and fix syntax
sed -i '' '/^[[:space:]]*,$/d' $PACKAGE_FILE
sed -i '' 's/name: "Sentry\(-.*\)\?"$/name: "Sentry\1",/g' $PACKAGE_FILE
sed -i '' 's/platforms: \[\.iOS(\.v11), \.macOS(\.v10_13), \.tvOS(\.v11), \.watchOS(\.v4)\]$/platforms: [.iOS(.v11), .macOS(.v10_13), .tvOS(.v11), .watchOS(.v4)],/g' $PACKAGE_FILE
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What is this trying to do?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

One of the CI jobs is replacing dependencies with local build artefacts. This broke, as swift-log was added to our Package.swift. This code fixes those issues.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does this only happen because of swift-log or should we do the fix in a separate PR?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Only because of swift-log dependency added in our Package.swift

@denrase denrase requested a review from itaybre October 13, 2025 14:31
.library(name: "SentrySwiftUI", targets: ["Sentry", "SentrySwiftUI", "SentryCppHelper"]),
.library(name: "SentryDistribution", targets: ["SentryDistribution"])
.library(name: "SentryDistribution", targets: ["SentryDistribution"]),
.library(name: "SentrySwiftLog", targets: ["Sentry", "SentrySwiftLog"])
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

m: I don't fully understand why this is another target. Is this only here because swift-log is a new dependency? Shouldn't we add it as a dependency to Sentry instead?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The intent was to only pull in the dependency for users that actually are using swift-log, but then again, I am not sure how this would behave if a user is not using SentrySwiftLog target. Will check.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, there is a proposal that was partially adopted:

https://github.com/swiftlang/swift-evolution/blob/main/proposals/0226-package-manager-target-based-dep-resolution.md

I created a sample app with a similar setup. While SPM will always resolve the dependency, it will bot be linked/build if it is not required by the target.

So this setup works as expected.

platforms: [.iOS(.v15), .macOS(.v12), .tvOS(.v15), .watchOS(.v8)],
products: products,
dependencies: [
.package(url: "https://github.com/apple/swift-log", from: "1.6.0")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

h: So I think this is going to be a problem as soon as new swift-log version becomes available. AFAIK the SPM doesn't support peer-dependencies, so if someone uses swift-log with a version not being 1.6.0, while we depend on it, Xcode will not build because it doesn't support the same dependency with different versions like NPM.

So basically this will lock our SDK users into exactly the swift-log version we declare, requiring us to release a new SDK version with every update of swift-log, furthermore requiring users to update the Sentry SDK even though they just want a new version of swift-log. At this point I do not have a proposal how to fix this other than asking the user to "inject" their swift-log version by making it conform to a Sentry-included protocol

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, this one is difficult. We are depending on 1.6.0 < 2.0.0, so future minor versions should be covered and correctly resolved if users set their version to 1.8.0 for example, no? Assuming swift-log is not breaking current API with minor version updates.

One way would be to move this to a separate repository. While the same issue remains, we'd not be forced to create a new release in this repo, but rather just update the swift-log-sentry repo. This comes with the whole overhead of maintaining another repo, so I'd say this may not be worth it.

Another possibility would be to see if we can support lower versions of swift-log, for example 1.3.0 up to (excluding) 2.0.0, but then again if there is no demand for older minor versions, there's not much use in supporting those.

As long as swift-log is not regularly changing their major version, the cases where we need to do a release because of breaking swift-log API should be rare. If minor versions introduce additional features which are non-breaking that are useful to us, we need to update our code anyway.

@denrase denrase requested a review from philprime October 22, 2025 13:10
sentryLogger.error(messageString, attributes: attributes)
case .critical:
sentryLogger.fatal(messageString, attributes: attributes)
}
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Bug: Sentry Ignores Log Level Threshold

The SentryLogHandler.log method forwards all log messages to Sentry, ignoring the configured logLevel threshold. This means messages below the intended level are still processed and sent, potentially leading to unwanted Sentry events and unnecessary processing overhead.

Fix in Cursor Fix in Web

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Log Integration: swift-log

4 participants