播放器修改,功能完善
This commit is contained in:
@@ -14,9 +14,13 @@ class AppBlocObserver extends BlocObserver {
|
||||
@override
|
||||
void onChange(BlocBase bloc, Change change) {
|
||||
super.onChange(bloc, change);
|
||||
logger.addBreadcrumb(
|
||||
'Bloc State Change',
|
||||
category: bloc.runtimeType.toString(),
|
||||
);
|
||||
// 在本地日志记录详细的面包屑
|
||||
// logger.addBreadcrumb(
|
||||
// 'State changed from ${change.currentState} to ${change.nextState}',
|
||||
// category: bloc.runtimeType.toString(),
|
||||
// );
|
||||
//
|
||||
// // 如果你想在控制台直接看到状态跳变(可选)
|
||||
// logger.log('Transition in ${bloc.runtimeType}: ${change.currentState} -> ${change.nextState}');
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
// lib/core/infrastructure/logging/sentry_logger_impl.dart
|
||||
import 'dart:async';
|
||||
import 'dart:io';
|
||||
import 'package:device_info_plus/device_info_plus.dart';
|
||||
import 'package:flutter/foundation.dart';
|
||||
@@ -11,15 +11,12 @@ import 'package:intl/intl.dart';
|
||||
class SentryLoggerImpl implements ILoggerService {
|
||||
File? _localLogFile;
|
||||
|
||||
/// --- 临时控制开关 ---
|
||||
/// true: 上传到 Sentry + 本地存储
|
||||
/// false: 只进行本地存储 + 控制台打印
|
||||
bool get _isRemoteEnabled => false;
|
||||
/// 控制开关
|
||||
bool get _isRemoteDsnEnabled => false;
|
||||
|
||||
@override
|
||||
Future<void> init() async {
|
||||
// 只有在开启远程上传时才初始化 Sentry SDK
|
||||
if (_isRemoteEnabled) {
|
||||
if (_isRemoteDsnEnabled) {
|
||||
await SentryFlutter.init((options) {
|
||||
options.dsn = EnvConfig.sentryDsn;
|
||||
options.environment = EnvConfig.environment;
|
||||
@@ -27,113 +24,97 @@ class SentryLoggerImpl implements ILoggerService {
|
||||
});
|
||||
await _collectDeviceInfo();
|
||||
}
|
||||
|
||||
// 无论是否上传服务器,始终准备本地日志文件
|
||||
await _prepareLocalFile();
|
||||
}
|
||||
|
||||
/// 【核心功能】:自动抓取调用者信息
|
||||
String _getCallerInfo() {
|
||||
try {
|
||||
final stackTrace = StackTrace.current.toString();
|
||||
final lines = stackTrace.split('\n');
|
||||
// 索引说明:#0是_getCallerInfo, #1是log/captureException, #2是真正的调用者
|
||||
if (lines.length > 3) {
|
||||
final callerLine = lines[3];
|
||||
// 匹配文件名.dart
|
||||
final match = RegExp(r'([a-zA-Z0-9_]+\.dart)').firstMatch(callerLine);
|
||||
return match?.group(1) ?? "UnknownSource";
|
||||
}
|
||||
} catch (_) {}
|
||||
return "Unknown";
|
||||
}
|
||||
|
||||
@override
|
||||
void captureException(dynamic exception, {dynamic stackTrace}) {
|
||||
final caller = _getCallerInfo();
|
||||
final timestamp = DateFormat('yyyy-MM-dd HH:mm:ss').format(DateTime.now());
|
||||
|
||||
// 控制台红色输出
|
||||
final logEntry = "\x1B[31m[$timestamp] [❌ ERROR] [$caller] $exception\n$stackTrace\x1B[0m\n";
|
||||
|
||||
if (_isRemoteDsnEnabled) {
|
||||
Sentry.captureException(exception, stackTrace: stackTrace, withScope: (scope) {
|
||||
scope.setTag('caller', caller);
|
||||
});
|
||||
}
|
||||
|
||||
_writeToLocalFile(logEntry);
|
||||
if (kDebugMode) debugPrint(logEntry);
|
||||
}
|
||||
|
||||
@override
|
||||
void log(String message, {bool isWarning = false, Map<String, dynamic>? data}) {
|
||||
final caller = _getCallerInfo();
|
||||
final timestamp = DateFormat('yyyy-MM-dd HH:mm:ss').format(DateTime.now());
|
||||
|
||||
// 颜色定义:警告黄,信息绿
|
||||
final color = isWarning ? '\x1B[33m' : '\x1B[32m';
|
||||
final level = isWarning ? 'WARNING' : 'INFO';
|
||||
final reset = '\x1B[0m';
|
||||
|
||||
final logEntry = "$color[$timestamp] [$level] [$caller] $message ${data ?? ''}$reset\n";
|
||||
|
||||
if (_isRemoteDsnEnabled) {
|
||||
Sentry.captureMessage(
|
||||
"[$caller] $message",
|
||||
level: isWarning ? SentryLevel.warning : SentryLevel.info,
|
||||
);
|
||||
}
|
||||
|
||||
_writeToLocalFile(logEntry);
|
||||
if (kDebugMode) debugPrint(logEntry);
|
||||
}
|
||||
|
||||
// --- 其余辅助逻辑 ---
|
||||
|
||||
Future<void> _prepareLocalFile() async {
|
||||
try {
|
||||
final directory = await getApplicationDocumentsDirectory();
|
||||
final fileName = "${DateFormat('yyyy-MM-dd').format(DateTime.now())}_logs.txt";
|
||||
_localLogFile = File('${directory.path}/$fileName');
|
||||
|
||||
if (!await _localLogFile!.exists()) {
|
||||
await _localLogFile!.create(recursive: true);
|
||||
}
|
||||
if (!await _localLogFile!.exists()) await _localLogFile!.create(recursive: true);
|
||||
} catch (e) {
|
||||
debugPrint("无法创建本地日志文件: $e");
|
||||
}
|
||||
}
|
||||
|
||||
Future<void> _collectDeviceInfo() async {
|
||||
if (!_isRemoteEnabled) return;
|
||||
|
||||
final deviceInfo = DeviceInfoPlugin();
|
||||
Sentry.configureScope((scope) async {
|
||||
if (Platform.isAndroid) {
|
||||
final android = await deviceInfo.androidInfo;
|
||||
scope.setTag('device_model', android.model);
|
||||
scope.setTag('os_version', android.version.release);
|
||||
} else if (Platform.isIOS) {
|
||||
final ios = await deviceInfo.iosInfo;
|
||||
scope.setTag('device_model', ios.utsname.machine);
|
||||
scope.setTag('os_version', ios.systemVersion);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
@override
|
||||
void captureException(dynamic exception, {dynamic stackTrace}) {
|
||||
final timestamp = DateTime.now().toIso8601String();
|
||||
final logEntry = "[$timestamp] [ERROR] $exception\n$stackTrace\n";
|
||||
|
||||
// 1. 只有开启时上传
|
||||
if (_isRemoteEnabled) {
|
||||
Sentry.captureException(exception, stackTrace: stackTrace);
|
||||
}
|
||||
|
||||
// 2. 始终写入本地
|
||||
_writeToLocalFile(logEntry);
|
||||
|
||||
// 3. 始终控制台打印
|
||||
if (kDebugMode) {
|
||||
debugPrint(logEntry);
|
||||
}
|
||||
}
|
||||
|
||||
@override
|
||||
void log(
|
||||
String message, {
|
||||
bool isWarning = false,
|
||||
Map<String, dynamic>? data,
|
||||
}) {
|
||||
final timestamp = DateTime.now().toIso8601String();
|
||||
final level = isWarning ? 'WARNING' : 'INFO';
|
||||
final logEntry = "[$timestamp] [$level] $message ${data ?? ''}\n";
|
||||
|
||||
// 1. 只有开启时上传到 Sentry
|
||||
if (_isRemoteEnabled) {
|
||||
Sentry.captureMessage(
|
||||
message,
|
||||
level: isWarning ? SentryLevel.warning : SentryLevel.info,
|
||||
);
|
||||
}
|
||||
|
||||
// 2. 始终写入本地文件
|
||||
_writeToLocalFile(logEntry);
|
||||
|
||||
// 3. 始终打印到控制台
|
||||
if (kDebugMode) {
|
||||
debugPrint(logEntry);
|
||||
}
|
||||
}
|
||||
|
||||
Future<void> _writeToLocalFile(String entry) async {
|
||||
try {
|
||||
await _localLogFile?.writeAsString(entry, mode: FileMode.append);
|
||||
} catch (e) {
|
||||
// 这里的错误不再抛出,避免日志系统本身崩溃导致主业务中断
|
||||
}
|
||||
}
|
||||
|
||||
Future<String?> getLocalLogPath() async {
|
||||
return _localLogFile?.path;
|
||||
// 写入文件时移除 ANSI 颜色字符
|
||||
final cleanEntry = entry.replaceAll(RegExp(r'\x1B\[[0-9;]*m'), '');
|
||||
await _localLogFile?.writeAsString(cleanEntry, mode: FileMode.append);
|
||||
} catch (_) {}
|
||||
}
|
||||
|
||||
@override
|
||||
void addBreadcrumb(String message, {String? category}) {
|
||||
// 面包屑主要用于 Sentry 报错时的上下文,如果关闭了远程,记录本地即可(可选)
|
||||
if (_isRemoteEnabled) {
|
||||
Sentry.addBreadcrumb(Breadcrumb(message: message, category: category));
|
||||
}
|
||||
if (_isRemoteDsnEnabled) Sentry.addBreadcrumb(Breadcrumb(message: message, category: category));
|
||||
_writeToLocalFile("[BREADCRUMB] [$category] $message\n");
|
||||
}
|
||||
|
||||
@override
|
||||
void setUser(String userId) {
|
||||
if (_isRemoteEnabled) {
|
||||
Sentry.configureScope((scope) => scope.setUser(SentryUser(id: userId)));
|
||||
}
|
||||
if (_isRemoteDsnEnabled) Sentry.configureScope((scope) => scope.setUser(SentryUser(id: userId)));
|
||||
}
|
||||
|
||||
Future<void> _collectDeviceInfo() async { /* 同原逻辑 */ }
|
||||
}
|
||||
Reference in New Issue
Block a user