sentry-react-native icon indicating copy to clipboard operation
sentry-react-native copied to clipboard

Sentry Structured Logging for React Native

Open AbhiPrasad opened this issue 7 months ago • 12 comments

Description

https://develop.sentry.dev/sdk/telemetry/logs/

Sentry is adding support for structured logging. Let's add it to the React Native SDK!

  1. Add the Public API (SDK options and methods) as per the docs

You can look at the Browser SDK for how to structure this: https://github.com/getsentry/sentry-javascript/blob/develop/packages/browser/src/log.ts

const { info, warn, fmt } = Sentry.logger;

info(fmt`Updated profile for user ${userId}`);
warn(fmt`Rate limit approaching for endpoint ${endpoint}. Requests: ${requests}, Limit: ${limit}`);
  1. Make sure the SDK follow the documented behavior
  2. Think about what logging integrations we should add that are react native. For now we don't duplicate breadcrumbs, we will evaluate that at a later time.
  3. After we release this, we should create a GH discussion that contains instructions for setting up the SDK in your repo. Eventually this will be moved into the primary docs. See the JS SDK's GH discussion for inspiration.

AbhiPrasad avatar May 09 '25 20:05 AbhiPrasad

@AbhiPrasad Do we want to also capture SDK logs? or only user logs?

lucas-zimerman avatar May 13 '25 12:05 lucas-zimerman

Anything logs coming from the SDK debug logger should not be captured. These should only be for user facing logs.

We’re exploring if there should be a special debugging mode in the SDK that allows users to send this - but we’ll see.

AbhiPrasad avatar May 13 '25 12:05 AbhiPrasad

If this is going to be used for spotlight, I think we could also use the default logger to log SDK logs, additionally, we could add a flag to the logger to indicate the log will only be sent to spotlight (good for SDK logs) or both spotlight/sentry (use for other logs)

lucas-zimerman avatar May 13 '25 13:05 lucas-zimerman

We have a task to investigate forwarding SDK debug logs via this api - but this will only happen after logs is out of alpha/beta. For more we are not prioritizing this.

AbhiPrasad avatar May 13 '25 13:05 AbhiPrasad

I see, thank you!

I did a quick test on the non-native implementation and the logs are sent without any issues.

One minor issue that I found is that when expanding a log I only see an empty box

image.png

lucas-zimerman avatar May 13 '25 14:05 lucas-zimerman

One minor issue that I found is that when expanding a log I only see an empty box

Could you share the payload you are sending from the SDK? And the link to the sentry org/project?

AbhiPrasad avatar May 13 '25 14:05 AbhiPrasad

Sure thing

https://sentry-sdks.sentry.io/explore/logs/?end=2025-05-14T02%3A59%3A59&logsFields=timestamp&logsFields=message&logsSortBys=-timestamp&project=5428561&start=2025-05-13T03%3A00%3A00

1 log captured

"{"sdk":{"name":"sentry.javascript.react-native","version":"7.0.0-alpha.0"}}
{"type":"log","item_count":1,"content_type":"application/vnd.sentry.items.log+json"}
{"items":[{"timestamp":1747156467.539672,"level":"info","body":"lucas info log","trace_id":"114b234140134f05a933865802aeb906","severity_number":9,"attributes":{"sentry.environment":{"value":"dev","type":"string"},"sentry.sdk.name":{"value":"sentry.javascript.react-native","type":"string"},"sentry.sdk.version":{"value":"7.0.0-alpha.0","type":"string"}}}]}"

And a larger group of logs

{"sdk":{"name":"sentry.javascript.react-native","version":"7.0.0-alpha.0"}}
{"type":"log","item_count":6,"content_type":"application/vnd.sentry.items.log+json"}
{"items":[{"timestamp":1747156230.2881877,"level":"info","body":"info log","trace_id":"114b234140134f05a933865802aeb906","severity_number":9,"attributes":{"sentry.environment":{"value":"dev","type":"string"},"sentry.sdk.name":{"value":"sentry.javascript.react-native","type":"string"},"sentry.sdk.version":{"value":"7.0.0-alpha.0","type":"string"}}},{"timestamp":1747156230.2915647,"level":"trace","body":"trace log","trace_id":"114b234140134f05a933865802aeb906","severity_number":1,"attributes":{"sentry.environment":{"value":"dev","type":"string"},"sentry.sdk.name":{"value":"sentry.javascript.react-native","type":"string"},"sentry.sdk.version":{"value":"7.0.0-alpha.0","type":"string"}}},{"timestamp":1747156230.2932606,"level":"debug","body":"debug log","trace_id":"114b234140134f05a933865802aeb906","severity_number":5,"attributes":{"sentry.environment":{"value":"dev","type":"string"},"sentry.sdk.name":{"value":"sentry.javascript.react-native","type":"string"},"sentry.sdk.version":{"value":"7.0.0-alpha.0","type":"string"}}},{"timestamp":1747156230.2957127,"level":"warn","body":"warn log","trace_id":"114b234140134f05a933865802aeb906","severity_number":13,"attributes":{"sentry.environment":{"value":"dev","type":"string"},"sentry.sdk.name":{"value":"sentry.javascript.react-native","type":"string"},"sentry.sdk.version":{"value":"7.0.0-alpha.0","type":"string"}}},{"timestamp":1747156230.297442,"level":"error","body":"error log","trace_id":"114b234140134f05a933865802aeb906","severity_number":17,"attributes":{"sentry.environment":{"value":"dev","type":"string"},"sentry.sdk.name":{"value":"sentry.javascript.react-native","type":"string"},"sentry.sdk.version":{"value":"7.0.0-alpha.0","type":"string"}}},{"timestamp":1747156230.2990332,"level":"info","body":"info log with data","trace_id":"114b234140134f05a933865802aeb906","severity_number":9,"attributes":{"database":{"value":"admin","type":"string"},"number":{"value":123,"type":"integer"},"obj":{"value":"{\"password\":\"admin\"}","type":"string"},"sentry.environment":{"value":"dev","type":"string"},"sentry.sdk.name":{"value":"sentry.javascript.react-native","type":"string"},"sentry.sdk.version":{"value":"7.0.0-alpha.0","type":"string"}}}]}

lucas-zimerman avatar May 13 '25 14:05 lucas-zimerman

@lucas-zimerman the timestamp is in the future, somehow showing the list works since the time range you selected in the filter is also in the future, but I guess resolving the log details doesn't work for future timestamps?

kahest avatar May 13 '25 14:05 kahest

yup this looks like a timestamp thing - interesting behaviour that it creates. I created https://linear.app/getsentry/issue/LOGS-113 to track this further, we should address this in the product somehow.

AbhiPrasad avatar May 13 '25 14:05 AbhiPrasad

EDIT: envelope header must be 'application/vnd.sentry.items.log+json' instead of 'application/json' in order to show logs at Sentry.

lucas-zimerman avatar May 21 '25 22:05 lucas-zimerman

One integration that we could experiment is to log events from firebase analytics as sentry logs

lucas-zimerman avatar Jun 05 '25 17:06 lucas-zimerman

One integration that we could experiment is to log events from firebase analytics as sentry logs

I'd create a new issue to track this and wait for upvotes/comments. We should only prioritize it if we get enough user feedback!

AbhiPrasad avatar Jun 05 '25 17:06 AbhiPrasad

For everyone following along check out https://docs.sentry.io/platforms/react-native/logs/ for the docs on this!

Big thanks to @lucas-zimerman and @kahest for helping get this across the finish line :)

AbhiPrasad avatar Aug 08 '25 16:08 AbhiPrasad