Collect and send logs from Flutter applications to Simple Log Service (SLS) with the SLS Flutter SDK. The SDK supports Android 4.0 or later and iOS 10.0 or later, with features including resumable upload, dynamic configuration, and log callbacks.
Release notes
The SLS Flutter SDK is published on the official Dart package repository. For more information, see Aliyun Log Flutter Release.
Sample code
For complete working examples, see Aliyun Log Flutter SDK Example.
Prerequisites
Install the Flutter development environment.
The SLS Flutter SDK supports Android 4.0 or later and iOS 10.0 or later.
For iOS builds, you must add the following to your Podfile:
source 'https://github.com/CocoaPods/Specs.git' source 'https://github.com/aliyun-sls/Specs.git' # If you are using a repository in Chinese mainland, you must also add this line. source 'https://gitee.com/aliyun-sls/Specs.git'
Procedure
Step 1: Install the SDK
Add the SLS Flutter SDK module. From the root of your project, run the following command:
flutter pub add aliyun_log_dart_sdkAfter the process is complete, the following information is automatically added to the project's
pubspec.yamlfile, andflutter pub getis implicitly run.dependencies: aliyun_log_dart_sdk: ^1.0.0 // For more version information, see the Flutter SDK overview.Import the package in your Dart file.
import 'package:aliyun_log_dart_sdk/aliyun_log_dart_sdk.dart';
Step 2: Initialize the SDK
The following code demonstrates basic initialization. For advanced configuration options such as log package size and resumable upload, see Configuration parameters.
import 'package:aliyun_log_dart_sdk/aliyun_log_dart_sdk.dart';
AliyunLogDartSdk? _aliyunLogSdk;
void _initProducer() async {
// Set the endpoint, project name, and Logstore name.
LogProducerConfiguration configuration = LogProducerConfiguration(
endpoint: 'your endpoint', project: 'your project', logstore: 'your logstore'
);
// An Alibaba Cloud AccessKey. Using an AccessKey pair is risky as it grants full access to your resources. We strongly recommend using a RAM User for API calls.
configuration.accessKeyId = 'your access key id';
configuration.accessKeySecret = 'your access key secret';
configuration.securityToken = 'your access key token'; // Required only when using a temporary AccessKey from Security Token Service (STS).
_aliyunLogSdk = AliyunLogDartSdk();
LogProducerResult result = await _aliyunLogSdk!.initProducer(configuration);
}Step 3: Send logs
Call the addLog method to send custom business logs:
LogProducerResult code = await _aliyunLogSdk!.addLog({
'str': 'str value',
'int': 12,
'double': 12.12,
'boolean': true,
'map': {'key': 'value', 'inntt': 3333},
'array': ['a1', 'a2'],
'null': null,
'content': 'Chinese content'
});Logs are sent successfully only if code == LogProducerResult.ok. Otherwise, an error code is returned. For more information, see Error codes.
Step 4: Configure obfuscation rules (Android)
If your Flutter project has obfuscation rules enabled (these rules are enabled by default in Flutter v1.16.2 and later), you also need to add the following rules to your project's obfuscation configuration file. Otherwise, the Android project may not run correctly. iOS projects are not affected by this rule.
-keep class com.aliyun.sls.android.producer.* { *; }
-keep interface com.aliyun.sls.android.producer.* { *; }Dynamic configuration
The following parameters can be updated at runtime: Endpoint, Project, Logstore, and AccessKey.
Update the Endpoint, Project, and Logstore.
await _aliyunLogSdk!.setEndpoint('new-endpoint'); await _aliyunLogSdk!.setProject('new-project-name'); await _aliyunLogSdk!.setLogstore('new-logstore-name');Update the AccessKey.
// The securityToken is optional. It is required only when the AccessKey is obtained through Security Token Service (STS). await _aliyunLogSdk!.setAccessKey('your accesskey id', 'your accesskey secret', securityToken: 'your accesskey token');Update source, topic, and tag parameters.
await _aliyunLogSdk!.setSource('flutter'); await _aliyunLogSdk!.setTopic('flutter-test'); await _aliyunLogSdk!.addTag('tag1', 'value1'); await _aliyunLogSdk!.addTag('tag2', 'value2');Update other parameters.
ImportantAliyunLogDartSdk.updateConfiguration()does not support updating resumable upload parameters at runtime.LogProducerConfiguration configuration = LogProducerConfiguration(); configuration.dropDelayLog = true; configuration.dropUnauthorizedLog = true; // Other parameters of the LogProducerConfiguration class can also be set this way. await _aliyunLogSdk!.updateConfiguration(configuration);
Set a log callback
Set a callback for log-sending operations. The callback triggers on both success and failure, enabling SDK status monitoring and configuration updates.
_aliyunLogSdk!.setLogCallback((resultCode, errorMessage, logBytes, compressedBytes) {
// The parameters are invalid. You need to update the configuration.
if (LogProducerResult.parametersInvalid == resultCode) {
// For example, update the Endpoint.
_aliyunLogSdk!.setEndpoint('your endpoint');
// A missing or incorrect AccessKey also triggers parametersInvalid.
_aliyunLogSdk!.setAccessKey('your access key id', 'your access key secret', securityToken: 'your token');
}
// The authorization has expired. You need to update the AccessKey.
if (LogProducerResult.sendUnauthorized == resultCode) {
_aliyunLogSdk!.setAccessKey('your access key id', 'your access key secret', securityToken: 'your token');
}
});Enable resumable upload
To enable the resumable upload feature, you must enable it when you initialize AliyunLogDartSdk. You cannot dynamically modify the resumable upload configuration after the SDK is initialized.
Configure resumable upload during initialization:
configuration.persistent = true; // Enable resumable upload.
configuration.persistentFilePath = 'flutter/demo'; // The directory to cache binlogs.
configuration.persistentForceFlush = false; // Disable force flush. Keep this disabled, as enabling it can affect performance.
configuration.persistentMaxFileCount = 10; // The maximum number of cached files. Default: 10.
configuration.persistentMaxFileSize = 1024 * 1024; // The maximum size of a single cache file, in bytes. Default: 1024 * 1024.
configuration.persistentMaxLogCount = 64 * 1024; // The maximum number of cached logs. Default: 64 * 1024.
_aliyunLogSdk = AliyunLogDartSdk();
LogProducerResult result = await _aliyunLogSdk!.initProducer(configuration);Parameters
The parameters supported by the LogProducerConfiguration class are listed in the following table.
Parameter | Type | Description |
endpoint | string | The endpoint of the region where the project resides. Example: |
project | string | The name of the Project. For more information, see Project. |
logstore | string | The name of the Logstore. For more information, see Logstore. |
accessKeyId | string | Your AccessKey ID. For more information, see AccessKey pair. |
accessKeySecret | string | Your AccessKey Secret. For more information, see AccessKey pair. |
securityToken | string | The security token required for Security Token Service (STS) authentication. For more information, see AssumeRole. |
debuggable | bool | Specifies whether to enable Debug Mode. Default: false. Enable this mode when troubleshooting log collection issues. |
maxBufferLimit | int | The maximum memory that the SDK can use for caching. Unit: bytes. Default: 64 * 1024 * 1024. |
connectTimeout | int | The network connection timeout period, in seconds. Default: 10. Do not change this value unless necessary. |
sendTimeout | int | The timeout period for sending data, in seconds. Default: 15. Do not change this value unless necessary. |
ntpTimeOffset | int | The difference between the device time and the standard time, in seconds. Default: 0. Do not change this value unless necessary, as the SDK automatically corrects the time. |
maxLogDelayTime | int | The maximum time difference allowed between a log's timestamp and the local device time. Unit: seconds. Default: 7 * 24 * 3600. If this value is exceeded, the log is processed based on the dropDelayLog parameter. Do not change this value unless necessary. |
dropDelayLog | bool | Specifies the policy for handling logs that exceed maxLogDelayTime. The default value is false, which means that logs are not discarded. field is reset to the current time. |
dropUnauthorizedLog | bool | Specifies whether to drop logs that fail authentication. Default: false. |
source | string |
field, which indicates the log source. Default: Android or iOS. |
topic | string |
field, which indicates the Log Topic. No default value. |
Tags (via addTag() method) | string |
The value of the field, which is the tag metadata. This field has no default value. You must set this value by calling method. |
packetLogBytes | int | The size of each log package to be sent. Valid values: 1 to 5,242,880. Unit: bytes. Default: 1024 * 1024. |
packetLogCount | int | The maximum number of logs in each package. Valid values: 1 to 4,096. Default: 1,024. |
packetTimeout | int | The timeout period for a log package. If the timeout is reached, the package is sent immediately. Unit: milliseconds. Default: 3000. |
persistent | boolean | Specifies whether to enable the resumable upload feature. Default: false. Enable this feature to prevent data loss. |
persistentForceFlush | boolean | Specifies whether to forcibly flush the cache every time addLog is called.
Enable this feature only in high-reliability scenarios. |
persistentFilePath | string | The path to store cached binlogs for resumable upload. Default: empty string. Important The specified path must exist. Each |
persistentMaxFileCount | int | The maximum number of persistent files. Default: 10. |
persistentMaxFileSize | int | The maximum size of each persistent file, in bytes. Default: 1024*1024. |
persistentMaxLogCount | int | The maximum number of logs that can be cached. Default: 64*1024. |
Error codes
Error code | Description | Solution |
invalid | The SDK is uninitialized or has been destroyed. | Verify that the SDK was initialized correctly and that the |
writeError | A write error occurred, likely because the Project's write traffic quota was exceeded. | Adjust the write traffic quota for the Project. For more information, see Adjust resource quotas. |
dropError | The cache is full. | Refer to the parameter descriptions for the |
sendNetworkError | Network error. | Check your network connection and retry. |
sendQuotaError | The write traffic of the Project has reached its limit. | Adjust the write traffic quota for the Project. For more information, see Adjust resource quotas. |
sendUnauthorized | The AccessKey has expired, is invalid, or its permission policy is incorrect. | Verify that your AccessKey is valid and that the associated RAM User has the required permissions on SLS resources. For more information, see Grant permissions to a RAM user. |
sendServerError | Server error. | Retry the operation. |
sendDiscardError | The data was discarded. This is usually caused by time skew between the device and the server. | The SDK automatically resends the data. No action is required. |
sendTimeError | The device time is not synchronized with the server time. | The SDK automatically resolves this issue. No action is required. |
sendExitBuffered | Cached data was not sent before the SDK was destroyed. | Enable resumable upload to prevent data loss. |
parametersInvalid | Invalid SDK initialization parameters. | Check the configurations for AccessKey, Endpoint, Project, and Logstore. |
persistentError | Failed to write cached data to the disk. | Verify that the cache file path is correctly configured, that the cache is not full, and that sufficient disk space is available. |
unknown | Unknown error. | Retry the operation. |