Flutter错误追踪与日志管理插件drift_sentry的使用
Flutter错误追踪与日志管理插件drift_sentry的使用
Drift Sentry
注意
官方插件 sentry_drift
已经发布。请在新项目中使用该插件。
[CI]: https://github.com/utisam/drift_sentry/actions/workflows/ci.yml/badge.svg
[pub package]: https://img.shields.io/pub/v/drift_sentry.svg
Sentry 集成到 drift
包中。
使用方法
LazyDatabase _openConnection() {
return LazyDatabase(() async {
final dbFolder = await getApplicationDocumentsDirectory();
final file = File(p.join(dbFolder.path, 'db.sqlite'));
// 使用 .addSentry() 包装 QueryExecutor
return NativeDatabase.createInBackground(file).addSentry();
});
}
完整示例代码
// 忽略对print的避免使用警告
import 'dart:async';
import 'dart:io';
import 'package:drift/native.dart';
import 'package:drift_sentry/drift_sentry.dart';
import 'package:sentry/sentry.dart';
import 'database.dart';
const dsn = String.fromEnvironment('dsn');
void main() async {
await runZonedGuarded(() async {
await Sentry.init(
(options) => options
..dsn = dsn
..tracesSampleRate = 1.0
..debug = true,
);
await _run();
exit(0);
}, (exception, stackTrace) async {
await Sentry.close();
stderr.writeln(exception.toString());
exit(-1);
});
}
Future<void> _run() async {
final database = ExampleDatabase(
NativeDatabase.memory().addSentry(),
);
final transaction = Sentry.startTransaction(
'example-transaction',
'db',
bindToScope: true,
);
try {
await database
.into(database.products)
.insert(ProductsCompanion.insert(title: 'My product'));
final allProducts = await database.select(database.products).get();
print('Products in database: $allProducts');
transaction.status = const SpanStatus.ok();
} catch (exception) {
transaction.throwable = exception;
transaction.status = const SpanStatus.internalError();
rethrow;
} finally {
await transaction.finish();
}
}
更多关于Flutter错误追踪与日志管理插件drift_sentry的使用的实战教程也可以访问 https://www.itying.com/category-92-b0.html
1 回复