Flutter Logging
Contents
1. Install package logging
Add logging
to pubspec.yaml
file:
1 | dependencies: |
2. Config Logger
Initialize Logger
before runApp()
in main.dart
file:
1 | import 'logging.dart'; |
1 | import 'package:logging/logging.dart'; |
3. Transmit log records from Logger to dart:developer
Logger is a producer
, but it will not post any log records if no one is listening.
Print to console:
1 | Logger.root.onRecord.listen((event) { |
Save to file:
1 | File logFile = ... |
It’s strongly recommended to use dart:developer
for logging:
1 | import 'dart:developer' as developer; |
4. Example
Logging a debug message on track()
:
1 | import 'package:logging/logging.dart'; |
If using dart:developer
for consuming log records, please check the following.
Flutter console displays logs:
DevTools’ Logging view shows logs:
Complete code
See gist: https://gist.github.com/yrom/1a86a6995720d9a4ff9261617fb13783
Redirect ‘print’
1 | void main() { |
References
- Using the Logging view: https://flutter.dev/docs/development/tools/devtools/logging
- Debugging Flutter apps programmatically: https://flutter.dev/docs/testing/code-debugging#logging
- logging pacakge: https://pub.dev/packages/logging
Author: Yrom
Link: https://yrom.net/blog/2020/07/08/flutter-logging/
License: 知识共享署名-非商业性使用 4.0 国际许可协议