Logging
Logging
备忘 You can view logs in DevTools’ Logging view or in your system console. This sections shows how to set up your logging statements.
You have two options for logging for your application.The first is to use stdout and stderr.Generally, this is done using print() statements,or by importing dart:io and invoking methods onstderr and stdout. For example:
stderr.writeln('print me');
If you output too much at once, then Android sometimesdiscards some log lines. To avoid this, use debugPrint(),from Flutter’s foundation library. This is a wrapper around printthat throttles the output to a level that avoids being dropped byAndroid’s kernel.
The other option for application logging is to use thedart:developer log() function. This allows you to include abit more granularity and information in the logging output.Here’s an example:
import 'dart:developer' as developer;
void main() {
developer.log('log me', name: 'my.app.category');
developer.log('log me 1', name: 'my.other.category');
developer.log('log me 2', name: 'my.other.category');
}
You can also pass application data to the log call.The convention for this is to use the error: namedparameter on the log() call, JSON encode the objectyou want to send, and pass the encoded string to theerror parameter.
import 'dart:convert';
import 'dart:developer' as developer;
void main() {
var myCustomObject = ...;
developer.log(
'log me',
name: 'my.app.category',
error: jsonEncode(myCustomObject),
);
}
If viewing the logging output in DevTool’s logging view,the JSON encoded error param is interpreted as a data objectand rendered in the details view for that log entry.
