All Products
Search
Document Center

Simple Log Service:Initialize Simple Log Service SDK for Java

Last Updated:Jun 17, 2026

Client is the Java SDK client for Simple Log Service. It provides methods to create projects and Logstores, write logs, and read logs. Before you send requests by using the Java SDK, initialize a Client instance and modify the default configurations as needed.

Prerequisites

Procedure

Two initialization methods are available. Choose the one that fits your scenario:

  • AK initialization: An AccessKey pair is a long-term credential that can be directly used for API calls. This method is suitable for stable scenarios that do not require frequent credential rotation.

  • STS initialization: Security Token Service (STS) generates temporary access credentials. This method is suitable for scenarios that require dynamic, short-lived authorization.

Initialize using an AccessKey pair

Initialize the client

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

Request parameters

Variable

Type

Required

Description

Example value

endpoint

String

Yes

The endpoint used to access Simple Log Service. An endpoint is a URL that specifies the access protocol, hostname, port, and path for client-to-service communication. Simple Log Service provides the following types of endpoints:

  • Public endpoint: Pulling data from a public endpoint incurs outbound traffic fees. For more information about billing, see Billable items for the pay-by-ingested-data mode and Billable items for the pay-by-feature mode. For a list of endpoints, see Endpoints.

  • VPC endpoint: To access Simple Log Service from other Alibaba Cloud services in the same region as your project, we recommend that you use a VPC endpoint. For more information, see Endpoints.

  • Acceleration endpoint: If your application server and Simple Log Service project are in different regions, such as one inside and one outside mainland China, data transmission over the public network may experience high latency and instability. In this case, use an 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 access credentials, the AccessKey ID of an Alibaba Cloud account or a Resource Access Management (RAM) user. The AccessKey ID identifies the caller. For more information, see Configure access credentials.

    Warning

    An Alibaba Cloud account AccessKey pair has full permissions on all resources. If the AccessKey pair is leaked, your resources are at risk. We recommend that you use the AccessKey pair of a RAM user that is granted only the required permissions.

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

accessKeySecret

String

Yes

If you use an AccessKey pair to configure access credentials, the AccessKey secret of an Alibaba Cloud account or a RAM user. The AccessKey secret authenticates the AccessKey ID. For more information, see Configure access credentials.

yourAccessKeySecret

Sample code

The V4 signature algorithm offers higher security through more complex encryption and signing. The V1 signature algorithm is simpler. Select a signature algorithm based on your 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 {
        // The endpoint of Simple Log Service. This example uses the endpoint of the China (Beijing) region. Replace it with the actual endpoint.
        String endpoint = "cn-beijing.log.aliyuncs.com";
        // This example obtains the AccessKey ID and 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 {
        // The endpoint of Simple Log Service. This example uses the endpoint of the China (Beijing) region. Replace it with the actual endpoint.
        String endpoint = "cn-beijing.log.aliyuncs.com";
        // This example obtains the AccessKey ID and 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);
    }
}

Initialize using STS

Initialize the client

Client(String endpoint, CredentialsProvider credentialsProvider) ;

Request parameters

Variable

Type

Required

Description

Example

endpoint

String

Yes

The endpoint used to access Simple Log Service. An endpoint is a URL that specifies the access protocol, hostname, port, and path for client-to-service communication. Simple Log Service provides the following types of endpoints:

  • Public endpoint: Pulling data from a public endpoint incurs outbound traffic fees. For more information about billing, see Billable items for the pay-by-ingested-data mode and Billable items for the pay-by-feature mode. For a list of endpoints, see Endpoints.

  • VPC endpoint: To access Simple Log Service from other Alibaba Cloud services in the same region as your project, we recommend that you use a VPC endpoint. For more information, see Endpoints.

  • Acceleration endpoint: If your application server and Simple Log Service project are in different regions, such as one inside and one outside mainland China, data transmission over the public network may experience high latency and instability. In this case, use an acceleration endpoint. For more information, see Manage transfer acceleration.

cn-hangzhou.log.aliyuncs.com

accessKeyId

String

Yes

If you use STS to configure access credentials, the AccessKeyId from the Credentials parameter returned by the AssumeRole API operation.

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

accessKeySecret

String

Yes

If you use STS to configure access credentials, the AccessKeySecret from the Credentials parameter returned by the AssumeRole API operation.

yourAccessKeySecret

securityToken

String

Yes

If you use STS to configure access credentials, the SecurityToken from the Credentials parameter returned by the AssumeRole API operation.

****************

Sample code

package com.test.controller;

import com.aliyun.openservices.log.Client;
import com.aliyun.openservices.log.common.auth.DefaultCredentials;
import com.aliyun.openservices.log.common.auth.StaticCredentialsProvider;

public class Sample {

    public static void main(String[] args) throws Exception {
        // The endpoint of Simple Log Service. This example uses the endpoint of the China (Beijing) region. Replace it with the actual endpoint.
        String endpoint = "cn-beijing.log.aliyuncs.com";
        // This example obtains the AccessKeyId from the Credentials parameter returned by the AssumeRole operation from an environment variable.
        String accessKeyId = System.getenv("ALIBABA_CLOUD_ACCESS_KEY_ID");
        // This example obtains the AccessKeySecret from the Credentials parameter returned by the AssumeRole operation from an environment variable.
        String accessKeySecret = System.getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET");
        // This example obtains the SecurityToken from the Credentials parameter returned by the AssumeRole operation from an environment variable.
        String securityToken = System.getenv("ALIBABA_CLOUD_STS_TOKEN");

        Client client = new Client(endpoint, new StaticCredentialsProvider(
                new DefaultCredentials(accessKeyId, accessKeySecret, securityToken)
        ));
    }
}

References

  • After you initialize the client, you can call API operations to create projects, write logs, or perform other operations. For more information, see Quick Start for Java SDK.