flush method

Future<void> flush()

Writes all pending logs to the NDJSON file.

Implementation

Future<void> flush() async {
  _flushTimer?.cancel();
  _flushTimer = null;
  if (_pendingWrites.isEmpty) return;
  final batch = List<HttpRequestLog>.from(_pendingWrites);
  _pendingWrites.clear();
  try {
    if (!await _dir.exists()) {
      await _dir.create(recursive: true);
    }
    final buffer = StringBuffer();
    for (final log in batch) {
      buffer.writeln(json.encode(log.toJson()));
    }
    await _file.writeAsString(
      buffer.toString(),
      mode: FileMode.append,
      flush: true,
    );
    await _trimIfNeeded();
  } catch (_) {
    // Silently ignore write errors to avoid impacting app performance.
  }
}