addNetworkLog method
- NetworkLog log
Writes a NetworkLog to the persistent log file.
The log is written to a file named network_logs.jsonl within the
application support directory. The log is written in JSON Lines format.
The log is written asynchronously in a separate isolate to avoid blocking the main isolate.
The log is written as a single JSON object, with the following keys:
timestamp: The timestamp of the log event, in ISO 8601 format.tag: The tag of the log event, a short string identifying the source of the log.method: The HTTP method used in the network request, such as "GET" or "POST".url: The URL of the network request.requestHeaders: A map of the request headers.requestBody: The body of the network request, if any.statusCode: The HTTP status code of the response.responseHeaders: A map of the response headers.responseBody: The body of the response, if any.isSuccess: A boolean indicating if the network request was successful.
Implementation
Future<void> addNetworkLog(NetworkLog log) async {
final tag = '${log.method} ${log.url}';
final msg =
'Status: ${log.statusCode} | ${log.isSuccess ? 'Success' : 'Error'}';
_printStyled(log.isSuccess ? 'info' : 'error', tag, msg);
final dir = await getApplicationSupportDirectory();
final path = dir.path;
final payload = {
'path': path,
'type': 'network',
...log.toJson(),
};
await Isolate.run(() => IsolateLogWriter.writeLog(payload));
}