Refactor app_logger to remove storage permission request and improve log file handling
This commit is contained in:
parent
ddbc1ec1e5
commit
98836f8157
@ -1,18 +1,14 @@
|
||||
import 'dart:io';
|
||||
import 'package:logger/logger.dart';
|
||||
import 'package:intl/intl.dart';
|
||||
import 'package:permission_handler/permission_handler.dart';
|
||||
import 'package:path_provider/path_provider.dart';
|
||||
|
||||
/// Global logger instance
|
||||
late final Logger appLogger;
|
||||
|
||||
/// Log file output handler
|
||||
late final FileLogOutput fileLogOutput;
|
||||
|
||||
/// Initialize logging (call once in `main()`)
|
||||
/// Initialize logging
|
||||
Future<void> initLogging() async {
|
||||
await requestStoragePermission();
|
||||
|
||||
fileLogOutput = FileLogOutput();
|
||||
|
||||
appLogger = Logger(
|
||||
@ -23,21 +19,13 @@ Future<void> initLogging() async {
|
||||
printEmojis: true,
|
||||
),
|
||||
output: MultiOutput([
|
||||
ConsoleOutput(), // ✅ Console will use the top-level PrettyPrinter
|
||||
fileLogOutput, // ✅ File will still use the SimpleFileLogPrinter
|
||||
ConsoleOutput(),
|
||||
fileLogOutput,
|
||||
]),
|
||||
level: Level.debug,
|
||||
);
|
||||
}
|
||||
|
||||
/// Request storage permission (for Android 11+)
|
||||
Future<void> requestStoragePermission() async {
|
||||
final status = await Permission.manageExternalStorage.status;
|
||||
if (!status.isGranted) {
|
||||
await Permission.manageExternalStorage.request();
|
||||
}
|
||||
}
|
||||
|
||||
/// Safe logger wrapper
|
||||
void logSafe(
|
||||
String message, {
|
||||
@ -46,7 +34,7 @@ void logSafe(
|
||||
StackTrace? stackTrace,
|
||||
bool sensitive = false,
|
||||
}) {
|
||||
if (sensitive) return;
|
||||
if (sensitive) return;
|
||||
|
||||
switch (level) {
|
||||
case LogLevel.debug:
|
||||
@ -66,15 +54,15 @@ void logSafe(
|
||||
}
|
||||
}
|
||||
|
||||
/// Custom log output that writes to a local `.txt` file
|
||||
/// Log output to file (safe path, no permission required)
|
||||
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');
|
||||
final baseDir = await getExternalStorageDirectory();
|
||||
final directory = Directory('${baseDir!.path}/marco_logs');
|
||||
if (!await directory.exists()) {
|
||||
await directory.create(recursive: true);
|
||||
}
|
||||
@ -119,7 +107,6 @@ class FileLogOutput extends LogOutput {
|
||||
return _logFile!.readAsString();
|
||||
}
|
||||
|
||||
/// Delete logs older than 3 days
|
||||
Future<void> _cleanOldLogs(Directory directory) async {
|
||||
final files = directory.listSync();
|
||||
final now = DateTime.now();
|
||||
@ -135,7 +122,7 @@ class FileLogOutput extends LogOutput {
|
||||
}
|
||||
}
|
||||
|
||||
/// A simple, readable log printer for file output
|
||||
/// Simple log printer for file output
|
||||
class SimpleFileLogPrinter extends LogPrinter {
|
||||
@override
|
||||
List<String> log(LogEvent event) {
|
||||
@ -152,5 +139,5 @@ class SimpleFileLogPrinter extends LogPrinter {
|
||||
}
|
||||
}
|
||||
|
||||
/// Optional log level enum for better type safety
|
||||
/// Optional enum for log levels
|
||||
enum LogLevel { debug, info, warning, error, verbose }
|
||||
|
Loading…
x
Reference in New Issue
Block a user