Skip to content

Commit acc93f9

Browse files
feat(dart/flutter): improve distinction between Logging integration and Structured Logs (#14527)
The logging integration refers to the integration with the Dart Logging package and it's pretty easy to confuse it with Structured Logs especially because if you google for Sentry Dart/Flutter logs the logging integration is the first page that shows up I tried to make it a bit more clearer but I'm unsure if my approach is good --------- Co-authored-by: Alex Krawiec <[email protected]>
1 parent d4c2043 commit acc93f9

File tree

4 files changed

+88
-24
lines changed

4 files changed

+88
-24
lines changed

docs/platforms/dart/common/logs/index.mdx

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,10 @@ With Sentry Structured Logs, you can send text based log information from your a
2121

2222
<PlatformContent includePath="logs/usage" />
2323

24+
## Integrations
25+
26+
<PlatformContent includePath="logs/integrations" />
27+
2428
## Options
2529

2630
<PlatformContent includePath="logs/options" />
Lines changed: 54 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
---
22
title: Logging Integration
3-
description: "Learn more about the Sentry Logging integration for the Dart SDK."
3+
description: "Integrate Sentry with the Dart Logging package to capture events, breadcrumbs, and automatically send structured logs to Sentry."
44
caseStyle: canonical
55
supportLevel: production
66
sidebar_order: 3
@@ -9,9 +9,19 @@ platforms:
99
- flutter
1010
---
1111

12-
The `sentry_logging` library provides [Logging](https://pub.dev/packages/logging) support for Sentry using the [onRecord property](https://pub.dev/documentation/logging/latest/logging/Logger/onRecord.html). It is able to collect breadcrumbs and capture events. Once this integration is configured, you can use Logging's public API exclusively or in combination to the Sentry's SDK API to capture and enrich events.
12+
This integration connects Sentry with the popular [Dart logging package](https://pub.dev/packages/logging), providing the following capabilities:
1313

14-
The source can be found [on GitHub](https://github.com/getsentry/sentry-dart/tree/main/logging/).
14+
- If `enableLogs` is set to `true`, Sentry will send your log messages as [Sentry Structured Logs](/platforms/dart/logs/) (new in `9.5.0`)
15+
- Captures breadcrumbs from your log calls
16+
- Converts error-level logs into Sentry error events
17+
- Works with your existing logging code
18+
19+
<Alert level="info">
20+
21+
This page covers the instrumentation of the **Dart Logging package**.
22+
This integration also supports creating structured logs. However, if you're looking to set up Sentry structured logs in general, visit our [Structured Logs](/platforms/dart/logs/) documentation.
23+
24+
</Alert>
1525

1626
## Install
1727

@@ -26,8 +36,7 @@ dependencies:
2636
2737
## Configure
2838
29-
Configuration should happen as early as possible in your application's lifecycle.
30-
39+
Add the `LoggingIntegration` to your `Sentry.init` call:
3140

3241
```dart
3342
import 'package:sentry_logging/sentry_logging.dart';
@@ -38,36 +47,64 @@ Future<void> main() async {
3847
(options) {
3948
options.dsn = '___PUBLIC_DSN___';
4049
options.addIntegration(LoggingIntegration());
50+
// If you want to enable sending structured logs, set `enableLogs` to `true`
51+
options.enableLogs = true;
4152
},
4253
appRunner: initApp, // Init your App.
4354
);
4455
}
4556
```
4657

58+
### Configuration Options
59+
60+
| Parameter | Default | Description |
61+
|-----------|---------|-------------|
62+
| `minBreadcrumbLevel` | `Level.INFO` | Minimum level for creating breadcrumbs |
63+
| `minEventLevel` | `Level.SEVERE` | Minimum level for creating error events |
64+
| `minSentryLogLevel` | `Level.INFO` | Minimum level for sending structured logs (requires `enableLogs` to be `true`) |
65+
66+
You can customize which log levels trigger different Sentry features:
67+
68+
```dart
69+
await Sentry.init(
70+
(options) {
71+
options.dsn = '___PUBLIC_DSN___';
72+
options.addIntegration(LoggingIntegration(
73+
minBreadcrumbLevel: Level.INFO, // Breadcrumbs for INFO and above
74+
minEventLevel: Level.SEVERE, // Error events for SEVERE and above
75+
minSentryLogLevel: Level.INFO, // Structured logs for INFO and above
76+
));
77+
},
78+
appRunner: initApp,
79+
);
80+
```
81+
4782
## Verify
4883

49-
This snippet captures an intentional error, so you can test that everything is working as soon as you set it up:
84+
Add the following snippet to your app and execute it to verify that Sentry is capturing your logs:
5085

5186
```dart
5287
import 'package:logging/logging.dart';
5388
54-
void main() async {
89+
void testLogging() {
5590
final log = Logger('MyAwesomeLogger');
5691
57-
log.info('a breadcrumb!');
92+
// This creates a breadcrumb AND a structured log (Level.INFO >= defaults)
93+
log.info('User logged in successfully');
94+
95+
// This creates a breadcrumb AND a structured log (Level.WARNING >= defaults)
96+
log.warning('Rate limit approaching');
5897
5998
try {
60-
throw StateError();
99+
throw StateError('Something went wrong');
61100
} catch (error, stackTrace) {
62-
log.severe('an error!', error, stackTrace);
101+
// This creates a breadcrumb, structured log, AND error event (Level.SEVERE >= all defaults)
102+
log.severe('Critical error occurred', error, stackTrace);
63103
}
64104
}
65105
```
66106

67-
<Alert>
68-
69-
Learn more about manually capturing an error or message, in our <PlatformLink to="/usage/">Usage documentation</PlatformLink>.
70-
71-
</Alert>
72-
73-
To view and resolve the recorded message, log into [sentry.io](https://sentry.io) and open your project. Clicking on the error's title will open a page where you can see detailed information and mark it as resolved.
107+
### What You'll See in Sentry:
108+
- **Breadcrumbs**: All three log calls will appear as breadcrumbs on the error event
109+
- **Error Event**: The `severe` log creates a full error event with stack trace
110+
- **Structured Logs**: (if `enableLogs` is `true`) Navigate to **Logs** in your Sentry project to see all three entries as searchable structured logs
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
Available integrations:
2+
- [Dart Logging package](/platforms/dart/integrations/logging/)
3+
4+
If there's an integration you would like to see, open a [new issue on GitHub](https://github.com/getsentry/sentry-dart/issues/new/choose).

platform-includes/logs/usage/dart.mdx

Lines changed: 26 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,33 @@ Once the feature is enabled on the SDK and the SDK is initialized, you can send
22

33
The `logger` namespace exposes six methods that you can use to log messages at different log levels: `trace`, `debug`, `info`, `warning`, `error`, and `fatal`.
44

5-
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.
5+
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.
6+
7+
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.
8+
9+
<Alert level="info">
10+
When using the `fmt` function, you must use the `%s` placeholder for each value you want to insert.
11+
</Alert>
612

713
```dart
8-
Sentry.logger.info("A simple log message");
9-
Sentry.logger.warn("This is a warning log with attributes.", attributes: {
10-
'attribute1': SentryLogAttribute.string('string'),
11-
'attribute2': SentryLogAttribute.int(1),
12-
'attribute3': SentryLogAttribute.double(1.0),
13-
'attribute4': SentryLogAttribute.bool(true),
14+
Sentry.logger.fmt.error('Uh oh, something broke, here is the error: %s', [
15+
errorMsg
16+
], attributes: {
17+
'additional_info': SentryLogAttribute.string('some info'),
1418
});
19+
Sentry.logger.fmt.info("%s added %s to cart.", [user.username, product.name]);
1520
```
21+
22+
You can also pass additional attributes directly to the logging functions, avoiding the need to use the `fmt` function.
23+
24+
```dart
25+
Sentry.logger.error('Uh oh, something broke, here is the error: $errorMsg',
26+
attributes: {
27+
'error': SentryLogAttribute.string(errorMsg),
28+
'some_info': SentryLogAttribute.string('some info'),
29+
});
30+
Sentry.logger.info('User ${user.username} added ${product.name} to cart.', attributes: {
31+
'user': SentryLogAttribute.string(user.username),
32+
'product': SentryLogAttribute.string(product.name),
33+
});
34+
```

0 commit comments

Comments
 (0)