基础配置 API

本文档介绍移动监控 Flutter SDK 提供的基础 API,包括启动 SDK 、更新用户 ID 和昵称、设置自定义维度、调整日志级别

1. 启动SDK

使用配置选项ApmOptions启动 SDK。

接口定义

static Future<void> start(
  ApmOptions options, {
    Future<void> Function()? appRunner,
  })

ApmOptions参数说明

参数

类型

是否必填

说明

appKey

String

应用监控服务的 appKey

appSecret

String

应用监控服务的 appSecret

appRsaSecret

String

应用监控服务的 appRsaSecret

components

Set<SdkComponent>

应用监控 SDK加载的业务组件

崩溃分析:SdkComponent.crashAnalysis

userId

String

用户ID,建议保持唯一,字符串最大长度不超过128

userNick

String

用户昵称,字符串最大长度不超过128

channel

String

渠道标识

代码示例

Future<void> main() async {
  const options = ApmOptions(
    appKey: 'Your iOS AppKey',
    appSecret: 'Your iOS AppSecret',
    appRsaSecret: 'Your iOS AppRsaSecret',
    // 以下是可选配置项(按需替换)
    channel: 'Your Channel',
    userNick: 'Your UserNick',
    userId: 'Your UserId',
  );
  await Apm.start(options);
  runApp(const YourApp());
}

2. runGuarded

接口定义

 static Future<void> runGuarded(Future<void> Function() runner)

代码示例

Future<void> main() async {
  await Apm.runGuarded(() async {
    await Apm.start(
      const ApmOptions(
        appKey: '<app-key>',
        appSecret: '<app-secret>',
        appRsaSecret: '<rsa-secret>',
      ),
    );
    runApp(const MyApp());
  });
}

3. 获取 Apm 启动状态

接口定义

static bool get isStarted

代码示例

bool apmStarted = Apm.isStarted;

4. 设置用户ID

接口定义

static Future<void> setUserId(String userId) async

参数说明

参数

类型

是否必填

说明

userId

String

用户ID,字符串最大长度不超过128

代码示例

await Apm.setUserId("Your UserId");

5. 设置用户昵称

接口定义

 static Future<void> setUserNick(String userNick) async

参数说明

参数

类型

是否必填

说明

userNick

String

用户昵称,字符串最大长度不超过128

代码示例

await Apm.setUserNick("Your UserNick");

6. 自定义维度

通过以下接口可以自定义维度,SDK最多记录64个自定义维度。

接口定义

  static Future<void> setCustomProperty(String key, Object value) async
  static Future<void> setCustomProperties(Map<String, Object> properties) async  

参数说明

参数

类型

是否必填

长度范围

说明

key

String

1~128

自定义维度的键

value

Object

1~128

自定义维度的值,转换为字符串

代码示例

await Apm.setCustomProperty('sessionId', 'abc123');

await Apm.setCustomProperties({
  'userLevel': 3,
  'featureAEnabled': true,
});

7. 设置 SDK 日志级别

接口定义

class ApmConfiguration {
  static LogLevel get loggerLevel {}
  static Future<void> setLoggerLevel(LogLevel level) async {}
}

enum LogLevel { error, warning, notice, info, debug }

参数说明

参数

类型

是否必填

说明

LoggerLevel

enum

SDK 日志打印级别,默认是notice级别

  • error : 错误级别

  • warning : 警告级别

  • notice : 通知级别

  • info : 信息级别

  • debug : 调试级别

代码示例

await ApmConfiguration.setLoggerLevel(LogLevel.debug);