From e1d86ea5fc68a2e15af0db634b3f421640274eb4 Mon Sep 17 00:00:00 2001 From: Martin Edlman Date: Mon, 15 Sep 2025 11:22:37 +0200 Subject: [PATCH 1/2] Add support for retrieving current and rotated log files in AdvancedFileOutput --- lib/src/outputs/advanced_file_output.dart | 24 +++++++++++++++++++ .../outputs/advanced_file_output_stub.dart | 10 ++++++++ 2 files changed, 34 insertions(+) diff --git a/lib/src/outputs/advanced_file_output.dart b/lib/src/outputs/advanced_file_output.dart index 4b8d525..485c920 100644 --- a/lib/src/outputs/advanced_file_output.dart +++ b/lib/src/outputs/advanced_file_output.dart @@ -275,4 +275,28 @@ class AdvancedFileOutput extends LogOutput { } await _closeSink(); } + + /// 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..523e46b 100644 --- a/lib/src/outputs/advanced_file_output_stub.dart +++ b/lib/src/outputs/advanced_file_output_stub.dart @@ -87,4 +87,14 @@ class AdvancedFileOutput extends LogOutput { void output(OutputEvent event) { 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."); + } } From 4a9bfaf42429fd204904a442bc8791cbce6de97a Mon Sep 17 00:00:00 2001 From: Martin Edlman Date: Mon, 15 Sep 2025 11:24:59 +0200 Subject: [PATCH 2/2] Add `clearLog` method to `AdvancedFileOutput`. This method clears the main log file and removes all rotated log files. --- lib/src/outputs/advanced_file_output.dart | 24 +++++++++++++++++++ .../outputs/advanced_file_output_stub.dart | 5 ++++ 2 files changed, 29 insertions(+) diff --git a/lib/src/outputs/advanced_file_output.dart b/lib/src/outputs/advanced_file_output.dart index 4b8d525..1820bb7 100644 --- a/lib/src/outputs/advanced_file_output.dart +++ b/lib/src/outputs/advanced_file_output.dart @@ -275,4 +275,28 @@ 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); + } + } + } } diff --git a/lib/src/outputs/advanced_file_output_stub.dart b/lib/src/outputs/advanced_file_output_stub.dart index dcfd10c..63defaf 100644 --- a/lib/src/outputs/advanced_file_output_stub.dart +++ b/lib/src/outputs/advanced_file_output_stub.dart @@ -87,4 +87,9 @@ 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."); + } }