Skip to content

Commit

Permalink
fix: gc only files matching pattern
Browse files Browse the repository at this point in the history
  • Loading branch information
roman-vanesyan committed Nov 3, 2024
1 parent 4c05297 commit f298e1c
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 18 deletions.
1 change: 0 additions & 1 deletion analysis_options.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,4 @@ include: package:lints/recommended.yaml
analyzer:
language:
strict-casts: true
strict-raw-types: true
strict-inference: true
1 change: 1 addition & 0 deletions examples/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
*.log
39 changes: 26 additions & 13 deletions examples/file_rotation_periodic_example.dart
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,12 @@ import 'package:strlog/handlers.dart' show FileHandler, LogFileRotationPolicy;

final formatter = TextFormatter.withDefaults();
final handler = FileHandler(
LogFileRotationPolicy.periodic(Duration(seconds: 30)), // each 30seconds
LogFileRotationPolicy.periodic(Duration(seconds: 30)), // each 30 seconds
path: './file_rotation_periodic_example.log',
formatter: formatter.call);
final logger = Logger.detached()..handler = handler;
final logger = Logger.detached()
..handler = handler
..level = Level.all;

Future<void> main() async {
log.set(logger);
Expand All @@ -23,29 +25,40 @@ Future<void> main() async {
])
});

final timer = context.startTimer('Uploading!', level: Level.info);
final uploadingTimer = context.startTimer('Uploading!', level: Level.info);

// Emulate exponential backoff retry
final maxAttempts = 5;
for (int attempt = 0; attempt < maxAttempts; attempt++) {
for (int attempt = 1; attempt <= maxAttempts; attempt++) {
try {
await Future<void>.delayed(Duration(seconds: 10 * (attempt + 1)));
final attemptTimer = context.withFields({
Int('attempt', attempt),
}).startTimer('Uploading attempt', level: Level.debug);

final waitFor = Duration(seconds: (2.5 * attempt).toInt());
await Future<void>.delayed(waitFor);

// resolve on the last attempt.
if (attempt != maxAttempts - 1) {
context
.error('Failed to upload, retrying...', {Int('attempt', attempt)});
if (attempt != maxAttempts) {
attemptTimer.stop('Failed attempt');
context.warn('Failed to upload, retrying...',
{Int('attempt', attempt), Dur('waited_for', waitFor)});

throw Exception('failed');
}

attemptTimer.stop('Succeeded');

break; // Success, exit loop
} catch (e) {
timer.stop('Aborting...');
context.fatal('Failed to upload!', {const Str('reason', 'Timeout')});
if (attempt == maxAttempts) {
uploadingTimer.stop('Aborting...');
context.error('Failed to upload!', {const Str('reason', 'Timeout')});

if (attempt == maxAttempts - 1) rethrow; // Last attempt failed
rethrow; // Last attempt failed
}
}

timer.stop('Uploaded!');
}

uploadingTimer.stop('Uploaded!');
}
10 changes: 6 additions & 4 deletions lib/src/handlers/file_handler/io.dart
Original file line number Diff line number Diff line change
Expand Up @@ -102,9 +102,11 @@ class FileHandler extends Handler {
/// [_gc] performs garbage collection on old backup files.
void _gc() {
final dir = _fs.directory(_path._dirname);
final files = List.of(dir
.listSync(followLinks: false)
.where((file) => file.statSync().type == FileSystemEntityType.file));
final files = List.of(dir.listSync(followLinks: false).where((file) =>
file.statSync().type == FileSystemEntityType.file &&
// Only delete files with matching basename and log extension
file.basename.contains(_path._basename) &&
extension(file.basename) == _path._extension));

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

final baseName = basenameWithoutExtension(normalizedPath);
final tagIndex = baseName.lastIndexOf('_');
final tagIndex = baseName.lastIndexOf('.');
String tag = '';
late final String finalBaseName;
if (tagIndex != -1) {
Expand Down

0 comments on commit f298e1c

Please sign in to comment.