-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathFileAppender.dart
193 lines (179 loc) · 4.98 KB
/
FileAppender.dart
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
import 'dart:io';
import 'package:basic_utils/basic_utils.dart';
import 'package:intl/intl.dart';
import 'package:log_4_dart_2/src/LogRecordFormatter.dart';
import '../../log_4_dart_2.dart';
import '../Utils.dart';
///
/// A appender for writing logs to a logfile
///
class FileAppender extends Appender {
/// The logfile pattern
String? filePattern;
/// The logfile file extension. Default is .log
String? fileExtension = 'log';
/// The path where the file(s) is/are stored
String? path = '';
/// The rotation cycle
RotationCycle rotationCycle = RotationCycle.NEVER;
/// The current file for the appender
late File _file;
///
/// Returns the full file name of the current logfile
///
String _getFullFilename() {
switch (rotationCycle) {
case RotationCycle.NEVER:
return path! + filePattern! + '.' + fileExtension!;
case RotationCycle.DAY:
return path! +
filePattern! +
'_' +
DateFormat('yyyy-MM-dd').format(created) +
'.' +
fileExtension!;
case RotationCycle.WEEK:
return path! +
filePattern! +
'_' +
created.year.toString() +
'-CW' +
DateUtils.getCalendarWeek(created).toString() +
'.' +
fileExtension!;
case RotationCycle.MONTH:
return path! +
filePattern! +
'_' +
DateFormat('yyyy-MM').format(created) +
'.' +
fileExtension!;
case RotationCycle.YEAR:
return path! +
filePattern! +
'_' +
DateFormat('yyyy').format(created) +
'.' +
fileExtension!;
}
return '';
}
@override
void append(LogRecord logRecord) async {
switch (rotationCycle) {
case RotationCycle.NEVER:
// Do nothing
break;
case RotationCycle.DAY:
case RotationCycle.WEEK:
case RotationCycle.MONTH:
case RotationCycle.YEAR:
await checkForFileChange();
break;
}
logRecord.loggerName ??= getType();
_file.writeAsStringSync(
LogRecordFormatter.format(logRecord, format!, dateFormat: dateFormat) +
'\n',
mode: FileMode.append);
if (logRecord.stackTrace != null) {
_file.writeAsStringSync(logRecord.stackTrace.toString() + '\n',
mode: FileMode.append);
}
}
@override
Future<void>? init(
Map<String, dynamic> config, bool test, DateTime? date) async {
created = date ?? DateTime.now();
type = AppenderType.FILE;
if (config.containsKey('format')) {
format = config['format'];
} else {
format = Appender.defaultFormat;
}
if (config.containsKey('dateFormat')) {
dateFormat = config['dateFormat'];
} else {
dateFormat = Appender.defaultDateFormat;
}
if (config.containsKey('filePattern')) {
filePattern = config['filePattern'];
} else {
throw ArgumentError('Missing file argument for file appender');
}
if (config.containsKey('fileExtension')) {
fileExtension = config['fileExtension'];
}
if (config.containsKey('rotationCycle')) {
rotationCycle = Utils.getRotationCycleFromString(config['rotationCycle']);
}
if (config.containsKey('level')) {
level = Level.fromString(config['level']);
} else {
level = Level.INFO;
}
if (config.containsKey('path')) {
path = config['path'];
}
if (!test) {
if (FileSystemEntity.typeSync(_getFullFilename()) ==
FileSystemEntityType.notFound) {
_file = await File(_getFullFilename()).create();
} else {
_file = File(_getFullFilename());
}
}
return null;
}
///
/// Check whether to create a new logfile depending on the [RotationCycle].
///
Future<void>? checkForFileChange() async {
var now = DateTime.now();
var create = false;
switch (rotationCycle) {
case RotationCycle.NEVER:
return;
case RotationCycle.DAY:
if (now.year > created.year || now.month > created.month) {
create = true;
} else if (now.day > created.day) {
create = true;
}
break;
case RotationCycle.WEEK:
if (now.year > created.year) {
create = true;
} else if (DateUtils.getCalendarWeek(now) >
DateUtils.getCalendarWeek(created)) {
create = true;
}
break;
case RotationCycle.MONTH:
if (now.year > created.year) {
create = true;
} else if (now.month > created.month) {
create = true;
}
break;
case RotationCycle.YEAR:
if (now.year > created.year) {
create = true;
}
break;
}
if (create) {
created = now;
_file = await File(_getFullFilename()).create();
}
return null;
}
@override
Appender getInstance() {
return FileAppender();
}
@override
String getType() {
return AppenderType.FILE.name;
}
}