All Products
Search
Document Center

Simple Log Service:Initialize the Simple Log Service SDK for Java

Last Updated:Feb 07, 2025

The Client is a Java SDK client that enables access to Simple Log Service. It offers a variety of methods to create projects, Logstores, write logs, and read logs. To initiate a request with the Simple Log Service SDK for Java, you must initialize a Client instance and adjust its default settings to meet your business needs.

Prerequisites

Procedure

Below are two methods for initialization. Choose the one that suits your requirements:

  • AccessKey pair initialization: This long-term valid pair can be used directly for API calls and is ideal for stable environments where credentials seldom change.

  • STS initialization: Temporary access credentials generated by STS are appropriate for scenarios requiring dynamic and temporary authorization.

Use an AccessKey pair

Initialize the client

 public Client(String endpoint, String  accessKeyId, String accessKeySecret)

Request parameters

Variable

Type

Required

Description

Example

endpoint

String

Yes

Endpoint is the entry point for accessing Alibaba Cloud services. It is usually a URL that specifies the access protocol, hostname, port, and path of the service. The client can use this information to communicate with the service. Simple Log Service supports public endpoints, virtual private cloud (VPC) endpoints, and acceleration endpoints:

  • Public endpoint: When you pull data from the interface where the public domain name of Simple Log Service is located, external network read traffic is generated. For billing information, see Billable items for the pay-by-ingested-data billing method and Billable items for the pay-by-feature billing method. For a specific list, see Endpoint.

  • VPC endpoint: If you access Simple Log Service through other Alibaba Cloud products in the same region as the project, we recommend that you use the VPC endpoint. For more information, see Endpoint.

  • Transfer acceleration domain name: If the business server and Simple Log Service project belong to the Chinese mainland region and the overseas region, respectively, using the public network to transfer data may cause high network latency or unstable transmission. You can choose the acceleration endpoint. For more information, see Manage transfer acceleration.

cn-hangzhou.log.aliyuncs.com

accessKeyId

String

Yes

  • If you use an AccessKey pair to configure the access credential, the AccessKey ID of the Alibaba Cloud account (primary account) and RAM user (sub-account) is used to identify the user. For more information, see Configure access credentials.

    Warning

    The AccessKey pair of an Alibaba Cloud account has full permissions on resources. We recommend that you do not use the AccessKey pair of an Alibaba Cloud account to avoid risks caused by AccessKey pair leaks. We recommend that you use the AccessKey pair of a RAM user that is granted permissions based on the principle of least privilege.

LTAI5tQisap3nb5aDQ******

accessKeySecret

String

Yes

If you use an AccessKey pair to configure the access credential, the AccessKey secret of the Alibaba Cloud account (primary account) and RAM user (sub-account) is used to authenticate the password of the AccessKey ID. For more information, see Configure access credentials.

8IK3wR4CLBWVnhgY1DB1vttQ******

Sample code

The V4 signature algorithm provides enhanced security through more complex encryption and signature methods, while the V1 signature algorithm is simpler. Choose according to your security needs:

V4 signature

package com.test.controller;

import com.aliyun.openservices.log.Client;
import com.aliyun.openservices.log.http.client.ClientConfiguration;
import com.aliyun.openservices.log.http.signer.SignVersion;

public class Sample {

    public static void main(String[] args) throws Exception {
        // The endpoint of Simple Log Service. Beijing is used as an example. Specify other regions based on your actual situation.
        String endpoint = "cn-beijing.log.aliyuncs.com";
        // In this example, the AccessKey ID and AccessKey secret are obtained from environment variables.
        String accessKeyId = System.getenv("ALIBABA_CLOUD_ACCESS_KEY_ID");
        String accessKeySecret = System.getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET");
        ClientConfiguration clientConfiguration = new ClientConfiguration();
        clientConfiguration.setRegion("cn-beijing");
        clientConfiguration.setSignatureVersion(SignVersion.V4);
        Client client = new Client(endpoint,
                accessKeyId,
                accessKeySecret,
                clientConfiguration);
    }
}

V1 signature

package com.test.controller;

import com.aliyun.openservices.log.Client;

public class Sample {

    public static void main(String[] args) throws Exception {
        // The endpoint of Simple Log Service. Beijing is used as an example. Specify other regions based on your actual situation.
        String endpoint = "cn-beijing.log.aliyuncs.com";
        // In this example, the AccessKey ID and AccessKey secret are obtained from environment variables.
        String accessKeyId = System.getenv("ALIBABA_CLOUD_ACCESS_KEY_ID");
        String accessKeySecret = System.getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET");
        Client client = new Client(endpoint, accessKeyId, accessKeySecret);
    }
}

Use STS

Initialize the client

 public Client(String endpoint, String  accessKeyId, String accessKeySecret)

Request parameters

Variable

Type

Required

Description

Example

endpoint

String

Yes

Endpoint is the entry point for accessing Alibaba Cloud services. It is usually a URL that specifies the access protocol, hostname, port, and path of the service. The client can use this information to communicate with the service. Simple Log Service supports public endpoints, virtual private cloud (VPC) endpoints, and acceleration endpoints:

  • Public endpoint: When you pull data from the interface where the public domain name of Simple Log Service is located, external network read traffic is generated. For billing information, see Billable items for the pay-by-ingested-data billing method and Billable items for the pay-by-feature billing method. For a specific list, see Endpoint.

  • VPC endpoint: If you access Simple Log Service through other Alibaba Cloud products in the same region as the project, we recommend that you use the VPC endpoint. For more information, see Endpoint.

  • Transfer acceleration domain name: If the business server and Simple Log Service project belong to the Chinese mainland region and the overseas region, respectively, using the public network to transfer data may cause high network latency or unstable transmission. You can choose the acceleration endpoint. For more information, see Manage transfer acceleration.

cn-hangzhou.log.aliyuncs.com

accessKeyId

String

Yes

If you use STS to configure the access credential, the AccessKeyId parameter below the Credentials parameter that is returned by the AssumeRole operation is used.

LTAI5tQisap3nb5aDQ******

accessKeySecret

String

Yes

If you use STS to configure the access credential, the AccessKeySecret parameter below the Credentials parameter that is returned by the AssumeRole operation is used.

8IK3wR4CLBWVnhgY1DB1vttQ******

Sample code

package com.test.controller;

import com.aliyun.openservices.log.Client;

public class Sample {

    public static void main(String[] args) throws Exception {
        // The endpoint of Simple Log Service. Beijing is used as an example. Specify other regions based on your actual situation.
        String endpoint = "cn-beijing.log.aliyuncs.com";
        // In this example, obtain the value of the AccessKeyId parameter below the Credentials parameter that is returned by the AssumeRole operation.
        String accessKeyId = System.getenv("ALIBABA_CLOUD_ACCESS_KEY_ID");
        // In this example, obtain the value of the AccessKeySecret parameter below the Credentials parameter that is returned by the AssumeRole operation.
        String accessKeySecret = System.getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET");
        // In this example, obtain the value of the SecurityToken parameter below the Credentials parameter that is returned by the AssumeRole operation.
        String securityToken = System.getenv("ALIBABA_CLOUD_STS_TOKEN");
        Client client = new Client(endpoint, accessKeyId, accessKeySecret);
        client.setSecurityToken(securityToken);
    }
}

References

  • Once the client is initialized, you can proceed with operations such as creating a project and writing logs. For more information, see the Quick Start for Java SDK.