All Products
Search
Document Center

Simple Log Service:Initialize Simple Log Service SDK for Java

Last Updated:Apr 08, 2025

Client is a Java SDK client that you can use to access Simple Log Service. It provides various methods for you to create a project, create a Logstore, write logs, and read logs. To use Simple Log Service SDK for Java to initiate a request, you must initialize a Client instance and modify the default settings of the Client instance based on your business requirements.

Prerequisites

Simple Log Service SDK for Java is installed. For more information, see Install Simple Log Service SDK for Java.

Procedure

You can select one of the following initialization methods based on your business requirements:

  • AccessKey pair-based initialization: AccessKey pairs are valid for a long period of time and can be directly used for API calls. AccessKey pair-based initialization is suitable for scenarios in which frequent access credential replacement is not required.

  • STS-based initialization: Security Token Service (STS) generates temporary access credentials. STS-based initialization is suitable for scenarios in which dynamic and temporary authorization is required.

Use an AccessKey pair

Initialize a Client instance

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

Request parameters

Parameter

Type

Required

Description

Example

endpoint

String

Yes

The endpoint. An endpoint is used to access an Alibaba Cloud service. In most cases, an endpoint is a URL. An endpoint specifies information about a service, such as the access protocol, hostname, port, and path. Clients can use the information to access the service. For more information, see Endpoints. Simple Log Service supports public endpoints, virtual private cloud (VPC) endpoints, and acceleration endpoints.

  • Public endpoints: If data is pulled over a public Simple Log Service endpoint, read traffic over the Internet is generated. For more information about billing, see Billable items of pay-by-ingested-data and Billable items of pay-by-feature. For more information, see Endpoints.

  • VPC endpoints: If you use another Alibaba Cloud service that resides in the same region as your Simple Log Service project to access Simple Log Service, we recommend that you use a VPC endpoint. For more information, see Endpoints.

  • Acceleration endpoints: If your server and Simple Log Service reside in different regions, such as in and outside China, network latency is high and transmission is unstable when data is transmitted over the Internet. In this case, you can use an acceleration endpoint. For more information, see Use the transfer acceleration feature.

cn-hangzhou.log.aliyuncs.com

accessKeyId

String

Yes

  • The AccessKey ID of your Alibaba Cloud account or Resource Access Management (RAM) user if you use an AccessKey pair to configure access credentials. The AccessKey ID 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.

LTAI****************

accessKeySecret

String

Yes

The AccessKey secret of your Alibaba Cloud account or RAM user if you use an AccessKey pair to configure access credentials. The AccessKey secret is used to verify your AccessKey ID. For more information, see Configure access credentials.

yourAccessKeySecret

Sample code

The V4 signature algorithm uses more complex encryption and signature methods to provide higher security. The V1 signature algorithm is relatively simple. You can select a signature algorithm based on your business requirements.

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 {
        // Specify a Simple Log Service endpoint. In this example, the Simple Log Service endpoint for the China (Beijing) region is used. Replace the parameter value with an actual endpoint.
        String endpoint = "cn-beijing.log.aliyuncs.com";
        // Obtain an AccessKey ID and an AccessKey secret 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 {
        // Specify a Simple Log Service endpoint. In this example, the Simple Log Service endpoint for the China (Beijing) region is used. Replace the parameter value with an actual endpoint.
        String endpoint = "cn-beijing.log.aliyuncs.com";
        // Obtain an AccessKey ID and an AccessKey secret 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 a Client instance

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

Request parameters

Parameter

Type

Required

Description

Example

endpoint

String

Yes

The endpoint. An endpoint is used to access an Alibaba Cloud service. In most cases, an endpoint is a URL. An endpoint specifies information about a service, such as the access protocol, hostname, port, and path. Clients can use the information to access the service. For more information, see Endpoints. Simple Log Service supports public endpoints, VPC endpoints, and acceleration endpoints.

  • Public endpoints: If data is pulled over a public Simple Log Service endpoint, read traffic over the Internet is generated. For more information about billing, see Billable items of pay-by-ingested-data and Billable items of pay-by-feature. For more information, see Endpoints.

  • VPC endpoints: If you use another Alibaba Cloud service that resides in the same region as your Simple Log Service project to access Simple Log Service, we recommend that you use a VPC endpoint. For more information, see Endpoints.

  • Acceleration endpoints: If your server and Simple Log Service reside in different regions, such as in and outside China, network latency is high and transmission is unstable when data is transmitted over the Internet. In this case, you can use an acceleration endpoint. For more information, see Use the transfer acceleration feature.

cn-hangzhou.log.aliyuncs.com

accessKeyId

String

Yes

The value of the AccessKeyId parameter below the Credentials parameter that is returned by the AssumeRole operation if you use STS to configure access credentials.

LTAI****************

accessKeySecret

String

Yes

The value of the AccessKeySecret parameter below the Credentials parameter that is returned by the AssumeRole operation if you use STS to configure access credentials.

yourAccessKeySecret

Sample code

package com.test.controller;

import com.aliyun.openservices.log.Client;

public class Sample {

    public static void main(String[] args) throws Exception {
        // Specify a Simple Log Service endpoint. In this example, the Simple Log Service endpoint for the China (Beijing) region is used. Replace the parameter value with an actual endpoint.
        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

After you initialize a Client instance, you can use Simple Log Service SDK for Java to create a project and write logs. For more information, see Get started with Simple Log Service SDK for Java.