tencent cloud

Cloud Log Service

Flutter SDKログアップロード

ダウンロード
フォーカスモード
フォントサイズ
最終更新日: 2026-10-12 15:06:43
AI翻訳
本文では、CLSのFlutter SDKを使用してログアップロードを迅速に実行する方法を説明します。

前提条件

TencentCloud APIキー情報であるaccessKeyとaccessSecretを作成して取得します。キー情報の取得についてはAPIキー管理にアクセスしてください。
また、キーに関連付けられたアカウントに、対応するSDKログアップロード権限が付与されていることを確認してください。
Flutter環境がインストール済みです。詳細についてはInstall Flutterを参照してください。

Flutter SDKのインストール

1. Flutterプロジェクトを作成します。
2. プロジェクトのルートディレクトリで以下のコマンドを実行して依存関係を追加します。
flutter pub add tencentcloud_cls_sdk_dart
3. インストール完了後、DartファイルにCLSモジュールをインポートします。
import 'package:tencentcloud_cls_sdk_dart/tencentcloud_cls_sdk_dart.dart';

リクエストパラメータ

変数
タイプ
必須/任意
説明
host
String
はい
地域情報です。入力時は、利用可能なリージョンのAPIアップロードログタブに記載されているドメインをご参照ください。
accessKey
String
はい
TencentCloud APIキー情報です。キー情報の取得は、APIキー管理で行ってください。また、キーに関連付けられたアカウントに、対応するSDKログアップロード権限が付与されていることを確認してください。
accessSecret
String
はい
TencentCloud APIキー情報です。キー情報の取得は、APIキー管理で行ってください。また、キーに関連付けられたアカウントに、対応するSDKログアップロード権限が付与されていることを確認してください。
topicId
String
はい
日志主题的 ID 信息。
accessToken
String
いいえ
一時キーのtokenです。一時キーを使用する場合は入力できます。

ログアップロードのサンプルコード

お客様のFlutterプロジェクトでは、以下のサンプルコードを使用してログアップロード機能を実装できます。サンプルコードは以下の通りです。
コード内でTencentCloud 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),
)),
)),
],
);
}

}

おわりに

上記の手順により、Tencent Cloud CLSのFlutter SDKを使用してログのアップロード操作を迅速に完了できます。問題が発生した場合は、お問い合わせください。


ヘルプとサポート

この記事はお役に立ちましたか?

フィードバック