Sub topic 5
Logging with `dart:developer` and the Logging view
Assume you know basic Dart syntax and async programming, can build Flutter screens, and can run an app on an emulator or device. This chapter uses those skills to trace application behavior without stopping the app.
Logging means writing diagnostic messages while a program runs. Flutter DevTools collects these messages in its Logging view. A useful log tells you what happened, where it happened, and enough context to understand the state of the app.
Add structured logs
Dart's dart:developer library provides the log function. A structured log is a message with fields such as a name, severity level, error, and stack trace.
The name field identifies the part of the app that produced the message. Use stable names such as cart, auth, or checkout. This makes filtering easier than searching through unrelated text.
import 'dart:developer' as developer;
import 'package:flutter/material.dart';
void main() {
runApp(const CartApp());
}
class CartApp extends StatelessWidget {
const CartApp({super.key});
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(title: const Text('Cart logging')),
body: Center(
child: ElevatedButton(
onPressed: () async {
await loadCart(['Book', 'Pen']);
},
child: const Text('Load cart'),
),
),
),
);
}
}
Future<void> loadCart(List<String> items) async {
developer.log(
'Loaded ${items.length} items',
name: 'cart',
level: 800,
);
}The level is an integer severity value. Dart does not force a fixed set of names for these values. A common convention is to use values similar to standard logging systems:
| Level | Typical meaning |
|---|---|
| 500 | Detailed debugging information |
| 800 | Normal progress information |
| 900 | A possible problem |
| 1000 | An error |
Keep messages short and include useful values. Avoid passwords, access tokens, personal data, and complete authorization headers.
Open the Logging view
Run the app in a mode that supports DevTools, then connect DevTools to the Dart VM service for that app.
flutter run
dart devtoolsThe terminal output from flutter run includes connection information. Open DevTools with that connection, then select Logging. Press Load cart in the example app. A message with the cart name should appear.
The Logging view can contain framework messages, application logs, and output from other sources. Filter by the logger name or by text to reduce the list. A stable name is especially helpful when several screens produce similar messages.
Log errors with context
When an operation fails, pass the error and stack trace separately. The Logging view can then display the failure details without forcing you to put everything into one message.
import 'dart:developer' as developer;
Future<String> fetchProfile() async {
try {
throw StateError('Profile service unavailable');
} catch (error, stackTrace) {
developer.log(
'Profile request failed',
name: 'profile',
level: 1000,
error: error,
stackTrace: stackTrace,
);
rethrow;
}
}Use developer.log for events that help explain behavior. Do not log every line of a function. Excessive logs make the important event harder to find and can affect performance. Add temporary detailed logs while investigating, then remove or reduce them after confirming the fix.
A log shows that a path was reached, but it does not prove that the result is correct. Record important inputs, state transitions, and outcomes. For example, log when a request starts, whether it succeeds, and how many records it returns. Avoid logging sensitive values.
Exercise
Exercise
In the cart example, how would you make it easier to find failed cart requests without logging the user's cart contents?
Show answer
Give the messages a stable name such as cart, use a high severity level for failures, and pass the error and stack trace through error and stackTrace. Log only safe details such as the item count or request identifier.