---
title: "Logs"
description: "Structured logs allow you to send, view and query logs sent from your applications within Sentry."
url: https://docs.sentry.io/platforms/react-native/logs/
---

# Set Up Logs | Sentry for React Native

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](https://docs.sentry.io/platforms/react-native/logs.md#requirements)

##### Version requirement

Logs for React Native<!-- --> requires <!-- -->Sentry React Native SDK<!-- --> version `7.0.0` or newer.

## [Setup](https://docs.sentry.io/platforms/react-native/logs.md#setup)

To enable logging, you need to initialize the SDK with the `enableLogs` option set to `true`.

```js
Sentry.init({
  dsn: "___PUBLIC_DSN___",
  // Enable logs to be sent to Sentry
  enableLogs: true,
});
```

## [Usage](https://docs.sentry.io/platforms/react-native/logs.md#usage)

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`.

Aside from the primary logging methods, we've provided a format text function, `Sentry.logger.fmt`, that you can use to insert properties into to your log entries.

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.

```js
Sentry.logger.error(
  Sentry.logger.fmt`Uh oh, something broke, here's the error: '${error}'`
);
Sentry.logger.info(
  Sentry.logger.fmt`'${user.username}' added '${product.name}' to cart.`
);
```

You can also pass additional attributes directly to the logging functions, avoiding the need to use the `fmt` function.

```js
Sentry.logger.trace("Starting database connection", { database: "users" });
Sentry.logger.debug("Cache miss for user", { userId: 123 });
Sentry.logger.info("Updated profile", { profileId: 345 });
Sentry.logger.warn("Rate limit reached for endpoint", {
  endpoint: "/api/results/",
  isEnterprise: false,
});
Sentry.logger.error("Failed to process payment", {
  orderId: "order_123",
  amount: 99.99,
});
Sentry.logger.fatal("Database connection pool exhausted", {
  database: "users",
  activeConnections: 100,
});
```

Setting a log message is required for the Sentry SDK to send the log to Sentry.

With version `10.32.0` and above, you can use scope APIs to set attributes on the SDK's scopes which will be applied to all logs as long as the respective scopes are active. For the time being, only `string`, `number` and `boolean` attribute values are supported.

```js
Sentry.getGlobalScope().setAttributes({
  is_admin: true,
  auth_provider: "sentry",
});

Sentry.withScope((scope) => {
  scope.setAttribute("step", "authentication");

  // scope attributes `is_admin`, `auth_provider` and `step` are added
  Sentry.logger.info(`user ${user.id} logged in`, { activeSince: 100 });
  Sentry.logger.info(`updated ${user.id} last activity`);
});

// scope attributes `is_admin` and `auth_provider` are added
Sentry.logger.warn("stale website version, reloading page");
```

## [Options](https://docs.sentry.io/platforms/react-native/logs.md#options)

#### [beforeSendLog](https://docs.sentry.io/platforms/react-native/logs.md#beforesendlog)

To filter logs, or update them before they are sent to Sentry, you can use the `beforeSendLog` option.

```js
Sentry.init({
  dsn: "___PUBLIC_DSN___",
  enableLogs: true,
  beforeSendLog: (log) => {
    if (log.level === "info") {
      // Filter out all info logs
      return null;
    }

    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 `null` if you want to discard it.

## [Default Attributes](https://docs.sentry.io/platforms/react-native/logs.md#default-attributes)

The React Native SDK automatically sets several default attributes on all log entries to provide context and improve debugging:

### [Core Attributes](https://docs.sentry.io/platforms/react-native/logs.md#core-attributes)

* `environment`: The environment set in the SDK if defined. This is sent from the SDK as `sentry.environment`.
* `release`: The release set in the SDK if defined. This is sent from the SDK as `sentry.release`.
* `sdk.name`: The name of the SDK that sent the log. This is sent from the SDK as `sentry.sdk.name`.
* `sdk.version`: The version of the SDK that sent the log. This is sent from the SDK as `sentry.sdk.version`.

### [Message Template Attributes](https://docs.sentry.io/platforms/react-native/logs.md#message-template-attributes)

If the log was parameterized, Sentry adds the message template and parameters as log attributes.

* `message.template`: The parameterized template string. This is sent from the SDK as `sentry.message.template`.
* `message.parameter.X`: The parameters to fill the template string. X can either be the number that represent the parameter's position in the template string (`sentry.message.parameter.0`, `sentry.message.parameter.1`, etc) or the parameter's name (`sentry.message.parameter.item_id`, `sentry.message.parameter.user_id`, etc). This is sent from the SDK as `sentry.message.parameter.X`.

For example, with the following log:

```javascript
const user = "John";
const product = "Product 1";
Sentry.logger.info(
  Sentry.logger.fmt`'${user}' added '${product}' to cart.`,
);
```

Sentry will add the following attributes:

* `message.template`: "%s added %s to cart."
* `message.parameter.0`: "John"
* `message.parameter.1`: "Product 1"

### [User Attributes](https://docs.sentry.io/platforms/react-native/logs.md#user-attributes)

If user information is available in the current scope, the following attributes are added to the log:

* `user.id`: The user ID.
* `user.name`: The username.
* `user.email`: The email address.

### [Message Template Attributes](https://docs.sentry.io/platforms/react-native/logs.md#message-template-attributes)

If the log was parameterized (like with `Sentry.logger().error("A %s log message", "formatted");`), Sentry adds the message template and parameters as log attributes.

### [Integration Attributes](https://docs.sentry.io/platforms/react-native/logs.md#integration-attributes)

If a log is generated by an SDK integration, the SDK will set additional attributes to help you identify the source of the log.

* `origin`: The origin of the log. This is sent from the SDK as `sentry.origin`.

To filter out all SDK-generated logs and keep only your application logs, use the following snippet:

```js
Sentry.init({
  dsn: "___PUBLIC_DSN___",
  enableLogs: true,
  beforeSendLog: (log) => {
    if (log.attributes?.["sentry.origin"] !== undefined) {
      return null;
    }
    return log;
  },
});
```

### [Log Object Properties](https://docs.sentry.io/platforms/react-native/logs.md#log-object-properties)

The log object has the following properties:

* `level`: (string - one of `trace`, `debug`, `info`, `warn`, `error`, `fatal`) The log level.
* `message`: (string) The message to be logged.
* `timestamp`: (number) The timestamp of the log.
* `attributes`: (object) The attributes of the log.

## [Troubleshooting](https://docs.sentry.io/platforms/react-native/logs.md#troubleshooting)

### [Missing Logs for Crashes](https://docs.sentry.io/platforms/react-native/logs.md#missing-logs-for-crashes)

Logs can get lost in certain crash scenarios, if the SDK can not send the logs before the app terminates. We are [currently working on improving](https://github.com/getsentry/sentry-react-native/issues/5125) this to ensure that all logs are sent, at the latest on the next app restart.

## [Other Logging Integrations](https://docs.sentry.io/platforms/react-native/logs.md#other-logging-integrations)

Available integrations:

* [Console logging](https://docs.sentry.io/platforms/react-native/integrations/console-logging.md)
* [Consola](https://docs.sentry.io/platforms/react-native/integrations/consola.md)

If there's an integration you would like to see, open a [new issue on GitHub](https://github.com/getsentry/sentry-react-native/issues/new/choose).
