Quick start
Introduction
This document mainly introduces the installation and use of Baidu AI Cloud log service Android SDK.
This document assumes that you have enabled the Baidu AI Cloud log service.
If you haven't enabled or don't understand the log service, please log on to the Log Service Product Homepage for more help.
Environmental requirements
- Android system version: 2.3 and above
- It is required to register a Baidu AI Cloud user account and open the log service
Install SDK package
The Android SDK for the log service depends on OKhttp and Gson.
Installation steps
- Find the Baidu log service from the client SDK list of Baidu AI Cloud Developer Center and download the SDK compressed package.
- Extract the archive to get the jar package. The jar package should include three files: bls-android-sdk-1.X.X.jar, okhttp-3.x.x.jar, and gson-2.x.x.jar.
- Move the jar package to the lib (or libs) directory of your Android project.
- For Eclipse: Right-click on your project, go to Properties > Java Build Path > Add JARs, and import the copied jar package. For Android Studio: Right-click on your project, select Open Module Settings, navigate to Module > Dependencies > +, select File Dependency, then choose the copied jar package from the lib (or libs) directory to complete the import.
Configure permissions
Below are the Android permissions required by the log service Android SDK. Make sure these permissions are configured in your project's AndroidManifest.xml file; otherwise, the SDK will not function correctly.
1<uses-permission android:name="android.permission.INTERNET"/>
2<uses-permission android:name="android.permission.READ_PHONE_STATE"/>
3<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>
4<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
5<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
SDK directory structure
1com.baidubce
2 ├── auth //BCE signature classes
3 ├── http //BCE HTTP communication classes
4 ├── internal //SDK internal classes
5 ├── model //BCE common model classes
6 ├── services
7 │ └── bls
8 │ ├── request //Internal BLS models, such as Request or Response
9 │ ├── BlsClient.class / //BLS client entry class
10 │ └── BlsClientConfiguration.class //Configuration for BLS-specific HttpClient
11 ├── util //BCE common utilities
12 ├── BceClientConfiguration.class // Configuration for BCE HttpClient
13 ├── BceClientException.class // BCE client exception class
14 ├── BceServiceException.class // Exception class after BCE server interaction
15 ├── ErrorCode.class // BCE common error codes
16 └── Region.class // Regions where BCE provides services
Quick Start
The basic process for uploading logs is demonstrated below.
- Initialize BlsClient; refer to the init () method in MainActivity for the method. (Available endpoints can be viewed in [API Reference-Service Domain Name](BLS/Development Guide/API Reference/Service domain.md).)
1 //All APIs complete request signing through AK (Access Key ID)/SK (Secret Access Key) to pass server authentication and authorization
2 String ak = "***";
3 String sk = "***";
4 // Set authentication
5 DefaultBceCredentials stsCredentials = new DefaultBceCredentials(ak, sk);
6 // Configuration information
7 LogClientConfiguration blsConfig = new LogClientConfiguration();
8 blsConfig.setCredentials(stsCredentials);
9 // Set endpoint
10 blsConfig.setEndpoint(BLS_ENDPOINT);
11 // Set the maximum number of retries and the delay time of each retry
12 blsConfig.setRetryPolicy(new DefaultRetryPolicy(RETRY_MAX, RETRY_DELAY));
13 // Set timeout duration
14 blsConfig.withSocketTimeoutInMillis(DEFAULT_TIMEOUT_IN_MILLIS);
15 // Initialize a client
16 logClient = new BlsClient(blsConfig);
-
Initialize the log and then upload it using the client. Log records can be in TEXT or JSON format. For TEXT log records, the logs are not processed; for JSON log records, only the first-level fields are automatically detected (nested fields are not currently supported).
- Upload TEXT logs
JAVA1 //logstore name,set by users 2 String logStoreName = createdLogStoreName; 3 //logstream name,set by users 4 String logStreamName = Settings.System.getString(MainActivity.this.getContentResolver(),Settings.System.ANDROID_ID); 5 // Set log structure 6 List<LogRecord> logRecordList = new ArrayList<LogRecord>(); 7 LogRecord record = new LogRecord(); 8 record.setMessage("this is log."); 9 record.setTimestamp(System.currentTimeMillis()); 10 logRecordList.add(record); 11 // Upload log request 12 PushLogRequest pushLogRequest = new PushLogRequest(logStoreName, logStreamName, logRecordList); 13 PushLogResponse response = logClient.pushLog(pushLogRequest); 14 boolean isSuccess = response.getHttpResponse().getStatusCode() == StatusCodes.HTTP_OK; 15 Log.e("main", "push log result: " + isSuccess);- Upload JSON logs
JAVA1 //logstore name,set by users 2 String logStoreName = createdLogStoreName; 3 //logstream name,set by users 4 String logStreamName = Settings.System.getString(MainActivity.this.getContentResolver(),Settings.System.ANDROID_ID); 5 // Set log structure 6 List<LogRecord> logRecordList = new ArrayList<LogRecord>(); 7 LogRecord record = new LogRecord(); 8 record.setMessage("{\"level\":\"info\", \"status\": 200, \"cost\": 304.87}"); 9 record.setTimestamp(System.currentTimeMillis()); 10 logRecordList.add(record); 11 // Upload log request 12 PushLogRequest pushLogRequest = new PushLogRequest(logStoreName, logStreamName, "JSON", logRecordList); 13 PushLogResponse response = logClient.pushLog(pushLogRequest); 14 boolean isSuccess = response.getHttpResponse().getStatusCode() == StatusCodes.HTTP_OK; 15 Log.e("main", "push log result: " + isSuccess);- To upload specific fields processed from the log, along with the raw log text, use the JSON format. The raw log text should be included as a value for the key "@raw" in the JSON object. When processing "@raw," BLS will interpret its content as the raw log text.
JAVA1 //logstore name,set by users 2 String logStoreName = createdLogStoreName; 3 //logstream name,set by users 4 String logStreamName = Settings.System.getString(MainActivity.this.getContentResolver(),Settings.System.ANDROID_ID); 5 // Set log structure 6 List<LogRecord> logRecordList = new ArrayList<LogRecord>(); 7 LogRecord record = new LogRecord(); 8 record.setMessage("{\"@raw\":\"info 200 304.87ms this is log.\", \"level\":\"info\", \"status\": 200, \"cost\": 304.87}"); 9 record.setTimestamp(System.currentTimeMillis()); 10 logRecordList.add(record); 11 // Upload log request 12 PushLogRequest pushLogRequest = new PushLogRequest(logStoreName, logStreamName, "JSON", logRecordList); 13 PushLogResponse response = logClient.pushLog(pushLogRequest); 14 boolean isSuccess = response.getHttpResponse().getStatusCode() == StatusCodes.HTTP_OK; 15 Log.e("main", "push log result: " + isSuccess);
For the specific usage of API used by SDK, please refer to: API Reference-Overview
