All Products
Search
Document Center

Simple Log Service:Use the Java SDK to write logs

Last Updated:Jun 23, 2026

Upload application, operating system, or user logs to Simple Log Service by calling the PutLogs method in the Simple Log Service Java SDK.

Prerequisites

Usage notes

  • The aliyun-log-producer calls the PutLogs operation to upload logs. Raw log size limits apply per request. For more information, see Data read and write.

  • SLS resources — projects, Logstores, shards, LogtailConfig, machine groups, LogItem size, LogItem (Key) length, and LogItem (Value) length — are subject to limits. For more information, see Basic resource limits.

  • After the code runs for the first time, enable Logstore indexing in the SLS console and wait one minute before running queries.

  • When querying logs in the console, field values exceeding the maximum length are truncated and excluded from analysis. For more information, see Create indexes.

General writes and writes to specific locations

public PutLogsResponse PutLogs(String project, String logStore,
			String topic, List<LogItem> logItems, String source,
			String shardHash)

Parameters

Parameter

Type

Required

Description

project

String

Yes

Target project

logStore

String

Yes

The destination Logstore.

topic

String

No

The topic of the logs.

logItems

List

Yes

The log or list of logs to send. The logs must be in the LogItem format.

source

String

No

The log source identifier.

Note

If you leave this parameter empty or do not specify it, the IP address of the host where the producer resides is used.

shardHash

String

No

The hash ID of the shard to which you want to write logs.

Sample code

import com.aliyun.openservices.log.Client;
import com.aliyun.openservices.log.common.LogContent;
import com.aliyun.openservices.log.common.LogItem;
import com.aliyun.openservices.log.exception.LogException;

import java.util.ArrayList;
import java.util.List;

public class PutLogsTest {
    public static void main(String[] args) throws LogException {

        /**
         * In this example, the AccessKey ID and AccessKey secret are obtained from environment variables.
         */
        String accessId = System.getenv("ALIBABA_CLOUD_ACCESS_KEY_ID");
        String accessKey = System.getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET");
        /**
         * The endpoint of Simple Log Service. In this example, the endpoint for the China (Hangzhou) region is used. Replace the value with the actual endpoint.
         */
        String host = "cn-hangzhou.log.aliyuncs.com";
        /**
         * Create a Simple Log Service client.
         */
        Client client = new Client(host, accessId, accessKey);
        String project = "ali-peoject-test";
        String logStore = "test-logstore";
        String topic = "";
        String source = "";
        LogContent logContent = new LogContent("message", "2021-05-15 16:43:35 ParameterInvalid 400\n" +
                "com.aliyun.openservices.log.exception.LogException:The body is not valid json string.\n" +
                "at com.aliyun.openservices.log.Client.ErrorCheck(Client.java:2161)\n" +
                "at com.aliyun.openservices.log.Client.SendData(Client.java:2312)\n" +
                "at com.aliyun.openservices.log.Client.PullLogsk(Client.java:1397)\n" +
                "at com.aliyun.openservices.log.Client.SendData(Client.java:2265)\n" +
                "at com.aliyun.openservices.log.Client.GetCursor(Client.java:1123)\n" +
                "at com.aliyun.openservices.log.Client.PullLogs(Client.java:2161)\n" +
                "at com.aliyun.openservices.log.Client.ErrorCheck(Client.java:2426)\n" +
                "at transformEvent.main(transformEvent.java:2559)");
        List<LogItem> logItems = new ArrayList<>();
        for (int i = 0; i < 5; ++i) {
            LogItem logItem = new LogItem();
            logItem.PushBack("language", "android");
            logItem.PushBack("time", String.valueOf(System.currentTimeMillis()));
            logItem.PushBack(logContent);
            logItems.add(logItem);
        }

        client.PutLogs(project, logStore, topic, logItems, source, null);
    }
}

What to do next

References