Flutter错误报告插件vigil_reporter的使用

Flutter 错误报告插件 vigil_reporter 的使用

Dart Vigil Reporter

pub package
likes
popularity
pub points

Vigil Reporter for Dart。用于与 Vigil 微服务状态页面一起使用。

Vigil Reporter 用于主动向 Vigil 提交健康信息。最佳监控方式是通过应用探针,这些探针能够报告详细的系统信息,如 CPU 和 RAM 负载。这使得 Vigil 能够显示应用程序主机系统是否处于高负载状态。

🇫🇷 在法国南特制作。

使用者是谁?

图标 公司名称
Pikomit Pikomit

👋 如果你正在使用 vigil_reporter 并希望被列出来?请发送邮件到 go.jroussel@gmail.com

如何安装?

pubspec.yaml 文件中包含 vigil_reporter 依赖项。

dependencies:
  vigil_reporter: ^1.2.1

或者,你也可以运行以下命令:

dart pub add vigil_reporter

如何使用?

1. 创建 reporter

vigil_reporter 可以这样实例化:

import 'package:vigil_reporter/vigil_reporter.dart';

final vigilReporter = VigilReporter(
  url: 'https://status.example.com',
  token: 'YOUR_TOKEN_SECRET',
  probeId: 'relay',
  nodeId: 'socket-client',
  replicaId: '192.168.1.10',
  interval: 30,
  logger: VigilLogger(),
);

2. 关闭 reporter

如果你需要关闭一个活动的 reporter,可以使用 end({bool flush = false}) 方法来解除绑定。

vigilReporter.end(flush: false)
  .then(() {
    // 处理结束操作
  }).catchError(() {
    // 处理错误
  });

Vigil 是什么?

ℹ️ 想知道 Vigil 是什么吗? 查看 valeriansaliou/vigil


完整示例 Demo

以下是一个完整的示例,展示了如何使用 vigil_reporter 插件。

import 'dart:io';

import 'package:vigil_reporter/vigil_reporter.dart';

void main() {
  final vigilReporter = VigilReporter(
    url: 'https://status.example.com',
    token: 'YOUR_TOKEN_SECRET',
    probeId: 'relay',
    nodeId: 'socket-client',
    replicaId: '192.168.1.10',
    interval: 30,
    logger: VigilLogger(),
  );

  // 监听 Ctrl+C 信号并处理退出
  ProcessSignal.sigint.watch().listen((event) async {
    await vigilReporter.end(flush: true);
    exit(0);
  });
}

更多关于Flutter错误报告插件vigil_reporter的使用的实战教程也可以访问 https://www.itying.com/category-92-b0.html

1 回复

更多关于Flutter错误报告插件vigil_reporter的使用的实战系列教程也可以访问 https://www.itying.com/category-92-b0.html


当然,下面是一个关于如何在Flutter项目中集成和使用vigil_reporter错误报告插件的示例代码。vigil_reporter是一个用于捕获和报告Flutter应用中的错误的插件。

1. 添加依赖

首先,在你的pubspec.yaml文件中添加vigil_reporter依赖:

dependencies:
  flutter:
    sdk: flutter
  vigil_reporter: ^最新版本号  # 请替换为实际的最新版本号

然后运行flutter pub get来安装依赖。

2. 初始化插件

在你的Flutter应用的入口文件(通常是main.dart)中初始化vigil_reporter

import 'package:flutter/material.dart';
import 'package:vigil_reporter/vigil_reporter.dart';

void main() {
  WidgetsFlutterBinding.ensureInitialized();
  
  // 初始化 VigilReporter
  VigilReporter.init(
    apiKey: '你的API_KEY', // 替换为你的Vigil API密钥
    enableConsoleLogging: true, // 是否在控制台打印日志
    enableInReleaseMode: true, // 是否在发布模式下启用
  );

  runApp(MyApp());
}

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: MyHomePage(),
    );
  }
}

class MyHomePage extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('Flutter Demo Home Page'),
      ),
      body: Center(
        child: Text('Hello, Vigil Reporter!'),
      ),
      floatingActionButton: FloatingActionButton(
        onPressed: () {
          // 模拟一个错误
          throw Exception('这是一个测试异常');
        },
        tooltip: 'Throw Error',
        child: Icon(Icons.error),
      ),
    );
  }
}

3. 捕获全局错误

为了捕获全局错误,你可以使用Flutter的ErrorWidget.builderZonedState来捕获未处理的异常。

import 'package:flutter/material.dart';
import 'package:flutter/foundation.dart' show ZonedState;
import 'package:vigil_reporter/vigil_reporter.dart';

void main() {
  WidgetsFlutterBinding.ensureInitialized();
  
  VigilReporter.init(
    apiKey: '你的API_KEY',
    enableConsoleLogging: true,
    enableInReleaseMode: true,
  );

  FlutterError.onError = (FlutterErrorDetails details) {
    // 报告Flutter错误
    VigilReporter.reportFlutterError(details);
  };

  runZonedGuarded(() {
    runApp(MyApp());
  }, (error, stackTrace) {
    // 报告Dart错误
    VigilReporter.reportDartError(error, stackTrace);
  });
}

4. 自定义错误报告

你还可以根据需要自定义错误报告,例如添加用户信息或应用上下文。

import 'package:vigil_reporter/vigil_reporter.dart';

// 自定义错误报告
void reportCustomError() {
  Map<String, dynamic> customData = {
    'user_id': '12345',
    'app_version': '1.0.0',
    'additional_info': '这是一个自定义错误信息',
  };

  VigilReporter.reportCustomError(
    message: '自定义错误',
    error: null, // 可以传入具体的Dart错误对象
    stackTrace: null, // 可以传入具体的堆栈跟踪
    customData: customData,
  );
}

5. 调用自定义错误报告

你可以在应用的任何地方调用自定义错误报告函数,例如在一个按钮点击事件中。

class MyHomePage extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('Flutter Demo Home Page'),
      ),
      body: Center(
        child: Text('Hello, Vigil Reporter!'),
      ),
      floatingActionButton: FloatingActionButton(
        onPressed: () {
          reportCustomError();
        },
        tooltip: 'Report Custom Error',
        child: Icon(Icons.report_problem),
      ),
    );
  }
}

这就是如何在Flutter项目中集成和使用vigil_reporter错误报告插件的基本示例。请确保替换示例中的你的API_KEY为你的实际Vigil API密钥。

回到顶部