百度智能云

All Product Document

          Log Service

          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

          1. Find the Baidu log service from the client SDK list of Baidu AI Cloud Developer Center and download the SDK compressed package.
          2. 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.
          3. Move the jar package to the lib (or libs) directory of your Android project.
          4. 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.

          <uses-permission android:name="android.permission.INTERNET"/>
          <uses-permission android:name="android.permission.READ_PHONE_STATE"/>
          <uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>
          <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
          <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>

          SDK directory structure

          com.baidubce
           ├── auth                                        //BCE signature classes
           ├── http                                        //BCE HTTP communication classes
           ├── internal                                    //SDK internal classes
           ├── model                                       //BCE common model classes
                 ├── services
                 │       └── bls                                
           │           ├── request                         //Internal BLS models, such as Request or Response
           │           ├── BlsClient.class                 / //BLS client entry class
           │           └── BlsClientConfiguration.class    //Configuration for BLS-specific HttpClient
           ├── util                                        //BCE common utilities
           ├── BceClientConfiguration.class                 // Configuration for BCE HttpClient
           ├── BceClientException.class                    // BCE client exception class
           ├── BceServiceException.class                   // Exception class after BCE server interaction
           ├── ErrorCode.class                           // BCE common error codes
           └── Region.class                              // Regions where BCE provides services

          Quick Start

          The basic process for uploading logs is demonstrated below.

          1. 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).)
              //All APIs complete request signing through AK (Access Key ID)/SK (Secret Access Key) to pass server authentication and authorization
              String ak = "***";
              String sk = "***";
           // Set authentication
              DefaultBceCredentials stsCredentials = new DefaultBceCredentials(ak, sk);
           // Configuration information
              LogClientConfiguration blsConfig = new LogClientConfiguration();
              blsConfig.setCredentials(stsCredentials);
           // Set endpoint
              blsConfig.setEndpoint(BLS_ENDPOINT);
           // Set the maximum number of retries and the delay time of each retry
              blsConfig.setRetryPolicy(new DefaultRetryPolicy(RETRY_MAX, RETRY_DELAY));
           // Set timeout duration
              blsConfig.withSocketTimeoutInMillis(DEFAULT_TIMEOUT_IN_MILLIS);
           // Initialize a client
              logClient = new BlsClient(blsConfig);
          1. 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).

            1. Upload TEXT logs
                //logstore name,set by users
                String logStoreName = createdLogStoreName;
             //logstream name,set by users
                String logStreamName = Settings.System.getString(MainActivity.this.getContentResolver(),Settings.System.ANDROID_ID);
             // Set log structure
                List<LogRecord> logRecordList = new ArrayList<LogRecord>();
                LogRecord record = new LogRecord();
                record.setMessage("this is log.");
                record.setTimestamp(System.currentTimeMillis());
                logRecordList.add(record);
             // Upload log request
                PushLogRequest pushLogRequest = new PushLogRequest(logStoreName, logStreamName, logRecordList);
                PushLogResponse response = logClient.pushLog(pushLogRequest);
                boolean isSuccess = response.getHttpResponse().getStatusCode() == StatusCodes.HTTP_OK;
                Log.e("main", "push log result: " + isSuccess);
            1. Upload JSON logs
                //logstore name,set by users
                String logStoreName = createdLogStoreName;
             //logstream name,set by users
                String logStreamName = Settings.System.getString(MainActivity.this.getContentResolver(),Settings.System.ANDROID_ID);
             // Set log structure
                List<LogRecord> logRecordList = new ArrayList<LogRecord>();
                LogRecord record = new LogRecord();
                record.setMessage("{\"level\":\"info\", \"status\": 200, \"cost\": 304.87}");
                record.setTimestamp(System.currentTimeMillis());
                logRecordList.add(record);
             // Upload log request
                PushLogRequest pushLogRequest = new PushLogRequest(logStoreName, logStreamName, "JSON", logRecordList);
                PushLogResponse response = logClient.pushLog(pushLogRequest);
                boolean isSuccess = response.getHttpResponse().getStatusCode() == StatusCodes.HTTP_OK;
                Log.e("main", "push log result: " + isSuccess);
            1. 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.
                //logstore name,set by users
                String logStoreName = createdLogStoreName;
             //logstream name,set by users
                String logStreamName = Settings.System.getString(MainActivity.this.getContentResolver(),Settings.System.ANDROID_ID);
             // Set log structure
                List<LogRecord> logRecordList = new ArrayList<LogRecord>();
                LogRecord record = new LogRecord();
                record.setMessage("{\"@raw\":\"info 200 304.87ms this is log.\", \"level\":\"info\", \"status\": 200, \"cost\": 304.87}");
                record.setTimestamp(System.currentTimeMillis());
                logRecordList.add(record);
             // Upload log request
                PushLogRequest pushLogRequest = new PushLogRequest(logStoreName, logStreamName, "JSON", logRecordList);
                PushLogResponse response = logClient.pushLog(pushLogRequest);
                boolean isSuccess = response.getHttpResponse().getStatusCode() == StatusCodes.HTTP_OK;
                Log.e("main", "push log result: " + isSuccess);

          For the specific usage of API used by SDK, please refer to: API Reference-Overview

          Previous
          Overview
          Next
          Version Release Records