LTS Java SDK

Log Tank Service (LTS) SDK provides a series of methods for reporting logs in Java language, so that you can directly report logs to LTS in encoding mode.

Prerequisites

  • Before using the LTS SDK, you need to register an HUAWEI CLOUD account and enable the LTS service.
  • Obtain the HUAWEI CLOUD region information to be reported by LTS

Configuration Before Use

Before using the LTS SDK, you need to configure initialization information such as ConnectionConfig. Configure the content as follows:

  1. Determining the region where the LTS service is deployed

    Before using the SDK, determine the region (for example, CN North – Beijing1) to which the current tenant belongs and select the LTS service region that matches the region.

  2. Configuring the access key (AK/SK)

    The log SDK depends on the AK (Access Key) and SK (Secret Access Key) for authentication. Before using the SDK, obtain the AK/SK information.

Procedure

  1. Obtain the HUAWEI CLOUD account AK/SK. For details about the steps, see How to obtain the AK/SK.
  2. Install the Java development environment. The log SDK supports the development environment of JDK1.6 or later.
  3. Download and install the LTS SDK package

    1. Download the LTS SDK package.
    2. Decompress the downloaded package to a specified directory. Note: Decompress the package only.
    3. Add all the decompressed JAR packages to your Java projects.

  4. Create log groups and log streams in LTS.
  5. Start a new Java project.

    public class sdkTest {
    public static void main(String args[]){
        ConnectionConfig config = new ConnectionConfig();
    config.setLogGroup("xxx-xx-xxx-xx-xxxx");// Log group to which the sent log belongs
    config.setLogStream("xx-xxxxx-xxx");// Log stream to which the sent log belongs
    config.setProjectid("xxxxxxxxxxx");// Project ID of the current tenant
    config.setAK("xxxxx");//Access key of the current tenant
    config.setSK("xxxxx");//Secret Access Key of the current tenant
    config.setEndpoint("xx.xx.xx.xxx:xxx");//Destination endpoint to which the logs are sent
    config.setBatchSize(60);//Set the size of logs to be processed in batches
    config.setThreadCount(10);//Set the number of threads that send logs at the same time
    config.setDataQueueSize(500*1000);//Set the length of the log queue when processing logs
    config.setIamEndpoint ("xx.xx.xx.xxxx") ;// IAM authentication endpoint of the current region
    Producer producer = ProducerFactory.getInstance().createProceduer(config);
    LogItem item = new LogItem();
    item.add(System.currentTimeMillis(), "xxxxxxxx");//Add the log logItem. The input parameters are the log generation time and log content.
    producer.sendData(item);//No callback mode is available.
    try {
         producer.sendData(item, new LogCallback(){
          @Override
          public void onCompletion(LogItem item, Exception e){
            if(exception != null){.......}
            else{.......}});
         } catch (Exception e) {
           e.printStackTrace();
    }//Data is sent in callback mode.

Configuration Parameters

Parameter

Description

Method

Example Value

endPoint

Address of the logs reported from the corresponding region.

SetEndPoint

xxx.y.z.xxx:xxx

region

Region where the tenant is located.

setRegion

cn-north-4

ak

Access key of the HUAWEI CLOUD tenant

setAk

xxx-xx-xxx

sk

Secret key of the HUAWEI CLOUD tenant

setSk

xxx-xx-xxx

projectId

Project ID of the current tenant

setProjectId

abcdxxxx

threadCout

Number of concurrent threads for reporting logs

setThreadCout

10 (within 20)

dataQueueSize

Length of the log queue when reporting logs

setDataQueueSize

500000

resultHandQueueSize

Length of the queue of log results

setResultHandQueueSize

500000

batchSize

Number of logs reported at a time

setBatchSize

1000 (The size of a log size* batchSize must be smaller than 2 MB. Otherwise, the log cannot be sent.)

logGroup

Log group to which the reported logs belong.

setLogGroup

aabdsfzx

version

Target log stream of which logs are reported.

setVersion

v1

sendType

Log reporting mode

setSendType

Endpoint

hostIP

IP address of the node where the user service is located.

setHostIP

X.X.X.X

hostName

Name of the node where the user service is located.

setHostName

Host1

How to Obtain

  • Log Group ID: Log in to the LTS console and choose Log Management. IDs under the log group names are log group IDs.

  • Log Stream ID: Click a log group name to go to the log stream list. IDs under the log stream names are log stream IDs.

  • Project ID: For details, see My Credentials.
  • Access key (AK)/secret access key (SK). For details, see My Credentials.
  • The following table lists the region, IAM authentication address, and log reporting address.

    Region

    Name

    IAM Authentication Address

    Log Reporting Address

    CN North-Beijng1

    cn-north-1

    iam.cn-north-1.myhuaweicloud.com

    Contact Huawei engineers to obtain the address.

    CN North-Beijing4

    cn-north-4

    iam.cn-north-4.myhuaweicloud.com

    Contact Huawei engineers to obtain the address.

    South China-Guangzhou1

    cn-south-1

    iam.cn-south-1.myhuaweicloud.com

    Contact Huawei engineers to obtain the address.

    CN East-Shanghai2

    cn-east-2

    iam.cn-east-2.myhuaweicloud.com

    Contact Huawei engineers to obtain the address.