Flutter日志收集与分析插件flutter_bugfender的使用
Flutter日志收集与分析插件flutter_bugfender的使用
flutter_bugfender
是一个为Flutter项目提供的Bugfender封装插件,它实现了原生代码以支持日志记录和崩溃报告等功能。需要注意的是,该插件由社区提供,因此按照“现状”发布,官方支持可能有限。
使用包
添加依赖
首先,在 pubspec.yaml
文件中添加 flutter_bugfender
依赖:
dependencies:
flutter:
sdk: flutter
flutter_bugfender: ^3.0.0
然后运行 flutter pub get
来下载安装这个包。
初始化插件
在 lib/main.dart
中导入插件:
import 'package:flutter_bugfender/flutter_bugfender.dart';
并在 main()
函数中初始化插件,包裹 runApp()
调用:
void main() {
FlutterBugfender.handleUncaughtErrors(() async {
await FlutterBugfender.init("YOUR_APP_KEY",
enableCrashReporting: true, // 可选但推荐开启
enableUIEventLogging: true,
enableAndroidLogcatLogging: true);
FlutterBugfender.log("hello world!");
runApp(new MyApp());
});
}
更多初始化选项
apiUri
和baseUri
: 用于自定义服务器地址(适用于本地部署)maximumLocalStorageSize
: 设置本地日志缓存的最大大小(字节)printToConsole
: 是否打印到控制台enableUIEventLogging
: 自动记录用户交互事件的日志enableCrashReporting
: 自动报告原生崩溃信息enableAndroidLogcatLogging
: 自动记录Android Logcat日志overrideDeviceName
: 设备名称(已弃用,建议使用FlutterBugfender.setDeviceString()
替代)
其他功能调用示例
FlutterBugfender.log("Working fine!");
FlutterBugfender.fatal("Fatal sent!");
FlutterBugfender.error("Error sent!");
FlutterBugfender.warn("Warning sent!");
FlutterBugfender.info("Info sent!");
FlutterBugfender.debug("Debug sent!");
FlutterBugfender.trace("Trace sent!");
FlutterBugfender.sendLog(
line: 42,
method: "someMethod()",
file:"someFile",
level: LogLevel.info,
tag: "Custom tag",
text: "This is a custom log"
);
FlutterBugfender.setDeviceString("user.email", "example@example.com");
FlutterBugfender.setDeviceInt("user.id", 32);
FlutterBugfender.setDeviceFloat("user.pi", 3.14);
FlutterBugfender.setDeviceBool("user.enabled", true);
FlutterBugfender.removeDeviceKey("user.pi");
FlutterBugfender.sendCrash("Test Crash", "Stacktrace here!");
FlutterBugfender.sendIssue("Test Issue", "Issue value goes here!");
FlutterBugfender.sendUserFeedback("Test user feedback", "User feedback details here!");
FlutterBugfender.setForceEnabled(true);
FlutterBugfender.forceSendOnce();
FlutterBugfender.getDeviceUri();
FlutterBugfender.getSessionUri();
FlutterBugfender.getUserFeedback(); // 显示请求反馈的屏幕
报告Dart和Flutter异常
对于更精细的错误处理控制,可以替换 handleUncaughtErrors()
调用如下:
// 捕获Flutter错误
FlutterError.onError = (FlutterErrorDetails details) async {
FlutterError.presentError(details);
await FlutterBugfender.sendCrash(details.exception.toString(), details.stack?.toString() ?? "");
};
// 捕获Dart异常
runZonedGuarded(() {
runApp(new MyApp());
}, (Object error, StackTrace stack) async {
await FlutterBugfender.sendCrash(error.toString(), stack.toString());
});
完整示例Demo
下面是一个完整的示例应用,展示了如何集成和使用 flutter_bugfender
:
import 'package:flutter/material.dart';
import 'package:flutter_bugfender/flutter_bugfender.dart';
void main() {
FlutterBugfender.handleUncaughtErrors(() async {
await FlutterBugfender.init("<INSERT YOUR BUGFENDER APP KEY>",
printToConsole: true,
enableCrashReporting: true,
enableAndroidLogcatLogging: false,
version: "1.0",
build: "555");
FlutterBugfender.log("hello world!");
runApp(new MyApp());
});
}
class MyApp extends StatefulWidget {
@override
_MyAppState createState() => new _MyAppState();
}
class _MyAppState extends State<MyApp> {
String _message = 'Bugfender not started';
@override
void initState() {
super.initState();
initPlatformState();
}
Future<void> initPlatformState() async {
try {
FlutterBugfender.log("Working fine!");
FlutterBugfender.fatal("Fatal sent!");
FlutterBugfender.error("Error sent!");
FlutterBugfender.warn("Warning sent!");
FlutterBugfender.info("Info sent!");
FlutterBugfender.debug("Debug sent!");
FlutterBugfender.trace("Trace sent!");
FlutterBugfender.sendLog(
line: 42,
method: "fakeMethod()",
file: "fakeFile.fake",
level: LogLevel.info,
tag: "TAG",
text: "Custom log 1");
FlutterBugfender.sendLog(tag: "TAG", text: "Custom log 2");
FlutterBugfender.setDeviceString("user.email", "example@example.com");
FlutterBugfender.setDeviceInt("user.id", 1);
FlutterBugfender.setDeviceFloat("user.weight", 1.234);
FlutterBugfender.setDeviceBool("user.team", true);
FlutterBugfender.removeDeviceKey("user.team");
print(await FlutterBugfender.sendCrash("Test Crash", "Stacktrace here!"));
print(await FlutterBugfender.sendIssue("Test Issue", "Issue value goes here!"));
print(await FlutterBugfender.sendUserFeedback("Test user feedback", "User feedback details here!"));
FlutterBugfender.setForceEnabled(true);
FlutterBugfender.setForceEnabled(false);
FlutterBugfender.forceSendOnce();
print(await FlutterBugfender.getDeviceUri());
print(await FlutterBugfender.getSessionUri());
} catch (e) {
print("Error found!!!! $e");
throw e;
}
if (!mounted) return;
setState(() {
_message = "All working fine!";
});
}
void getUserFeedback() async {
print(await FlutterBugfender.getUserFeedback());
}
void generateException() async {
var result = 100 ~/ 0; // 这将导致除以零异常
}
void generateError() {
throw Error(); // 手动生成一个错误
}
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(
title: Text('Bugfender Plugin example app'),
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
crossAxisAlignment: CrossAxisAlignment.center,
children: [
Text('$_message\n'),
ElevatedButton(
onPressed: () {
getUserFeedback();
},
child: Text('Show Feedback Screen'),
),
ElevatedButton(
onPressed: () {
generateException();
},
child: Text('Generate exception'),
),
ElevatedButton(
onPressed: () {
generateError();
},
child: Text('Generate error'),
)
],
),
),
),
);
}
}
以上示例展示了一个基本的应用程序框架,其中包括了如何使用 flutter_bugfender
插件来记录日志、发送崩溃报告以及获取用户反馈等操作。请确保根据实际需要调整代码中的参数和逻辑。
更多关于Flutter日志收集与分析插件flutter_bugfender的使用的实战系列教程也可以访问 https://www.itying.com/category-92-b0.html
1 回复