---
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/dart/guides/flutter/logs/
---

# Set Up Logs | Sentry for Flutter

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/dart/guides/flutter/logs.md#requirements)

Logs for Flutter are supported in Sentry Flutter SDK version `9.0.0` and above.

## [Setup](https://docs.sentry.io/platforms/dart/guides/flutter/logs.md#setup)

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

```dart
await SentryFlutter.init(
  (options) {
    options.dsn = "___PUBLIC_DSN___";
    // Enable logs to be sent to Sentry
    options.enableLogs = true;
  },
);
```

## [Usage](https://docs.sentry.io/platforms/dart/guides/flutter/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 `logger` namespace exposes six methods that you can use to log messages at different log levels: `trace`, `debug`, `info`, `warning`, `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.

When using the `fmt` function, you must use the `%s` placeholder for each value you want to insert.

```dart
Sentry.logger.fmt.error('Uh oh, something broke, here is the error: %s', [
  errorMsg
], attributes: {
  'additional_info': SentryAttribute.string('some info'),
});
Sentry.logger.fmt.info("%s added %s to cart.", [user.username, product.name]);
```

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

```dart
Sentry.logger.error('Uh oh, something broke, here is the error: $errorMsg',
    attributes: {
      'error': SentryAttribute.string(errorMsg),
      'some_info': SentryAttribute.string('some info'),
    });
Sentry.logger.info('User ${user.username} added ${product.name} to cart.', attributes: {
  'user': SentryAttribute.string(user.username),
  'product': SentryAttribute.string(product.name),
});
```

## [Options](https://docs.sentry.io/platforms/dart/guides/flutter/logs.md#options)

#### [beforeSendLog](https://docs.sentry.io/platforms/dart/guides/flutter/logs.md#beforesendlog)

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

```dart
await SentryFlutter.init(
  (options) {
    options.dsn = "___PUBLIC_DSN___";
    options.beforeSendLog = (log) {
      if (log.level == SentryLogLevel.info) {
        // Filter out all info logs
        return null;
      }

      return log;
    };
  },
);
```

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

## [Best Practices](https://docs.sentry.io/platforms/dart/guides/flutter/logs.md#best-practices)

### [Wide Events Over Scattered Logs](https://docs.sentry.io/platforms/dart/guides/flutter/logs.md#wide-events-over-scattered-logs)

Instead of many thin logs that are hard to correlate, emit one comprehensive log per operation with all relevant context.

This makes debugging dramatically faster — one query returns everything about a specific order, user, or request.

```dart
// ❌ Scattered thin logs
Sentry.logger.info('Starting checkout');
Sentry.logger.info('Validating cart');
Sentry.logger.info('Processing payment');
Sentry.logger.info('Checkout complete');

// ✅ One wide event with full context
Sentry.logger.info('Checkout completed', attributes: {
  'order_id': SentryAttribute.string(order.id),
  'user_id': SentryAttribute.string(user.id),
  'user_tier': SentryAttribute.string(user.subscription),
  'cart_value': SentryAttribute.double(cart.total),
  'item_count': SentryAttribute.int(cart.items.length),
  'payment_method': SentryAttribute.string('stripe'),
  'duration_ms': SentryAttribute.int(
      DateTime.now().difference(startTime).inMilliseconds),
});
```

### [Include Business Context](https://docs.sentry.io/platforms/dart/guides/flutter/logs.md#include-business-context)

Add attributes that help you prioritize and debug:

* **User context** — tier, account age, lifetime value
* **Transaction data** — order value, item count
* **Feature state** — active feature flags
* **Request metadata** — endpoint, method, duration

This lets you filter logs by high-value customers or specific features.

```dart
Sentry.logger.info('API request completed', attributes: {
  // User context
  'user_id': SentryAttribute.string(user.id),
  'user_tier': SentryAttribute.string(user.plan),
  'account_age_days': SentryAttribute.int(user.ageDays),

  // Request data
  'endpoint': SentryAttribute.string('/api/orders'),
  'method': SentryAttribute.string('POST'),
  'duration_ms': SentryAttribute.int(234),

  // Business context
  'order_value': SentryAttribute.double(149.99),
});
```

### [Consistent Attribute Naming](https://docs.sentry.io/platforms/dart/guides/flutter/logs.md#consistent-attribute-naming)

Pick a naming convention and stick with it across your codebase. Inconsistent names make queries impossible.

**Recommended:** Use `snake_case` for custom attributes to match Dart conventions.

```dart
// ❌ Inconsistent naming
{'user': SentryAttribute.string('123')}
{'userId': SentryAttribute.string('123')}
{'user_id': SentryAttribute.string('123')}
{'UserID': SentryAttribute.string('123')}

// ✅ Consistent snake_case
{
  'user_id': SentryAttribute.string('123'),
  'order_id': SentryAttribute.string('456'),
  'cart_value': SentryAttribute.double(99.99),
  'item_count': SentryAttribute.int(3),
}
```

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

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

### [Core Attributes](https://docs.sentry.io/platforms/dart/guides/flutter/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/dart/guides/flutter/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:

```dart
Sentry.logger.fmt.info("%s added %s to cart.", ["John", "Product 1"]);
```

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/dart/guides/flutter/logs.md#user-attributes)

* `user.id`: The user ID. Maps to id in the User payload, which is set by default by the SDKs.

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

* `user.name`: The username. Maps to username in the User payload.
* `user.email`: The email address. Maps to email in the User payload.

### [Message Template Attributes](https://docs.sentry.io/platforms/dart/guides/flutter/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/dart/guides/flutter/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`.

## [Troubleshooting](https://docs.sentry.io/platforms/dart/guides/flutter/logs.md#troubleshooting)

### [Missing Logs for Crashes](https://docs.sentry.io/platforms/dart/guides/flutter/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-dart/issues/3227) this to ensure that all logs are sent, at the latest on the next app restart.
