- Replaced instances of the Logger package with a custom appLogger for consistent logging. - Introduced app_logger.dart to manage logging with file output and storage permissions. - Updated all controllers (e.g., DashboardController, EmployeesScreenController, etc.) to use appLogger for logging messages. - Ensured that logging messages are appropriately categorized (info, warning, error) throughout the application. - Implemented a file logging mechanism to store logs in a designated directory. - Cleaned up old log files to maintain only the most recent logs.
108 lines
2.9 KiB
Dart
108 lines
2.9 KiB
Dart
import 'dart:io';
|
|
import 'package:logger/logger.dart';
|
|
import 'package:intl/intl.dart';
|
|
import 'package:permission_handler/permission_handler.dart';
|
|
|
|
/// Global logger instance
|
|
late final Logger appLogger;
|
|
|
|
/// Log file output handler
|
|
late final FileLogOutput fileLogOutput;
|
|
|
|
/// Initialize logging (call once in `main()`)
|
|
Future<void> initLogging() async {
|
|
await requestStoragePermission();
|
|
fileLogOutput = FileLogOutput();
|
|
appLogger = Logger(
|
|
printer: SimpleFileLogPrinter(),
|
|
output: fileLogOutput,
|
|
);
|
|
}
|
|
|
|
/// Request storage permission (for Android 11+)
|
|
Future<void> requestStoragePermission() async {
|
|
final status = await Permission.manageExternalStorage.status;
|
|
if (!status.isGranted) {
|
|
await Permission.manageExternalStorage.request();
|
|
}
|
|
}
|
|
|
|
/// Custom log output that writes to a local `.txt` file
|
|
class FileLogOutput extends LogOutput {
|
|
File? _logFile;
|
|
|
|
/// Initialize log file in Downloads/marco_logs/log_YYYY-MM-DD.txt
|
|
Future<void> _init() async {
|
|
if (_logFile != null) return;
|
|
|
|
final directory = Directory('/storage/emulated/0/Download/marco_logs');
|
|
if (!await directory.exists()) {
|
|
await directory.create(recursive: true);
|
|
}
|
|
|
|
final date = DateFormat('yyyy-MM-dd').format(DateTime.now());
|
|
final filePath = '${directory.path}/log_$date.txt';
|
|
_logFile = File(filePath);
|
|
|
|
if (!await _logFile!.exists()) {
|
|
await _logFile!.create();
|
|
}
|
|
|
|
await _cleanOldLogs(directory);
|
|
}
|
|
|
|
@override
|
|
void output(OutputEvent event) async {
|
|
await _init();
|
|
final logMessage = event.lines.join('\n') + '\n';
|
|
await _logFile!.writeAsString(
|
|
logMessage,
|
|
mode: FileMode.append,
|
|
flush: true,
|
|
);
|
|
}
|
|
|
|
Future<String> getLogFilePath() async {
|
|
await _init();
|
|
return _logFile!.path;
|
|
}
|
|
|
|
Future<void> clearLogs() async {
|
|
await _init();
|
|
await _logFile!.writeAsString('');
|
|
}
|
|
|
|
Future<String> readLogs() async {
|
|
await _init();
|
|
return _logFile!.readAsString();
|
|
}
|
|
|
|
/// Delete logs older than 3 days
|
|
Future<void> _cleanOldLogs(Directory directory) async {
|
|
final files = directory.listSync();
|
|
final now = DateTime.now();
|
|
|
|
for (var file in files) {
|
|
if (file is File && file.path.endsWith('.txt')) {
|
|
final stat = await file.stat();
|
|
if (now.difference(stat.modified).inDays > 3) {
|
|
await file.delete();
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
/// A simple, readable log printer for file output
|
|
class SimpleFileLogPrinter extends LogPrinter {
|
|
@override
|
|
List<String> log(LogEvent event) {
|
|
final timestamp = DateFormat('yyyy-MM-dd HH:mm:ss').format(DateTime.now());
|
|
final level = event.level.name.toUpperCase();
|
|
final message = event.message;
|
|
final error = event.error != null ? ' | ERROR: ${event.error}' : '';
|
|
final stack = event.stackTrace != null ? '\nSTACKTRACE:\n${event.stackTrace}' : '';
|
|
return ['[$timestamp] [$level] $message$error$stack'];
|
|
}
|
|
}
|