|
1 | | -// ignore_for_file: unused_local_variable |
2 | | - |
3 | 1 | import 'package:df_log/df_log.dart'; |
4 | 2 |
|
5 | 3 | void main() { |
6 | | - // Configure [df_log](https://pub.dev/packages/df_log) to keep the last 50 |
7 | | - // log events in memory. |
8 | | - Log.maxStoredLogs = 50; |
| 4 | + // Let's say we only want to print logs with #ui and #auth tags. |
| 5 | + Log.activeTags = {#ui, #auth}; |
9 | 6 |
|
10 | | - // Set up a callback for crash reporting. |
11 | | - Log.addCallback((logItem) { |
12 | | - // We only care about logs that are tagged as errors. |
13 | | - if (logItem.tags.contains(#error)) { |
14 | | - // Get the history of recent events. |
15 | | - final history = Log.items.map((item) => item.toMap()).toList(); |
| 7 | + // ✅ Printed! #auth tag got added! |
| 8 | + Log.ok('User logged in.', {#auth}); |
16 | 9 |
|
17 | | - // Compile the payload to send to your crash reporter. |
18 | | - final payload = { |
19 | | - 'exception': logItem.message, |
20 | | - 'extra': {'breadcrumbs': history}, |
21 | | - }; |
| 10 | + // ❌ NOT Printed! #network tag is not added! |
| 11 | + Log.trace('Fetching network info (1)...', {#network}); |
22 | 12 |
|
23 | | - // ---> ---> Send the payload to your crash reporter here! <--- <--- |
24 | | - } |
25 | | - }); |
| 13 | + // Start printing Logs tagged with #network from here on. |
| 14 | + Log.addTags({#network}); |
26 | 15 |
|
27 | | - // runApp(MyApp()); |
| 16 | + // ✅ Printed! #network tag got added! |
| 17 | + Log.trace('Fetching network info (2)...', {#network}); |
28 | 18 |
|
29 | | - // Somewhere else in your code... |
30 | | - updateUserProfile(); |
31 | | -} |
| 19 | + // Stop printing Logs tagged with #network from here on. |
| 20 | + Log.removeTags({#network}); |
32 | 21 |
|
33 | | -// Somewhere else in your code... |
34 | | -void updateUserProfile() { |
35 | | - Log.info('Navigated to profile screen.', {#ui, #profile}); |
36 | | - try { |
37 | | - // Do stuff... |
38 | | - throw Exception('Connection timed out'); |
39 | | - } catch (e) { |
40 | | - // This single line now does two things: |
41 | | - // 1. Prints the error to the console for you. |
42 | | - // 2. Triggers the callback to send a crash report WITH breadcrumbs. |
43 | | - Log.err('Failed to update profile: $e', {#profile, #network}); |
44 | | - } |
| 22 | + // ❌ NOT Printed! #network tag got removed! |
| 23 | + Log.trace('Fetching network info (3)...', {#network}); |
45 | 24 | } |
0 commit comments