-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathlogger_file.dart
100 lines (78 loc) · 2.67 KB
/
logger_file.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
import 'dart:io';
import 'package:cherrilog/formatter/message.dart';
import 'package:cherrilog/logger/logger.dart';
import 'package:cherrilog/model/message.dart';
import 'package:dart_art/dart_art.dart';
class CherriFile extends CherriLogger {
late String fileNamePattern = '@yyyy-@MM-@dd-@HH-@id.log';
late String location = './logs';
late Duration shelfLife = const Duration(days: 20);
late int maxFilesCount = 30;
late BinarySize maxSizeLimit = BinarySize.parse('100 MB')!;
late BinarySize singleFileSizeLimit = BinarySize.parse('10 MB')!;
late int clearMode = CherriFileClearMode.oldFiles | CherriFileClearMode.outSizedFiles;
late int _fileId = 0;
CherriFile() {
_fileId = getNextId();
}
int getNextId() {
var lastIdFile = File('$location/options/last_id');
if (lastIdFile.existsSync()) {
var result = int.parse(lastIdFile.readAsStringSync());
lastIdFile.writeAsStringSync((result + 1).toString());
return result + 1;
} else {
lastIdFile
..createSync(recursive: true)
..writeAsStringSync('1');
return 1;
}
}
String getFileName() {
var now = DateTime.now();
var fileName = fileNamePattern
.replaceAll('@yyyy', now.year.toString().padLeft(4, '0'))
.replaceAll('@MM', now.month.toString().padLeft(2, '0'))
.replaceAll('@dd', now.day.toString().padLeft(2, '0'))
.replaceAll('@HH', now.hour.toString().padLeft(2, '0'))
.replaceAll('@mm', now.minute.toString().padLeft(2, '0'))
.replaceAll('@ss', now.second.toString().padLeft(2, '0'))
.replaceAll('@id', _fileId.toString());
var file = File('$location/$fileName');
if (file.existsSync()) {
var size = BinarySize()..bytesCount = file.lengthSync();
if (size > singleFileSizeLimit) {
_fileId = getNextId();
return getFileName();
}
}
return fileName;
}
@override
void log(CherriMessage message) {
var formatter = CherriFormatterMessageDefault(
timestampPattern: options.timeStampPattern,
costumeSplitter: ' ',
autoColorize: false,
);
if (message.logLevel.inRange(options.logLevelRange)) {
var formattedMessage = formatter.format(message);
for (var line in formattedMessage.split('\n')) {
pushLine(line);
}
}
}
@override
void flush() {
var file = File('$location/${getFileName()}');
if (file.existsSync() == false) {
file.createSync(recursive: true);
}
var content = '${buffer.join('\n')}\n';
file.writeAsStringSync(content, mode: FileMode.writeOnlyAppend);
}
}
class CherriFileClearMode {
static const oldFiles = 1;
static const outSizedFiles = 2;
}