init method
初始化(建议 App 启动时调用一次)。
参数含义参考 flutter_mxlogger 文档:
- storagePolicy: 例如 "yyyy_MM_dd_HH"
- cryptKey/iv: AES CFB 128 位加密参数(16 字符示例)
Implementation
Future<mx.MXLogger> init({
String nameSpace = 'flutter.mxlogger',
mx.MXStoragePolicyType storagePolicy = mx.MXStoragePolicyType.yyyy_MM_dd_HH,
String cryptKey = 'abcuioqbsdguijlk',
String iv = 'bccuioqbsdguijiv',
int? maxDiskAgeSeconds,
int? maxDiskSizeBytes,
int? fileLevel,
int maxPendingLogs = 200,
}) {
// 已初始化:直接复用
final existing = _inner;
if (existing != null) return Future.value(existing);
// 初始化中:复用同一个 Future,避免并发重复 initialize
final inflight = _initFuture;
if (inflight != null) return inflight;
_maxPending = maxPendingLogs;
final future = mx.MXLogger.initialize(
directory: TCICConst.logFilePath ?? "",
nameSpace: nameSpace,
storagePolicy: storagePolicy,
cryptKey: cryptKey,
iv: iv,
).then((logger) {
// 额外配置(可选)
if (maxDiskAgeSeconds != null) {
logger.setMaxDiskAge(maxDiskAgeSeconds);
}
if (maxDiskSizeBytes != null) {
logger.setMaxDiskSize(maxDiskSizeBytes);
}
if (fileLevel != null) {
logger.setLevel(fileLevel);
}
final logLevel = TCICController.instance.getConfig().logLevel ?? TCICLogLevelEnum.error;
logger.setConsoleEnable(logLevel == TCICLogLevelEnum.all || logLevel == TCICLogLevelEnum.debug || logLevel == TCICLogLevelEnum.info);
_inner = logger;
_loggerKey = logger.loggerKey;
// 尽量等待后台 isolate 握手完成后再 flush,避免刚初始化阶段把 debug/info 写在 UI isolate 上
_ensureBgIsolate().whenComplete(() {
_flushPending(logger);
});
return logger;
}).whenComplete(() {
_initFuture = null;
});
_initFuture = future;
return future;
}