Set Up Logs

Structured logs allow you to send, view and query logs sent from your applications within Sentry.

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.

Logs for Flutter are supported in Sentry Flutter SDK version 9.0.0-RC.3 and above.

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

Copied
await SentryFlutter.init(
  (options) {
    options.dsn = "https://examplePublicKey@o0.ingest.sentry.io/0";
    // Enable logs to be sent to Sentry
    options.enableLogs = true;
  },
);

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.

You can pass additional attributes directly to the logging functions. 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.

Copied
Sentry.logger.info("A simple log message");
Sentry.logger.warn("This is a warning log with attributes.", attributes: {
  'attribute1': SentryLogAttribute.string('string'),
  'attribute2': SentryLogAttribute.int(1),
  'attribute3': SentryLogAttribute.double(1.0),
  'attribute4': SentryLogAttribute.bool(true),
});

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

Copied
await SentryFlutter.init(
  (options) {
    options.dsn = "https://examplePublicKey@o0.ingest.sentry.io/0";
    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.

Was this helpful?
Help improve this content
Our documentation is open source and available on GitHub. Your contributions are welcome, whether fixing a typo (drat!) or suggesting an update ("yeah, this would be better").