Skip to content

Commit f298e1c

Browse files
fix: gc only files matching pattern
1 parent 4c05297 commit f298e1c

File tree

4 files changed

+33
-18
lines changed

4 files changed

+33
-18
lines changed

analysis_options.yaml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,4 @@ include: package:lints/recommended.yaml
33
analyzer:
44
language:
55
strict-casts: true
6-
strict-raw-types: true
76
strict-inference: true

examples/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
*.log

examples/file_rotation_periodic_example.dart

Lines changed: 26 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,12 @@ import 'package:strlog/handlers.dart' show FileHandler, LogFileRotationPolicy;
77

88
final formatter = TextFormatter.withDefaults();
99
final handler = FileHandler(
10-
LogFileRotationPolicy.periodic(Duration(seconds: 30)), // each 30seconds
10+
LogFileRotationPolicy.periodic(Duration(seconds: 30)), // each 30 seconds
1111
path: './file_rotation_periodic_example.log',
1212
formatter: formatter.call);
13-
final logger = Logger.detached()..handler = handler;
13+
final logger = Logger.detached()
14+
..handler = handler
15+
..level = Level.all;
1416

1517
Future<void> main() async {
1618
log.set(logger);
@@ -23,29 +25,40 @@ Future<void> main() async {
2325
])
2426
});
2527

26-
final timer = context.startTimer('Uploading!', level: Level.info);
28+
final uploadingTimer = context.startTimer('Uploading!', level: Level.info);
2729

2830
// Emulate exponential backoff retry
2931
final maxAttempts = 5;
30-
for (int attempt = 0; attempt < maxAttempts; attempt++) {
32+
for (int attempt = 1; attempt <= maxAttempts; attempt++) {
3133
try {
32-
await Future<void>.delayed(Duration(seconds: 10 * (attempt + 1)));
34+
final attemptTimer = context.withFields({
35+
Int('attempt', attempt),
36+
}).startTimer('Uploading attempt', level: Level.debug);
37+
38+
final waitFor = Duration(seconds: (2.5 * attempt).toInt());
39+
await Future<void>.delayed(waitFor);
3340

3441
// resolve on the last attempt.
35-
if (attempt != maxAttempts - 1) {
36-
context
37-
.error('Failed to upload, retrying...', {Int('attempt', attempt)});
42+
if (attempt != maxAttempts) {
43+
attemptTimer.stop('Failed attempt');
44+
context.warn('Failed to upload, retrying...',
45+
{Int('attempt', attempt), Dur('waited_for', waitFor)});
46+
3847
throw Exception('failed');
3948
}
4049

50+
attemptTimer.stop('Succeeded');
51+
4152
break; // Success, exit loop
4253
} catch (e) {
43-
timer.stop('Aborting...');
44-
context.fatal('Failed to upload!', {const Str('reason', 'Timeout')});
54+
if (attempt == maxAttempts) {
55+
uploadingTimer.stop('Aborting...');
56+
context.error('Failed to upload!', {const Str('reason', 'Timeout')});
4557

46-
if (attempt == maxAttempts - 1) rethrow; // Last attempt failed
58+
rethrow; // Last attempt failed
59+
}
4760
}
48-
49-
timer.stop('Uploaded!');
5061
}
62+
63+
uploadingTimer.stop('Uploaded!');
5164
}

lib/src/handlers/file_handler/io.dart

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -102,9 +102,11 @@ class FileHandler extends Handler {
102102
/// [_gc] performs garbage collection on old backup files.
103103
void _gc() {
104104
final dir = _fs.directory(_path._dirname);
105-
final files = List.of(dir
106-
.listSync(followLinks: false)
107-
.where((file) => file.statSync().type == FileSystemEntityType.file));
105+
final files = List.of(dir.listSync(followLinks: false).where((file) =>
106+
file.statSync().type == FileSystemEntityType.file &&
107+
// Only delete files with matching basename and log extension
108+
file.basename.contains(_path._basename) &&
109+
extension(file.basename) == _path._extension));
108110

109111
if (files.length > _maxBackupsCount) {
110112
files.sort((a, b) => _ParsedPath.fromString(a.path)
@@ -187,7 +189,7 @@ class _ParsedPath implements Comparable<_ParsedPath> {
187189
final normalizedPath = normalize(path);
188190

189191
final baseName = basenameWithoutExtension(normalizedPath);
190-
final tagIndex = baseName.lastIndexOf('_');
192+
final tagIndex = baseName.lastIndexOf('.');
191193
String tag = '';
192194
late final String finalBaseName;
193195
if (tagIndex != -1) {

0 commit comments

Comments
 (0)