Skip to content

Commit ab0feb3

Browse files
feat(native): add custom log attributes API (#15491)
<!-- Use this checklist to make sure your PR is ready for merge. You may delete any sections you don't need. --> Vercel previews - [options page](https://sentry-docs-git-joshua-featnativelogscustomattributes.sentry.dev/platforms/native/configuration/options/#logs_with_attributes) - [logs page](https://sentry-docs-git-joshua-featnativelogscustomattributes.sentry.dev/platforms/native/logs/#additional-attributes) - [enriching events > attributes](https://sentry-docs-git-joshua-featnativelogscustomattributes.sentry.dev/platforms/native/enriching-events/attributes/) ## DESCRIBE YOUR PR Documents the feature added in getsentry/sentry-native#1435 (comment) ### ToDo - [x] add general `attributes` overview explaining how to use the API ## IS YOUR CHANGE URGENT? Help us prioritize incoming PRs by letting us know when the change needs to go live. - [ ] Urgent deadline (GA date, etc.): <!-- ENTER DATE HERE --> - [ ] Other deadline: <!-- ENTER DATE HERE --> - [x] None: Not urgent, can wait up to 1 week+ ## SLA - Teamwork makes the dream work, so please add a reviewer to your PRs. - Please give the docs team up to 1 week to review your PR unless you've added an urgent due date to it. Thanks in advance for your help! ## PRE-MERGE CHECKLIST *Make sure you've checked the following before merging your changes:* - [x] Checked Vercel preview for correctness, including links - [ ] PR was reviewed and approved by any necessary SMEs (subject matter experts) - [ ] PR was reviewed and approved by a member of the [Sentry docs team](https://github.com/orgs/getsentry/teams/docs)
1 parent 5d98a10 commit ab0feb3

File tree

3 files changed

+64
-3
lines changed

3 files changed

+64
-3
lines changed

docs/platforms/native/common/configuration/options.mdx

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,3 +123,9 @@ This option enables the [logging integration](/platforms/native/logs), which all
123123
This function is called with an SDK-specific log object, and can return a modified log object, or `sentry_value_new_null()` to drop the log.
124124

125125
</SdkOption>
126+
127+
<SdkOption name="logs_with_attributes" type="bool" defaultValue="false">
128+
129+
Whether calls to `sentry_log_X()` expect an attributes object as the second argument, allowing users to pass in custom log attributes. An example can be found on the [Logs page](/platforms/native/logs/#additional-attributes).
130+
131+
</SdkOption>
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
---
2+
title: Attributes
3+
description: "Learn how to construct custom attributes to enrich logs."
4+
---
5+
6+
Attributes are a specific kind of `sentry_value_t` object which contains items with a `value`, `type` and optional `unit` field. The type is inferred from the given `sentry_value_t` value, which can be `bool`, `int32`, `int64`, `uint64`, `double` or `string`.
7+
8+
```c
9+
sentry_value_t attributes = sentry_value_new_object();
10+
sentry_value_t attr = sentry_value_new_attribute(sentry_value_new_string("my_attribute"), NULL);
11+
sentry_value_t attr_2 = sentry_value_new_attribute(sentry_value_new_int64(INT64_MAX), "fermions");
12+
sentry_value_t attr_3 = sentry_value_new_attribute(sentry_value_new_int64(INT64_MIN), "bosons");
13+
sentry_value_set_by_key(attributes, "my.custom.attribute", attr);
14+
sentry_value_set_by_key(attributes, "number.first", attr_2);
15+
sentry_value_set_by_key(attributes, "number.second", attr_3);
16+
```
17+
18+
Attributes can currently be used to enrich the following:
19+
- [Structured logs](/platforms/native/logs/#additional-attributes)

platform-includes/logs/usage/native.mdx

Lines changed: 39 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,47 @@ Once the feature is enabled on the SDK and the SDK is initialized, you can send
22

33
The API exposes six methods that you can use to log messages at different log levels: `trace`, `debug`, `info`, `warn`, `error`, and `fatal`.
44

5+
```c
6+
sentry_log_info("A simple log message");
7+
sentry_log_error("A %s log message", "formatted");
8+
```
9+
10+
### Additional attributes
11+
12+
In case you want to provide additional attributes directly to the logging functions, you must enable the `logs_with_attributes` option. Any `sentry_log_X()` calls will expect a `sentry_value_t` object of attributes as the first `varg` after the log message.
13+
514
These properties will be sent to Sentry, and can be searched from within the Logs UI, and even added to the Logs views as a dedicated column.
615
716
```c
8-
import io.sentry.Sentry;
17+
sentry_options_set_logs_with_attributes(options, true);
918
10-
sentry_log_info("A simple log message");
11-
sentry_log_error("A %s log message", "formatted");
19+
sentry_value_t attributes = sentry_value_new_object();
20+
sentry_value_t attr = sentry_value_new_attribute(sentry_value_new_string("my_attribute"), NULL);
21+
sentry_value_t attr_2 = sentry_value_new_attribute(sentry_value_new_int64(INT64_MAX), "fermions");
22+
sentry_value_t attr_3 = sentry_value_new_attribute(sentry_value_new_int64(INT64_MIN), "bosons");
23+
sentry_value_set_by_key(attributes, "my.custom.attribute", attr);
24+
sentry_value_set_by_key(attributes, "number.first", attr_2);
25+
sentry_value_set_by_key(attributes, "number.second", attr_3);
26+
27+
sentry_log_debug("logging with %d custom attributes", attributes, 3);
28+
29+
// if the option is enabled, but you want no additional attributes on a log, pass in an empty sentry_value_new_object()
30+
sentry_log_debug("logging with %s custom attributes", sentry_value_new_object(), "no");
31+
```
32+
33+
<Alert level="warning">
34+
For now, logs can only take `bool`, `double`, `string` and signed `int` attributes. If you want to send unsigned integer values, you should convert these to strings before creating the attribute.
35+
</Alert>
36+
37+
These additional attributes take precedence over the default attributes, except for `sentry.message.parameter.X` values; if you pass in a string with format specifiers, these are used for the message parameter values.
38+
39+
```c
40+
sentry_value_t param_attributes = sentry_value_new_object();
41+
sentry_value_t param_attr = sentry_value_new_attribute(sentry_value_new_string("custom parameter"), NULL);
42+
sentry_value_t param_attr_2 = sentry_value_new_attribute(sentry_value_new_string("custom-sdk-name"), NULL);
43+
sentry_value_set_by_key(param_attributes, "sentry.message.parameter.0", param_attr);
44+
sentry_value_set_by_key(param_attributes, "sentry.sdk.name", param_attr_2);
45+
46+
// will only have "custom-sdk-name" as an attribute value, but not "custom parameter" which gets overwritten by "and format-string"
47+
sentry_log_fatal("logging with a custom parameter %s attributes", param_attributes, "and format-string");
1248
```

0 commit comments

Comments
 (0)