diff --git a/lib/src/outputs/advanced_file_output.dart b/lib/src/outputs/advanced_file_output.dart index 4b8d525..a7473e7 100644 --- a/lib/src/outputs/advanced_file_output.dart +++ b/lib/src/outputs/advanced_file_output.dart @@ -275,4 +275,51 @@ class AdvancedFileOutput extends LogOutput { } await _closeSink(); } + + /// Clears the log file. Removes all rotated log files as well. + Future 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() + // 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 getFiles() { + // only the latest file + if (_maxRotatedFilesCount == null) return [_file]; + + final files = _file.parent + .listSync() + .whereType() + .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!); + } } diff --git a/lib/src/outputs/advanced_file_output_stub.dart b/lib/src/outputs/advanced_file_output_stub.dart index dcfd10c..fe9bbc7 100644 --- a/lib/src/outputs/advanced_file_output_stub.dart +++ b/lib/src/outputs/advanced_file_output_stub.dart @@ -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 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 getFiles() { + throw UnsupportedError("Not supported on this platform."); + } }