Skip to content

Commit 4c05297

Browse files
fix: fix analyzer
1 parent 4591634 commit 4c05297

File tree

11 files changed

+85
-70
lines changed

11 files changed

+85
-70
lines changed

examples/file_example.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import 'package:strlog/handlers.dart' show FileHandler, LogFileRotationPolicy;
77

88
final formatter = TextFormatter.withDefaults();
99
final handler = FileHandler(LogFileRotationPolicy.never(),
10-
path: './file_example.log', maxBackupsCount: 1, formatter: formatter.call);
10+
path: './file_example.log', formatter: formatter.call);
1111

1212
final logger = Logger.detached()..handler = handler;
1313

examples/file_rotation_example.dart

Lines changed: 0 additions & 34 deletions
This file was deleted.
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
library;
2+
3+
import 'package:strlog/formatters.dart' show TextFormatter;
4+
import 'package:strlog/global_logger.dart' as log;
5+
import 'package:strlog/strlog.dart';
6+
import 'package:strlog/handlers.dart' show FileHandler, LogFileRotationPolicy;
7+
8+
final formatter = TextFormatter.withDefaults();
9+
final handler = FileHandler(
10+
LogFileRotationPolicy.periodic(Duration(seconds: 30)), // each 30seconds
11+
path: './file_rotation_periodic_example.log',
12+
formatter: formatter.call);
13+
final logger = Logger.detached()..handler = handler;
14+
15+
Future<void> main() async {
16+
log.set(logger);
17+
18+
final context = log.withFields({
19+
const Str('username', 'roman-vanesyan'),
20+
const Group('file', [
21+
Str('name', 'avatar.png'),
22+
Str('mime', 'image/png'),
23+
])
24+
});
25+
26+
final timer = context.startTimer('Uploading!', level: Level.info);
27+
28+
// Emulate exponential backoff retry
29+
final maxAttempts = 5;
30+
for (int attempt = 0; attempt < maxAttempts; attempt++) {
31+
try {
32+
await Future<void>.delayed(Duration(seconds: 10 * (attempt + 1)));
33+
34+
// resolve on the last attempt.
35+
if (attempt != maxAttempts - 1) {
36+
context
37+
.error('Failed to upload, retrying...', {Int('attempt', attempt)});
38+
throw Exception('failed');
39+
}
40+
41+
break; // Success, exit loop
42+
} catch (e) {
43+
timer.stop('Aborting...');
44+
context.fatal('Failed to upload!', {const Str('reason', 'Timeout')});
45+
46+
if (attempt == maxAttempts - 1) rethrow; // Last attempt failed
47+
}
48+
49+
timer.stop('Uploaded!');
50+
}
51+
}

examples/file_rotation_periodic_sized_example.dart

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

88
final formatter = TextFormatter.withDefaults();
99
final handler = FileHandler(
10+
// rotates log file if either log file is older than 5m, or size exceeds 10kb.
1011
LogFileRotationPolicy.union([
11-
LogFileRotationPolicy.periodic(Duration(minutes: 5)), // each 5 minutes
12-
LogFileRotationPolicy.sized(1024 * 1024) // if exceeds 1mb.
12+
LogFileRotationPolicy.periodic(Duration(minutes: 5)), // older than 5m
13+
LogFileRotationPolicy.sized(1024 * 10) // exceeds 10kb.
1314
]),
14-
formatter: formatter.call);
15+
formatter: formatter.call,
16+
path: './file_rotation_periodic_example.log');
1517

1618
final logger = Logger.detached()..handler = handler;
1719

file_example.log

Lines changed: 0 additions & 9 deletions
This file was deleted.

lib/src/fields/any.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,5 +57,5 @@ class _Any {
5757
///
5858
/// As [Any] makes runtime type checking make sure that it is used only
5959
/// where it is necessary, otherwise use one of predefined fields.
60-
// ignore:constant_identifier_names
60+
// ignore:library_private_types_in_public_api,constant_identifier_names
6161
const _Any Any = _Any();

lib/src/formatters/formatter.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import 'package:strlog/strlog.dart' show Record, Level, Field, FieldKind;
22

33
typedef LevelFormatter<R> = R Function(Level);
44
typedef TimestampFormatter<R> = R Function(DateTime);
5-
typedef FieldFormatter<R> = R Function(Field<dynamic>);
5+
typedef FieldFormatter<R> = R Function(Field);
66

77
/// [Formatter] is capable to format single [Record] entry.
88
typedef Formatter = List<int> Function(Record);
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
/// Maximum number of backup files to keep at a time.
2+
const int maxBackupCount = 3;

lib/src/handlers/file_handler/io.dart

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,15 @@ import 'package:path/path.dart'
88
import 'package:strlog/formatters.dart' show Formatter;
99
import 'package:strlog/strlog.dart' show Handler, Record, Filter;
1010
import 'package:strlog/src/handlers/file_handler/policy.dart';
11+
import 'package:strlog/src/handlers/file_handler/defaults.dart' as defaults;
1112

1213
const _localFs = LocalFileSystem();
1314

1415
class _LogFileStat implements LogFileStat {
15-
_LogFileStat({required this.modified, required this.size});
16+
_LogFileStat({required this.firstChanged, required this.size});
1617

1718
@override
18-
DateTime modified;
19+
DateTime firstChanged;
1920

2021
@override
2122
int size;
@@ -30,15 +31,16 @@ class _LogFileStat implements LogFileStat {
3031
/// and can maintain a maximum number of backup files. When rotation occurs, the current
3132
/// log file is archived with a timestamp and a new file is created.
3233
class FileHandler extends Handler {
33-
FileHandler(this._policy,
34-
{required Formatter formatter,
35-
required String path,
36-
required int maxBackupsCount,
37-
FileSystem? fs})
38-
: assert(path != ""),
34+
FileHandler(
35+
this._policy, {
36+
required Formatter formatter,
37+
required String path,
38+
FileSystem? fs,
39+
int? maxBackupsCount,
40+
}) : assert(path != ""),
3941
_formatter = formatter,
4042
_fs = fs ?? _localFs,
41-
_maxBackupsCount = maxBackupsCount,
43+
_maxBackupsCount = maxBackupsCount ?? defaults.maxBackupCount,
4244
_isClosed = false,
4345
_path = _ParsedPath.fromString(path) {
4446
_open();
@@ -52,10 +54,10 @@ class FileHandler extends Handler {
5254
bool _isClosed;
5355

5456
final FileSystem _fs;
57+
final _ParsedPath _path;
5558
late File _file; // created in _open
5659
late IOSink _sink; // created in _open
5760
late _LogFileStat _stat; // created in _open
58-
final _ParsedPath _path;
5961

6062
/// Sets records filter.
6163
///
@@ -76,7 +78,7 @@ class FileHandler extends Handler {
7678
case FileSystemEntityType.file:
7779
{
7880
final nextStat =
79-
_LogFileStat(size: stat.size, modified: stat.modified);
81+
_LogFileStat(size: stat.size, firstChanged: stat.changed);
8082
if (_policy.shouldRotate(nextStat)) {
8183
_rotate();
8284
} else {
@@ -87,7 +89,7 @@ class FileHandler extends Handler {
8789

8890
case FileSystemEntityType.notFound:
8991
_file.createSync(recursive: true);
90-
_stat = _LogFileStat(size: 0, modified: DateTime.now());
92+
_stat = _LogFileStat(size: 0, firstChanged: DateTime.now());
9193
break;
9294

9395
default:
@@ -141,7 +143,6 @@ class FileHandler extends Handler {
141143

142144
_sink.add(bytes);
143145
_stat.size += bytes.lengthInBytes;
144-
_stat.modified = DateTime.now();
145146
}
146147

147148
/// Closes the file handler and underlying file sink.

lib/src/handlers/file_handler/noop.dart

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,13 @@ import 'package:strlog/strlog.dart' show Handler, Record;
55
import './policy.dart';
66

77
class FileHandler extends Handler {
8-
FileHandler(LogFileRotationPolicy policy,
9-
{required String path,
10-
required int maxBackupsCount,
11-
required Formatter formatter,
12-
FileSystem? fs});
8+
FileHandler(
9+
LogFileRotationPolicy policy, {
10+
required String path,
11+
required Formatter formatter,
12+
FileSystem? fs,
13+
int? maxBackupsCount,
14+
});
1315

1416
@override
1517
void handle(Record record) {

0 commit comments

Comments
 (0)