purgeOldLogs static method

Future<void> purgeOldLogs(
  1. String dirPath,
  2. int daysToKeep
)

Deletes log files older than the specified number of days.

dirPath - The directory path where the log files are stored. daysToKeep - The number of days to retain log files. Files older than this will be purged.

This function iterates through the files in the specified directory, checks their modification date, and deletes those that are older than the specified number of days. Only files with a .jsonl extension are considered.

Implementation

// remove path/type from actual log
static Future<void> purgeOldLogs(String dirPath, int daysToKeep) async {
  final dir = Directory(dirPath);
  final now = DateTime.now();

  final files = dir.listSync();
  for (final file in files) {
    if (file is File && file.path.endsWith('.jsonl')) {
      final stat = await file.stat();
      final diff = now.difference(stat.modified);
      if (diff.inDays > daysToKeep) {
        await file.delete();
      }
    }
  }
}