diff --git a/analysis_options.yaml b/analysis_options.yaml index f697e1d..1498fff 100644 --- a/analysis_options.yaml +++ b/analysis_options.yaml @@ -3,5 +3,4 @@ include: package:lints/recommended.yaml analyzer: language: strict-casts: true - strict-raw-types: true strict-inference: true diff --git a/examples/.gitignore b/examples/.gitignore new file mode 100644 index 0000000..397b4a7 --- /dev/null +++ b/examples/.gitignore @@ -0,0 +1 @@ +*.log diff --git a/examples/file_rotation_periodic_example.dart b/examples/file_rotation_periodic_example.dart index ed7ee62..2b922f0 100644 --- a/examples/file_rotation_periodic_example.dart +++ b/examples/file_rotation_periodic_example.dart @@ -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 main() async { log.set(logger); @@ -23,29 +25,40 @@ Future 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.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.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!'); } diff --git a/lib/src/handlers/file_handler/io.dart b/lib/src/handlers/file_handler/io.dart index 68e0383..e62cfa5 100644 --- a/lib/src/handlers/file_handler/io.dart +++ b/lib/src/handlers/file_handler/io.dart @@ -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) @@ -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) {