Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 47 additions & 0 deletions lib/src/outputs/advanced_file_output.dart
Original file line number Diff line number Diff line change
Expand Up @@ -275,4 +275,51 @@ class AdvancedFileOutput extends LogOutput {
}
await _closeSink();
}

/// Clears the log file. Removes all rotated log files as well.
Future<void> clearLog() async {
// immediate flush, delete and recreate the file
_flushBuffer();
await _closeSink();
await _file.delete();
await _openSink();
// remove all rotated files
final files = _file.parent
.listSync()
.whereType<File>()
// Filter out the latest file
.where((f) => f.path != _file.path)
.toList();
for (final file in files) {
try {
await file.delete();
} catch (e, s) {
print('Failed to delete file: $e');
print(s);
}
}
}

/// Returns the current log file.
File getFile() {
return _file;
}

/// Returns list of log files (latest and rotated) up to the limit.
List<File> getFiles() {
// only the latest file
if (_maxRotatedFilesCount == null) return [_file];

final files = _file.parent
.listSync()
.whereType<File>()
.toList();

// If the number of files is less than the limit, return all files
if (files.length <= _maxRotatedFilesCount!) return files;

// return only the latest files (not to be deleted)
files.sort(_fileSorter);
return files.sublist(files.length - _maxRotatedFilesCount!);
}
}
15 changes: 15 additions & 0 deletions lib/src/outputs/advanced_file_output_stub.dart
Original file line number Diff line number Diff line change
Expand Up @@ -87,4 +87,19 @@ class AdvancedFileOutput extends LogOutput {
void output(OutputEvent event) {
throw UnsupportedError("Not supported on this platform.");
}

/// Clears the log file. Removes all rotated log files as well.
Future<void> clearLog() async {
throw UnsupportedError("Not supported on this platform.");
}

/// Returns the current log file.
File getFile() {
throw UnsupportedError("Not supported on this platform.");
}

/// Returns list of log files (latest and rotated) up to the limit.
List<File> getFiles() {
throw UnsupportedError("Not supported on this platform.");
}
}
Loading