Skip to content

Commit 69f4a4a

Browse files
committed
AdvancedFileOutput: Added fileHeader and fileFooter options
1 parent 5412f5a commit 69f4a4a

File tree

3 files changed

+51
-0
lines changed

3 files changed

+51
-0
lines changed

lib/src/outputs/advanced_file_output.dart

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,10 +57,17 @@ class AdvancedFileOutput extends LogOutput {
5757
/// The [latestFileName] will not be counted. The default [fileSorter] strategy is
5858
/// sorting by last modified date, beware that could be not reliable in some
5959
/// platforms and/or filesystems.
60+
///
61+
/// [fileHeader] and [fileFooter] can be used to respectively add a header
62+
/// or footer to the file when opening/closing the file sink.
63+
/// (Please note that this happens not only when files are rotated but also
64+
/// on every start and shutdown of your application!)
6065
AdvancedFileOutput({
6166
required String path,
6267
bool overrideExisting = false,
6368
Encoding encoding = utf8,
69+
this.fileHeader,
70+
this.fileFooter,
6471
List<Level>? writeImmediately,
6572
Duration maxDelay = const Duration(seconds: 2),
6673
int maxBufferSize = 2000,
@@ -96,6 +103,9 @@ class AdvancedFileOutput extends LogOutput {
96103
final bool _overrideExisting;
97104
final Encoding _encoding;
98105

106+
String? fileHeader;
107+
String? fileFooter;
108+
99109
final List<Level> _writeImmediately;
100110
final Duration _maxDelay;
101111
final int _maxFileSizeKB;
@@ -200,9 +210,17 @@ class AdvancedFileOutput extends LogOutput {
200210
mode: _overrideExisting ? FileMode.writeOnly : FileMode.writeOnlyAppend,
201211
encoding: _encoding,
202212
);
213+
214+
if (fileHeader != null) {
215+
_sink?.writeln(fileHeader);
216+
}
203217
}
204218

205219
Future<void> _closeSink() async {
220+
if (fileFooter != null) {
221+
_sink?.writeln(fileFooter);
222+
}
223+
206224
await _sink?.flush();
207225
await _sink?.close();
208226
_sink = null; // Explicitly set null until assigned again

lib/src/outputs/advanced_file_output_stub.dart

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,10 +52,17 @@ class AdvancedFileOutput extends LogOutput {
5252
/// The [latestFileName] will not be counted. The default [fileSorter] strategy is
5353
/// sorting by last modified date, beware that could be not reliable in some
5454
/// platforms and/or filesystems.
55+
///
56+
/// [fileHeader] and [fileFooter] can be used to respectively add a header
57+
/// or footer to the file when opening/closing the file sink.
58+
/// (Please note that this happens not only when files are rotated but also
59+
/// on every start and shutdown of your application!)
5560
AdvancedFileOutput({
5661
required String path,
5762
bool overrideExisting = false,
5863
Encoding encoding = utf8,
64+
String? fileHeader,
65+
String? fileFooter,
5966
List<Level>? writeImmediately,
6067
Duration maxDelay = const Duration(seconds: 2),
6168
int maxBufferSize = 2000,

test/outputs/advanced_file_output_test.dart

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,32 @@ void main() {
154154
expect(await rotatedFile.readAsString(), contains("2"));
155155
});
156156

157+
test('Rolling files with header/footer', () async {
158+
const fileHeader = "TEST-HEADER";
159+
const fileFooter = "TEST-FOOTER";
160+
161+
var output = AdvancedFileOutput(
162+
path: dir.path,
163+
maxFileSizeKB: 1,
164+
maxRotatedFilesCount: 1,
165+
fileHeader: fileHeader,
166+
fileFooter: fileFooter,
167+
);
168+
169+
await output.init();
170+
final event0 =
171+
OutputEvent(LogEvent(Level.fatal, ""), ["Header and Footer Test"]);
172+
output.output(event0);
173+
await output.destroy();
174+
175+
// Give the OS a chance to flush to the file system (should reduce flakiness)
176+
await Future.delayed(const Duration(milliseconds: 1000));
177+
178+
final latestFile = File('${dir.path}/latest.log');
179+
expect(await latestFile.readAsString(), startsWith(fileHeader));
180+
expect(await latestFile.readAsString(), endsWith("$fileFooter\n"));
181+
});
182+
157183
test('Rolling files with custom file sorter', () async {
158184
var output = AdvancedFileOutput(
159185
path: dir.path,

0 commit comments

Comments
 (0)