feat:请求日志,和日志删除
This commit is contained in:
1 parent
c6aabbd1b7
commit
5a1fe03f0c
26 files changed
+5661
-45
No files matched your search
@@ -1,4 +1,5 @@
|
||||
import 'dart:async';
|
||||
import 'dart:io';
|
||||
|
||||
import 'package:get/get.dart';
|
||||
import 'package:mqtt_client/mqtt_client.dart';
|
||||
@@ -39,6 +40,7 @@ class SyncCheckTool {
|
||||
checkMq();
|
||||
checkPrint();
|
||||
checkBroadcast();
|
||||
checkExpiredLog();
|
||||
}
|
||||
|
||||
void dispose() {
|
||||
@@ -129,4 +131,54 @@ class SyncCheckTool {
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
// 检查过期日志(超过3个月的日志文件都要删除)
|
||||
checkExpiredLog() async {
|
||||
const logMsg = "检查过期日志";
|
||||
try {
|
||||
final backupPath = await AppFileTool.getLoggerBackupPath();
|
||||
if (backupPath == null || backupPath.isEmpty) {
|
||||
logger.w("|$_tag|$logMsg,日志备份路径为空");
|
||||
return;
|
||||
}
|
||||
final loggerBackupDir = Directory(backupPath).parent.parent;
|
||||
if (!await loggerBackupDir.exists()) {
|
||||
logger.i("|$_tag|$logMsg,日志目录不存在 path:${loggerBackupDir.path}");
|
||||
return;
|
||||
}
|
||||
final expireBefore = DateTime(
|
||||
DateTime.now().year,
|
||||
DateTime.now().month - 3,
|
||||
DateTime.now().day,
|
||||
);
|
||||
final entities = loggerBackupDir.listSync(recursive: true);
|
||||
int deleteCount = 0;
|
||||
for (final entity in entities) {
|
||||
if (entity is! File) continue;
|
||||
if (!entity.path.endsWith(".log")) continue;
|
||||
final stat = await entity.stat();
|
||||
if (stat.modified.isBefore(expireBefore)) {
|
||||
await entity.delete();
|
||||
deleteCount++;
|
||||
logger.i("|$_tag|$logMsg,删除过期日志 path:${entity.path}");
|
||||
}
|
||||
}
|
||||
final monthDirs = loggerBackupDir
|
||||
.listSync()
|
||||
.whereType<Directory>()
|
||||
.toList()
|
||||
..sort((a, b) => b.path.compareTo(a.path));
|
||||
for (final dir in monthDirs) {
|
||||
if (!await dir.exists()) continue;
|
||||
final isEmpty = dir.listSync().isEmpty;
|
||||
if (isEmpty) {
|
||||
await dir.delete();
|
||||
logger.i("|$_tag|$logMsg,删除空目录 path:${dir.path}");
|
||||
}
|
||||
}
|
||||
logger.i("|$_tag|$logMsg,执行完成,删除日志数量:$deleteCount");
|
||||
} catch (e, stack) {
|
||||
logger.e("|$_tag|$logMsg,执行失败,error:$e,stack:$stack");
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user