145 lines
4.5 KiB
Dart
145 lines
4.5 KiB
Dart
import 'dart:io';
|
|
import 'package:img_picker/img_picker.dart';
|
|
import 'package:path_provider/path_provider.dart';
|
|
import 'package:intl/intl.dart';
|
|
|
|
class DailyLogUtils {
|
|
// 获取日志文件路径(按日期命名)
|
|
static Future<File> _getLogFile() async {
|
|
final dir = await getApplicationDocumentsDirectory();
|
|
final date = DateFormat('yyyy-MM-dd').format(DateTime.now());
|
|
final filePath = '${dir.path}/$date.log';
|
|
final file = File(filePath);
|
|
if (!await file.exists()) {
|
|
await file.create(recursive: true);
|
|
}
|
|
return file;
|
|
}
|
|
|
|
// 写入日志核心方法,带日志等级
|
|
static Future<void> _writeLogWithLevel(String level, String content) async {
|
|
final file = await _getLogFile();
|
|
final now = DateTime.now();
|
|
final time = DateFormat('HH:mm:ss').format(now);
|
|
final logLine = '[$time][$level] $content\n';
|
|
await file.writeAsString(logLine, mode: FileMode.append);
|
|
}
|
|
|
|
// 写入 info 日志(原 writeLog 保留)
|
|
static Future<void> writeLog(String content) async {
|
|
print("[dailylog-->info] $content]");
|
|
await _writeLogWithLevel('INFO', content);
|
|
}
|
|
|
|
// 写入 warning 日志
|
|
static Future<void> writeWarning(String content) async {
|
|
print("[dailylog-->waring] $content]");
|
|
await _writeLogWithLevel('WARNING', content);
|
|
}
|
|
|
|
// 写入 error 日志
|
|
static Future<void> writeError(String content) async {
|
|
print("[dailylog-->error] $content]");
|
|
await _writeLogWithLevel('ERROR', content);
|
|
}
|
|
|
|
// 写入 debug 日志
|
|
static Future<void> writeDebug(String content) async {
|
|
print("[dailylog-->debug] $content]");
|
|
await _writeLogWithLevel('DEBUG', content);
|
|
}
|
|
|
|
static Future<void> printLog(String content) async {
|
|
print("logger--->" + content);
|
|
// await writeLog(content);
|
|
}
|
|
|
|
// 读取当天日志
|
|
static Future<String> readTodayLog() async {
|
|
final file = await _getLogFile();
|
|
return await file.readAsString();
|
|
}
|
|
|
|
// 获取所有日志文件(返回 File 列表)
|
|
static Future<List<FileSystemEntity>> listLogFiles() async {
|
|
final dir = await getApplicationDocumentsDirectory();
|
|
final files = dir.listSync();
|
|
return files.where((f) => f.path.endsWith('.log')).toList();
|
|
}
|
|
|
|
// 清除所有日志
|
|
static Future<void> clearAllLogs() async {
|
|
final files = await listLogFiles();
|
|
for (final f in files) {
|
|
await File(f.path).delete();
|
|
}
|
|
}
|
|
|
|
/// 获取指定日期范围内的日志文件(包含起止日期)
|
|
static Future<List<File>> getLogsBetween(
|
|
DateTime fromDate, DateTime toDate) async {
|
|
final dir = await getApplicationDocumentsDirectory();
|
|
final logFiles = <File>[];
|
|
final dateFormat = DateFormat('yyyy-MM-dd');
|
|
|
|
for (DateTime date = fromDate;
|
|
!date.isAfter(toDate);
|
|
date = date.add(Duration(days: 1))) {
|
|
final fileName = '${dateFormat.format(date)}.log';
|
|
final file = File('${dir.path}/$fileName');
|
|
if (await file.exists()) {
|
|
logFiles.add(file);
|
|
}
|
|
}
|
|
|
|
return logFiles;
|
|
}
|
|
|
|
static Future<String> readLogsByDateRange(
|
|
DateTime fromDate, DateTime toDate) async {
|
|
try {
|
|
final dir = await getApplicationDocumentsDirectory();
|
|
final dateFormat = DateFormat('yyyy-MM-dd');
|
|
String combinedLogs = '';
|
|
|
|
// 遍历从 fromDate 到 toDate 的日期
|
|
for (DateTime date = fromDate;
|
|
!date.isAfter(toDate);
|
|
date = date.add(Duration(days: 1))) {
|
|
final dateStr = dateFormat.format(date); // 格式化日期
|
|
final file = File('${dir.path}/$dateStr.log'); // 日志文件路径
|
|
|
|
if (await file.exists()) {
|
|
final logContent = await file.readAsString();
|
|
combinedLogs += '日志日期: $dateStr\n$logContent\n\n'; // 合并日志内容
|
|
}
|
|
}
|
|
|
|
if (combinedLogs.isNotEmpty) {
|
|
return combinedLogs; // 返回合并后的日志
|
|
} else {
|
|
return '该时间段内没有日志记录'; // 如果没有日志
|
|
}
|
|
} catch (e) {
|
|
return '读取日志失败: $e';
|
|
}
|
|
}
|
|
|
|
// 添加分享功能
|
|
// static Future<void> exportLog(DateTime date) async {
|
|
// try {
|
|
// final dir = await getApplicationDocumentsDirectory();
|
|
// final dateStr = DateFormat('yyyy-MM-dd').format(date);
|
|
// final file = File('${dir.path}/$dateStr.log');
|
|
// if (await file.exists()) {
|
|
// await Share.shareXFiles(
|
|
// [XFile(file.path)],
|
|
// text: '应用日志 $dateStr',
|
|
// );
|
|
// }
|
|
// } catch (e) {
|
|
// print('导出日志失败: $e');
|
|
// }
|
|
// }
|
|
}
|