Skip to content

Commit e64ff21

Browse files
authored
docs(apple): Add docs for structured logs (#14687)
1 parent a65e178 commit e64ff21

File tree

6 files changed

+230
-16
lines changed

6 files changed

+230
-16
lines changed

docs/platforms/apple/common/index.mdx

Lines changed: 50 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -36,13 +36,13 @@ Select which Sentry features you'd like to install in addition to Error Monitori
3636

3737
<PlatformSection notSupported={["apple.tvos", "apple.watchos", "apple.visionos"]}>
3838

39-
<OnboardingOptionButtons options={['error-monitoring', 'performance', 'profiling', 'session-replay']}/>
39+
<OnboardingOptionButtons options={['error-monitoring', 'performance', 'profiling', 'session-replay', 'logs']}/>
4040

4141
</PlatformSection>
4242

4343
<PlatformSection notSupported={["apple.ios", "apple.macos"]}>
4444

45-
<OnboardingOptionButtons options={['error-monitoring', 'performance']}/>
45+
<OnboardingOptionButtons options={['error-monitoring', 'performance', 'logs']}/>
4646

4747
</PlatformSection>
4848

@@ -104,25 +104,30 @@ func application(_ application: UIApplication,
104104
// Adds IP for users.
105105
// For more information, visit: https://docs.sentry.io/platforms/apple/data-management/data-collected/
106106
options.sendDefaultPii = true
107-
108107
// ___PRODUCT_OPTION_START___ performance
108+
109109
// Set tracesSampleRate to 1 to capture 100% of transactions for performance monitoring.
110110
// We recommend adjusting this value in production.
111111
options.tracesSampleRate = 1
112112
// ___PRODUCT_OPTION_END___ performance
113-
114113
// ___PRODUCT_OPTION_START___ profiling
114+
115115
options.configureProfiling = {
116116
$0.lifecycle = .trace
117117
$0.sessionSampleRate = 1
118118
}
119119
// ___PRODUCT_OPTION_END___ profiling
120-
121120
// ___PRODUCT_OPTION_START___ session-replay
121+
122122
// Record session replays for 100% of errors and 10% of sessions
123123
options.sessionReplay.onErrorSampleRate = 1.0
124124
options.sessionReplay.sessionSampleRate = 0.1
125125
// ___PRODUCT_OPTION_END___ session-replay
126+
// ___PRODUCT_OPTION_START___ logs
127+
128+
// Enable logs to be sent to Sentry
129+
options.experimental.enableLogs = true
130+
// ___PRODUCT_OPTION_END___ logs
126131
}
127132

128133
return true
@@ -141,25 +146,30 @@ func application(_ application: UIApplication,
141146
// Adds IP for users.
142147
// For more information, visit: https://docs.sentry.io/platforms/apple/data-management/data-collected/
143148
options.sendDefaultPii = YES;
144-
145149
// ___PRODUCT_OPTION_START___ performance
150+
146151
// Set tracesSampleRate to 1.0 to capture 100% of transactions for performance monitoring.
147152
// We recommend adjusting this value in production.
148153
options.tracesSampleRate = @1.f;
149154
// ___PRODUCT_OPTION_END___ performance
150-
151155
// ___PRODUCT_OPTION_START___ profiling
156+
152157
options.configureProfiling = ^(SentryProfileOptions *profiling) {
153158
profiling.lifecycle = SentryProfileLifecycleTrace;
154159
profiling.sessionSampleRate = 1.f;
155160
};
156161
// ___PRODUCT_OPTION_END___ profiling
157-
158162
// ___PRODUCT_OPTION_START___ session-replay
163+
159164
// Record session replays for 100% of errors and 10% of sessions
160165
options.sessionReplay.onErrorSampleRate = 1.0;
161166
options.sessionReplay.sessionSampleRate = 0.1;
162167
// ___PRODUCT_OPTION_END___ session-replay
168+
// ___PRODUCT_OPTION_START___ logs
169+
170+
// Enable logs to be sent to Sentry
171+
options.experimental.enableLogs = YES;
172+
// ___PRODUCT_OPTION_END___ logs
163173
}];
164174

165175
return YES;
@@ -179,25 +189,29 @@ struct SwiftUIApp: App {
179189
// Adds IP for users.
180190
// For more information, visit: https://docs.sentry.io/platforms/apple/data-management/data-collected/
181191
options.sendDefaultPii = true
182-
183192
// ___PRODUCT_OPTION_START___ performance
193+
184194
// Set tracesSampleRate to 1 to capture 100% of transactions for performance monitoring.
185195
// We recommend adjusting this value in production.
186196
options.tracesSampleRate = 1
187197
// ___PRODUCT_OPTION_END___ performance
188-
189198
// ___PRODUCT_OPTION_START___ profiling
199+
190200
options.configureProfiling = {
191201
$0.lifecycle = .trace
192202
$0.sessionSampleRate = 1
193203
}
194204
// ___PRODUCT_OPTION_END___ profiling
195-
196205
// ___PRODUCT_OPTION_START___ session-replay
197206
// Record session replays for 100% of errors and 10% of sessions
198207
options.sessionReplay.onErrorSampleRate = 1.0
199208
options.sessionReplay.sessionSampleRate = 0.1
200209
// ___PRODUCT_OPTION_END___ session-replay
210+
211+
// ___PRODUCT_OPTION_START___ logs
212+
// Enable logs to be sent to Sentry
213+
options.experimental.enableLogs = true
214+
// ___PRODUCT_OPTION_END___ logs
201215
}
202216
}
203217
}
@@ -219,19 +233,24 @@ func application(_ application: UIApplication,
219233
// Adds IP for users.
220234
// For more information, visit: https://docs.sentry.io/platforms/apple/data-management/data-collected/
221235
options.sendDefaultPii = true
222-
223236
// ___PRODUCT_OPTION_START___ performance
237+
224238
// Set tracesSampleRate to 1 to capture 100% of transactions for performance monitoring.
225239
// We recommend adjusting this value in production.
226240
options.tracesSampleRate = 1
227241
// ___PRODUCT_OPTION_END___ performance
228-
229242
// ___PRODUCT_OPTION_START___ profiling
243+
230244
options.configureProfiling = {
231245
$0.lifecycle = .trace
232246
$0.sessionSampleRate = 1
233247
}
234248
// ___PRODUCT_OPTION_END___ profiling
249+
// ___PRODUCT_OPTION_START___ logs
250+
251+
// Enable logs to be sent to Sentry
252+
options.experimental.enableLogs = true
253+
// ___PRODUCT_OPTION_END___ logs
235254
}
236255

237256
return true
@@ -263,6 +282,11 @@ func application(_ application: UIApplication,
263282
profiling.sessionSampleRate = 1.f;
264283
};
265284
// ___PRODUCT_OPTION_END___ profiling
285+
// ___PRODUCT_OPTION_START___ logs
286+
287+
// Enable logs to be sent to Sentry
288+
options.experimental.enableLogs = YES;
289+
// ___PRODUCT_OPTION_END___ logs
266290
}];
267291

268292
return YES;
@@ -282,19 +306,24 @@ struct SwiftUIApp: App {
282306
// Adds IP for users.
283307
// For more information, visit: https://docs.sentry.io/platforms/apple/data-management/data-collected/
284308
options.sendDefaultPii = true
285-
286309
// ___PRODUCT_OPTION_START___ performance
310+
287311
// Set tracesSampleRate to 1 to capture 100% of transactions for performance monitoring.
288312
// We recommend adjusting this value in production.
289313
options.tracesSampleRate = 1
290314
// ___PRODUCT_OPTION_END___ performance
291-
292315
// ___PRODUCT_OPTION_START___ profiling
316+
293317
options.configureProfiling = {
294318
$0.lifecycle = .trace
295319
$0.sessionSampleRate = 1
296320
}
297321
// ___PRODUCT_OPTION_END___ profiling
322+
// ___PRODUCT_OPTION_START___ logs
323+
324+
// Enable logs to be sent to Sentry
325+
options.experimental.enableLogs = true
326+
// ___PRODUCT_OPTION_END___ logs
298327
}
299328
}
300329
}
@@ -318,12 +347,17 @@ func applicationDidFinishLaunching() {
318347
// Adds IP for users.
319348
// For more information, visit: https://docs.sentry.io/platforms/apple/data-management/data-collected/
320349
options.sendDefaultPii = true
321-
322350
// ___PRODUCT_OPTION_START___ performance
351+
323352
// Set tracesSampleRate to 1 to capture 100% of transactions for performance monitoring.
324353
// We recommend adjusting this value in production.
325354
options.tracesSampleRate = 1
326355
// ___PRODUCT_OPTION_END___ performance
356+
// ___PRODUCT_OPTION_START___ logs
357+
358+
// Enable logs to be sent to Sentry
359+
options.experimental.enableLogs = true
360+
// ___PRODUCT_OPTION_END___ logs
327361
}
328362
}
329363
```
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
---
2+
title: Set Up Logs
3+
sidebar_title: Logs
4+
description: "Structured logs allow you to send, view and query logs sent from your applications within Sentry."
5+
sidebar_order: 5755
6+
---
7+
8+
<Include name="feature-stage-beta-logs.mdx" />
9+
10+
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.
11+
12+
## Requirements
13+
14+
<PlatformContent includePath="logs/requirements" />
15+
16+
## Setup
17+
18+
<PlatformContent includePath="logs/setup" />
19+
20+
## Usage
21+
22+
<PlatformContent includePath="logs/usage" />
23+
24+
## Options
25+
26+
<PlatformContent includePath="logs/options" />
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
#### beforeSendLog
2+
3+
To filter logs, or update them before they are sent to Sentry, you can use the `beforeSendLog` option.
4+
5+
```swift {tabTitle:Swift}
6+
import Sentry
7+
8+
SentrySDK.start { options in
9+
options.dsn = "___PUBLIC_DSN___"
10+
options.experimental.enableLogs = true
11+
12+
options.beforeSendLog = { log in
13+
if log.level == .info {
14+
// Filter out all info logs
15+
return nil
16+
}
17+
return log
18+
}
19+
}
20+
```
21+
22+
```objc {tabTitle:Objective-C}
23+
@import Sentry;
24+
25+
[SentrySDK startWithConfigureOptions:^(SentryOptions *options) {
26+
options.dsn = @"___PUBLIC_DSN___";
27+
options.experimental.enableLogs = YES;
28+
29+
options.beforeSendLog = ^SentryLog* (SentryLog *log) {
30+
if (log.level == SentryStructuredLogLevelInfo) {
31+
// Filter out all info logs
32+
return nil;
33+
}
34+
return log;
35+
};
36+
}];
37+
```
38+
39+
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.
40+
41+
The log object has the following properties:
42+
43+
- `level`: The log level (trace, debug, info, warn, error, fatal)
44+
- `message`: The message to be logged
45+
- `timestamp`: The timestamp of the log
46+
- `attributes`: The attributes of the log
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
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.
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
To enable logging, you need to initialize the SDK with the experimental `enableLogs` option set to `true`.
2+
3+
```swift {tabTitle:Swift}
4+
import Sentry
5+
6+
SentrySDK.start { options in
7+
options.dsn = "___PUBLIC_DSN___"
8+
// Enable logs to be sent to Sentry
9+
options.experimental.enableLogs = true
10+
}
11+
```
12+
13+
```objc {tabTitle:Objective-C}
14+
@import Sentry;
15+
16+
[SentrySDK startWithConfigureOptions:^(SentryOptions *options) {
17+
options.dsn = @"___PUBLIC_DSN___";
18+
// Enable logs to be sent to Sentry
19+
options.experimental.enableLogs = YES;
20+
}];
21+
```
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
Once the feature is enabled on the SDK and the SDK is initialized, you can send logs using the `Sentry.logger` APIs.
2+
3+
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.
4+
5+
```swift {tabTitle:Swift}
6+
import Sentry
7+
8+
let logger = SentrySDK.logger
9+
10+
// Log messages without attributes
11+
logger.trace("Starting database connection")
12+
logger.debug("Cache miss for user")
13+
logger.info("Updated profile")
14+
15+
// Log messages with attributes
16+
logger.trace("Starting database connection", attributes: ["database": "users"])
17+
logger.debug("Cache miss for user", attributes: ["userId": 123])
18+
logger.info("Updated profile", attributes: ["profileId": 345])
19+
logger.warn("Rate limit reached for endpoint", attributes: [
20+
"endpoint": "/api/results/",
21+
"isEnterprise": false
22+
])
23+
logger.error("Failed to process payment", attributes: [
24+
"orderId": "order_123",
25+
"amount": 99.99
26+
])
27+
logger.fatal("Database connection pool exhausted", attributes: [
28+
"database": "users",
29+
"activeConnections": 100
30+
])
31+
```
32+
33+
```objc {tabTitle:Objective-C}
34+
@import Sentry;
35+
36+
SentryLogger *logger = SentrySDK.logger;
37+
38+
// Log messages without attributes
39+
[logger trace:@"Starting database connection"];
40+
[logger debug:@"Cache miss for user"];
41+
[logger info:@"Updated profile"];
42+
43+
// Log messages with attributes
44+
[logger trace:@"Starting database connection" attributes:@{@"database": @"users"}];
45+
[logger debug:@"Cache miss for user" attributes:@{@"userId": @123}];
46+
[logger info:@"Updated profile" attributes:@{@"profileId": @345}];
47+
[logger warn:@"Rate limit reached for endpoint" attributes:@{
48+
@"endpoint": @"/api/results/",
49+
@"isEnterprise": @NO
50+
}];
51+
[logger error:@"Failed to process payment" attributes:@{
52+
@"orderId": @"order_123",
53+
@"amount": @99.99
54+
}];
55+
[logger fatal:@"Database connection pool exhausted" attributes:@{
56+
@"database": @"users",
57+
@"activeConnections": @100
58+
}];
59+
```
60+
61+
### String Interpolation (Swift Only)
62+
63+
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}`.
64+
65+
```swift
66+
let userId = "user_123"
67+
let orderCount = 5
68+
let isPremium = true
69+
let totalAmount = 99.99
70+
71+
// String interpolation automatically extracts values as attributes
72+
logger.info("User \(userId) placed \(orderCount) orders, premium: \(isPremium), total: $\(totalAmount)")
73+
74+
// This is equivalent to manually specifying attributes:
75+
let message = "User \(userId) placed \(orderCount) orders, premium: \(isPremium), total: $\(totalAmount)"
76+
logger.info(
77+
message,
78+
attributes: [
79+
"sentry.message.template": "User {0} placed {1} orders, premium: {2}, total: {3}",
80+
"sentry.message.parameter.0": userId,
81+
"sentry.message.parameter.1": orderCount,
82+
"sentry.message.parameter.2": isPremium,
83+
"sentry.message.parameter.3": totalAmount
84+
]
85+
)
86+
```

0 commit comments

Comments
 (0)