Logging Integration for Dart
Learn more about the Sentry Logging integration for the Dart SDK.
This integration connects Sentry with the popular Dart logging package, providing the following capabilities:
- Sends your log messages as Sentry Structured Logs (enabled automatically when this integration is added)
- Captures breadcrumbs from your log calls
- Converts error-level logs into Sentry error events
- Works with your existing logging code
This page covers the instrumentation of the Dart Logging package. This integration also supports creating structured logs. However, if you're looking to set up Sentry structured logs in general, visit our Structured Logs documentation.
On SDK v9, enableLogs controls automatic forwarding of log records. In v10, that option is removed.
Set minSentryLogLevel: Level.OFF to stop forwarding logs from the Dart logging package while retaining breadcrumbs and error events. Direct Sentry.logger calls are unaffected. To drop all Sentry logs, return null from beforeSendLog.
To add the Logging integration, add the sentry_logging dependency.
pubspec.yamldependencies:
sentry: ^10.5.0
sentry_logging: ^10.5.0
logging: ^1.0.2
dependencies:
sentry: ^10.5.0
sentry_logging: ^10.5.0
logging: ^1.0.2
Add LoggingIntegration during SDK initialization. In Flutter apps, use SentryFlutter.init so native crash reporting and Flutter integrations are also initialized. The example below uses the core Dart SDK:
import 'package:sentry_logging/sentry_logging.dart';
import 'package:sentry/sentry.dart';
Future<void> main() async {
await Sentry.init(
(options) {
options.dsn = '___PUBLIC_DSN___';
options.addIntegration(LoggingIntegration());
},
appRunner: initApp, // Init your App.
);
}
import 'package:sentry_logging/sentry_logging.dart';
import 'package:sentry/sentry.dart';
Future<void> main() async {
await Sentry.init(
(options) {
options.dsn = '___PUBLIC_DSN___';
options.addIntegration(LoggingIntegration());
},
appRunner: initApp, // Init your App.
);
}
| Parameter | Default | Description |
|---|---|---|
minBreadcrumbLevel | Level.INFO | Minimum level for creating breadcrumbs |
minEventLevel | Level.SEVERE | Minimum level for creating error events |
minSentryLogLevel | Level.INFO | Minimum level for sending structured logs |
You can customize which log levels trigger different Sentry features:
await Sentry.init(
(options) {
options.dsn = '___PUBLIC_DSN___';
options.addIntegration(LoggingIntegration(
minBreadcrumbLevel: Level.INFO, // Breadcrumbs for INFO and above
minEventLevel: Level.SEVERE, // Error events for SEVERE and above
minSentryLogLevel: Level.INFO, // Structured logs for INFO and above
));
},
appRunner: initApp,
);
await Sentry.init(
(options) {
options.dsn = '___PUBLIC_DSN___';
options.addIntegration(LoggingIntegration(
minBreadcrumbLevel: Level.INFO, // Breadcrumbs for INFO and above
minEventLevel: Level.SEVERE, // Error events for SEVERE and above
minSentryLogLevel: Level.INFO, // Structured logs for INFO and above
));
},
appRunner: initApp,
);
Add the following snippet to your app and execute it to verify that Sentry is capturing your logs:
import 'package:logging/logging.dart';
void testLogging() {
final log = Logger('MyAwesomeLogger');
// This creates a breadcrumb AND a structured log (Level.INFO >= defaults)
log.info('User logged in successfully');
// This creates a breadcrumb AND a structured log (Level.WARNING >= defaults)
log.warning('Rate limit approaching');
try {
throw StateError('Something went wrong');
} catch (error, stackTrace) {
// This creates a breadcrumb, structured log, AND error event (Level.SEVERE >= all defaults)
log.severe('Critical error occurred', error, stackTrace);
}
}
import 'package:logging/logging.dart';
void testLogging() {
final log = Logger('MyAwesomeLogger');
// This creates a breadcrumb AND a structured log (Level.INFO >= defaults)
log.info('User logged in successfully');
// This creates a breadcrumb AND a structured log (Level.WARNING >= defaults)
log.warning('Rate limit approaching');
try {
throw StateError('Something went wrong');
} catch (error, stackTrace) {
// This creates a breadcrumb, structured log, AND error event (Level.SEVERE >= all defaults)
log.severe('Critical error occurred', error, stackTrace);
}
}
- Breadcrumbs: The earlier
infoandwarningcalls appear on the error event. Theseverecall is added as a breadcrumb after its own event is captured. - Error Event: The
severelog creates a full error event with stack trace - Structured Logs: Navigate to Logs in your Sentry project to see all three entries as searchable structured logs
The Dart logging package only includes a stack trace in a LogRecord if one is explicitly provided. When no stack trace is available, Sentry falls back to calling StackTrace.current internally, which points to Sentry's own internals rather than the actual call site. This means error events may show inaccurate stack traces unless you take one of the following approaches.
Configure the logging package to automatically capture a stack trace at the call site for specific log levels:
import 'package:logging/logging.dart';
// Automatically captures stack traces for SEVERE and above
recordStackTraceAtLevel = Level.SEVERE;
final log = Logger('MyLogger');
// The logging framework will now call StackTrace.current at this call site
log.severe('Something went wrong');
import 'package:logging/logging.dart';
// Automatically captures stack traces for SEVERE and above
recordStackTraceAtLevel = Level.SEVERE;
final log = Logger('MyLogger');
// The logging framework will now call StackTrace.current at this call site
log.severe('Something went wrong');
When catching exceptions, pass both the error and the stack trace directly:
final log = Logger('MyLogger');
try {
// code that may throw
} catch (error, stackTrace) {
log.severe('Something went wrong', error, stackTrace);
}
final log = Logger('MyLogger');
try {
// code that may throw
} catch (error, stackTrace) {
log.severe('Something went wrong', error, stackTrace);
}
This gives Sentry the most accurate stack trace since it's captured at the point where the exception occurred.
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").