-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
docs(apple): Add docs for structured logs #14687
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
denrase
merged 8 commits into
getsentry:master
from
denrase:denrase/cocoa-structured-logs
Aug 26, 2025
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
2f95425
docs(apple): Add docs for structured logs
denrase fa81bdc
update title
denrase 89e148f
update how message is passed
denrase 22b6826
fix incorrect @NO type
denrase 029b597
revert @NO
denrase 27c1788
update spacing
denrase f9fd08b
Merge branch 'denrase/cocoa-structured-logs' of github.com:denrase/se…
denrase 43d5e48
fix template
denrase File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,26 @@ | ||
| --- | ||
| title: Set Up Logs | ||
| sidebar_title: Logs | ||
| description: "Structured logs allow you to send, view and query logs sent from your applications within Sentry." | ||
| sidebar_order: 5755 | ||
| --- | ||
|
|
||
| <Include name="feature-stage-beta-logs.mdx" /> | ||
|
|
||
| With Sentry Structured Logs, you can send text based log information from your applications to Sentry. Once in Sentry, these logs can be viewed alongside relevant errors, searched by text-string, or searched using their individual attributes. | ||
|
|
||
| ## Requirements | ||
|
|
||
| <PlatformContent includePath="logs/requirements" /> | ||
|
|
||
| ## Setup | ||
|
|
||
| <PlatformContent includePath="logs/setup" /> | ||
|
|
||
| ## Usage | ||
|
|
||
| <PlatformContent includePath="logs/usage" /> | ||
|
|
||
| ## Options | ||
|
|
||
| <PlatformContent includePath="logs/options" /> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,46 @@ | ||
| #### beforeSendLog | ||
|
|
||
| To filter logs, or update them before they are sent to Sentry, you can use the `beforeSendLog` option. | ||
|
|
||
| ```swift {tabTitle:Swift} | ||
| import Sentry | ||
|
|
||
| SentrySDK.start { options in | ||
| options.dsn = "___PUBLIC_DSN___" | ||
| options.experimental.enableLogs = true | ||
|
|
||
| options.beforeSendLog = { log in | ||
| if log.level == .info { | ||
| // Filter out all info logs | ||
| return nil | ||
| } | ||
| return log | ||
| } | ||
| } | ||
| ``` | ||
|
|
||
| ```objc {tabTitle:Objective-C} | ||
| @import Sentry; | ||
|
|
||
| [SentrySDK startWithConfigureOptions:^(SentryOptions *options) { | ||
| options.dsn = @"___PUBLIC_DSN___"; | ||
| options.experimental.enableLogs = YES; | ||
|
|
||
| options.beforeSendLog = ^SentryLog* (SentryLog *log) { | ||
| if (log.level == SentryStructuredLogLevelInfo) { | ||
| // Filter out all info logs | ||
| return nil; | ||
| } | ||
| return log; | ||
| }; | ||
| }]; | ||
| ``` | ||
|
|
||
| The `beforeSendLog` function receives a log object, and should return the log object if you want it to be sent to Sentry, or `nil` if you want to discard it. | ||
|
|
||
| The log object has the following properties: | ||
|
|
||
| - `level`: The log level (trace, debug, info, warn, error, fatal) | ||
| - `message`: The message to be logged | ||
| - `timestamp`: The timestamp of the log | ||
| - `attributes`: The attributes of the log |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| Logs for Apple platforms are supported in Sentry Cocoa SDK version `8.55.0` and above. Logs are still experimental and the API may change in the future. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,21 @@ | ||
| To enable logging, you need to initialize the SDK with the experimental `enableLogs` option set to `true`. | ||
|
|
||
| ```swift {tabTitle:Swift} | ||
| import Sentry | ||
|
|
||
| SentrySDK.start { options in | ||
| options.dsn = "___PUBLIC_DSN___" | ||
| // Enable logs to be sent to Sentry | ||
| options.experimental.enableLogs = true | ||
| } | ||
| ``` | ||
|
|
||
| ```objc {tabTitle:Objective-C} | ||
| @import Sentry; | ||
|
|
||
| [SentrySDK startWithConfigureOptions:^(SentryOptions *options) { | ||
| options.dsn = @"___PUBLIC_DSN___"; | ||
| // Enable logs to be sent to Sentry | ||
| options.experimental.enableLogs = YES; | ||
| }]; | ||
| ``` |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,86 @@ | ||
| Once the feature is enabled on the SDK and the SDK is initialized, you can send logs using the `Sentry.logger` APIs. | ||
|
|
||
| The `Sentry.logger` namespace exposes six methods that you can use to log messages at different log levels: `trace`, `debug`, `info`, `warn`, `error`, and `fatal`. Supported types for attributes are Strings, Int, Double, and Bool. | ||
|
|
||
| ```swift {tabTitle:Swift} | ||
| import Sentry | ||
|
|
||
| let logger = SentrySDK.logger | ||
|
|
||
| // Log messages without attributes | ||
| logger.trace("Starting database connection") | ||
| logger.debug("Cache miss for user") | ||
| logger.info("Updated profile") | ||
|
|
||
| // Log messages with attributes | ||
| logger.trace("Starting database connection", attributes: ["database": "users"]) | ||
| logger.debug("Cache miss for user", attributes: ["userId": 123]) | ||
| logger.info("Updated profile", attributes: ["profileId": 345]) | ||
| logger.warn("Rate limit reached for endpoint", attributes: [ | ||
| "endpoint": "/api/results/", | ||
| "isEnterprise": false | ||
| ]) | ||
| logger.error("Failed to process payment", attributes: [ | ||
| "orderId": "order_123", | ||
| "amount": 99.99 | ||
| ]) | ||
| logger.fatal("Database connection pool exhausted", attributes: [ | ||
| "database": "users", | ||
| "activeConnections": 100 | ||
| ]) | ||
| ``` | ||
|
|
||
| ```objc {tabTitle:Objective-C} | ||
| @import Sentry; | ||
|
|
||
| SentryLogger *logger = SentrySDK.logger; | ||
|
|
||
| // Log messages without attributes | ||
| [logger trace:@"Starting database connection"]; | ||
| [logger debug:@"Cache miss for user"]; | ||
| [logger info:@"Updated profile"]; | ||
|
|
||
| // Log messages with attributes | ||
| [logger trace:@"Starting database connection" attributes:@{@"database": @"users"}]; | ||
| [logger debug:@"Cache miss for user" attributes:@{@"userId": @123}]; | ||
| [logger info:@"Updated profile" attributes:@{@"profileId": @345}]; | ||
| [logger warn:@"Rate limit reached for endpoint" attributes:@{ | ||
| @"endpoint": @"/api/results/", | ||
| @"isEnterprise": @NO | ||
denrase marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| }]; | ||
| [logger error:@"Failed to process payment" attributes:@{ | ||
| @"orderId": @"order_123", | ||
| @"amount": @99.99 | ||
| }]; | ||
| [logger fatal:@"Database connection pool exhausted" attributes:@{ | ||
| @"database": @"users", | ||
| @"activeConnections": @100 | ||
| }]; | ||
| ``` | ||
| ### String Interpolation (Swift Only) | ||
denrase marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| Swift supports automatic extraction of interpolated values as attributes. When you use string interpolation in your log messages, supported types (Strings, Int, Double, and Bool) are automatically extracted and added as attributes with the key format `sentry.message.parameter.{index}`. | ||
| ```swift | ||
| let userId = "user_123" | ||
| let orderCount = 5 | ||
| let isPremium = true | ||
| let totalAmount = 99.99 | ||
| // String interpolation automatically extracts values as attributes | ||
| logger.info("User \(userId) placed \(orderCount) orders, premium: \(isPremium), total: $\(totalAmount)") | ||
| // This is equivalent to manually specifying attributes: | ||
| let message = "User \(userId) placed \(orderCount) orders, premium: \(isPremium), total: $\(totalAmount)" | ||
| logger.info( | ||
| message, | ||
| attributes: [ | ||
| "sentry.message.template": "User {0} placed {1} orders, premium: {2}, total: {3}", | ||
| "sentry.message.parameter.0": userId, | ||
| "sentry.message.parameter.1": orderCount, | ||
| "sentry.message.parameter.2": isPremium, | ||
| "sentry.message.parameter.3": totalAmount | ||
| ] | ||
denrase marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| ) | ||
| ``` | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.