Flutter SDK 上传日志

最近更新时间:2024-12-16 10:02:23

我的收藏
本文介绍如何快速使用日志服务的Flutter SDK 实现日志上传的操作。

前提条件

创建并获取云 API 密钥信息 accessKey 和 accessSecret,密钥信息获取请前往 API 密钥管理
并请确保密钥关联的账号具有相应的 SDK 上传日志权限
已安装 Flutter 环境。更多信息请参见 Install Flutter

安装 Flutter SDK

1. 创建 Flutter 项目。
2. 在项目的根目录下执行如下命令添加依赖。
flutter pub add tencentcloud_cls_sdk_dart
3. 安装完成后,在您的 Dart 文件中导入日志服务模块。
import 'package:tencentcloud_cls_sdk_dart/tencentcloud_cls_sdk_dart.dart';

请求参数

变量
类型
是否必填
说明
host
String
地域信息,填写请参见 可用地域 中 API 上传日志 Tab 中的域名。
accessKey
String
云 API 密钥信息,密钥信息获取请前往 API 密钥管理。并请确保密钥关联的账号具有相应的 SDK 上传日志权限
accessSecret
String
云 API 密钥信息,密钥信息获取请前往 API 密钥管理。并请确保密钥关联的账号具有相应的 SDK 上传日志权限
topicId
String
日志主题的 ID 信息。
accessToken
String
临时密钥的 token,如使用临时密钥可填入。

日志上传示例代码

在您的 Flutter 项目中,可以使用如下的示例代码实现日志上传的能力,示例代码如下所示。
在代码中直接明文使用云 API 密钥(accessKey、accessSecret)风险较高,为确保安全性,建议使用 临时密钥 进行鉴权。
import 'package:flutter/material.dart';
import 'package:tencentcloud_cls_sdk_dart/tencentcloud_cls_sdk_dart.dart';

Future<void> main() async {
await RustLib.init();

runApp(const MyApp());
}

class MyApp extends StatefulWidget {
const MyApp({super.key});

@override
State<MyApp> createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> {
LogProducer? _logProducer;
String _consoleText = '';


@override
void initState() {
super.initState();

if (!mounted) return;
print("application started.");
}

void print(String message) {
setState(() {
_consoleText += message;
_consoleText += '\\n';
});
}

void _initProducer() async {
_logProducer = LogProducer(
topicId: '',
accessKey: '',
accessSecret: '',
accessToken: '',
host: 'ap-XXXXXX.cls.tencentcs.com',
waitSendLogQueue: 1000, addLogQueue: 1000, lingerMs: 2000);

print('init producer client success');
}

void _callback() async {
if (!check()) {
return;
}
_logProducer?.setCallback(dartCallback: (topicId, requestId, status, errorMessage) => print('$requestId!, $status, $topicId, $errorMessage'));
print('init callback success');
}

void _sendLog() async {
if (!check()) {
return;
}
_logProducer?.addLog(log: { 'hello': 'world' });
}

bool check() {
if (null == _logProducer) {
print('you should init producer first.');
return false;
}
return true;
}

@override
Widget build(BuildContext context) {
Color color = Theme.of(context).primaryColor;

return MaterialApp(
home: Scaffold(
appBar: AppBar(title: const Text('tencent cloud cls flutter sdk demo')),
body: Column(
children: [
_buildConsoleText(),
_buildButton(color, 'init', _initProducer),
_buildButton(color, 'set callback', _callback),
_buildButton(color, 'send', _sendLog),
],
),
),
);
}

Widget _buildConsoleText() {
return Row(mainAxisSize: MainAxisSize.max, mainAxisAlignment: MainAxisAlignment.start, children: [
Expanded(
flex: 1,
child: Container(
margin: const EdgeInsets.only(bottom: 18),
padding: const EdgeInsets.all(6),
height: 140,
decoration: BoxDecoration(
border: Border.all(
color: Colors.grey,
width: 0.67,
),
color: Colors.black),
child: SingleChildScrollView(
scrollDirection: Axis.vertical,
child: Text(
_consoleText,
style: const TextStyle(
color: Colors.white,
fontSize: 12,
letterSpacing: 2,
wordSpacing: 2,
fontFeatures: [FontFeature.tabularFigures()]),
),
),
))
]);
}

Widget _buildButton(Color color, String label, VoidCallback? onPressed) {
return Row(
mainAxisSize: MainAxisSize.max,
mainAxisAlignment: MainAxisAlignment.center,
children: [
Expanded(
flex: 1,
child: Container(
margin: const EdgeInsets.only(left: 16, top: 8, right: 16),
child: TextButton(
onPressed: onPressed,
style: ButtonStyle(
shape: WidgetStateProperty.all(RoundedRectangleBorder(borderRadius: BorderRadius.circular(12))),
side: WidgetStateProperty.all(BorderSide(color: color, width: 0.67)),
backgroundColor: WidgetStateProperty.all(Colors.transparent),
padding:
WidgetStateProperty.all(const EdgeInsets.only(left: 12, top: 6, right: 12, bottom: 6))),
child: Text(
label,
style: TextStyle(fontSize: 22, fontWeight: FontWeight.w400, color: color),
)),
)),
],
);
}

}

结语

通过以上步骤,您可以快速使用腾讯云 CLS 的 Flutter SDK 完成日志的上传操作。如遇到任何问题,请 联系我们 获取帮助。