mirror of
https://github.com/ZhuJHua/moodiary.git
synced 2026-04-05 16:31:45 +08:00
fix(log): log did not work in release mode
This commit is contained in:
@@ -1,58 +1,16 @@
|
||||
import 'dart:convert';
|
||||
import 'dart:io';
|
||||
|
||||
import 'package:flutter/foundation.dart';
|
||||
import 'package:logger/logger.dart';
|
||||
import 'package:moodiary/utils/file_util.dart';
|
||||
|
||||
class FileOutput extends LogOutput {
|
||||
final bool overrideExisting;
|
||||
final Encoding encoding;
|
||||
IOSink? _sink;
|
||||
|
||||
FileOutput({
|
||||
this.overrideExisting = false,
|
||||
this.encoding = utf8,
|
||||
});
|
||||
|
||||
@override
|
||||
Future<void> init() async {
|
||||
final file = File(FileUtil.getErrorLogPath());
|
||||
if (!await file.exists()) {
|
||||
await file.create(recursive: true);
|
||||
await file.writeAsString('');
|
||||
}
|
||||
return super.init();
|
||||
}
|
||||
|
||||
void _initIOSink() {
|
||||
_sink = File(FileUtil.getErrorLogPath()).openWrite(
|
||||
mode: overrideExisting ? FileMode.write : FileMode.append,
|
||||
encoding: encoding,
|
||||
);
|
||||
}
|
||||
|
||||
@override
|
||||
void output(OutputEvent event) {
|
||||
if (_sink == null) _initIOSink();
|
||||
|
||||
if (event.level.value >= Level.warning.value) {
|
||||
_sink?.writeAll(event.lines, '\n');
|
||||
}
|
||||
}
|
||||
|
||||
@override
|
||||
Future<void> destroy() async {
|
||||
await _sink?.flush();
|
||||
await _sink?.close();
|
||||
_sink = null;
|
||||
return super.destroy();
|
||||
}
|
||||
}
|
||||
|
||||
class LogUtil {
|
||||
static final Logger _logger =
|
||||
Logger(output: kDebugMode ? null : FileOutput());
|
||||
static final Logger _logger = Logger(
|
||||
output: kDebugMode
|
||||
? ConsoleOutput()
|
||||
: FileOutput(file: File(FileUtil.getErrorLogPath())),
|
||||
filter: kDebugMode ? DevelopmentFilter() : ProductionFilter(),
|
||||
);
|
||||
|
||||
static void printError(message,
|
||||
{required Object error, StackTrace? stackTrace}) {
|
||||
|
||||
Reference in New Issue
Block a user