All Products
Search
Document Center

Simple Log Service:Use Simple Log Service SDK for Java to write logs

Last Updated:Feb 28, 2025

If you want to upload the operational logs of applications, operating system logs, and user logs to Simple Log Service, you can use the PutLogs method provided by Simple Log Service SDK for Java. This topic describes how to use Simple Log Service SDK for Java to write logs to Simple Log Service.

Prerequisites

Limits

  • The underlying mechanism of Aliyun Log Java Producer calls the PutLogs operation to upload logs. The size of raw logs that can be uploaded each time is limited. For more information, see Data read and write.

  • The basic resources of Simple Log Service, such as projects, logstores, shards, and machine groups, also have limitations. For more information, see Basic resources.

  • The first time you run code, you must enable the indexing feature for your logstore in the Simple Log Service console. Then, wait for about one minute before querying logs.

  • If you query logs in the Simple Log Service console and the value length of a field in the returned logs exceeds the upper limit, the field value will be truncated, and the excess part will not be used for analysis. For more information, see Create indexes.

General writes and writes at 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

The destination project.

logStore

String

Yes

The destination Logstore.

topic

String

No

The topic of logs.

logItems

List

Yes

The log or logs that you want to upload. You must upload the log or logs in the LogItem format.

source

String

No

The source of logs.

Note

If you leave this parameter empty, the IP address of the host where the producer resides is automatically used.

shardHash

String

No

The hash ID of the location at which you want to upload 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 Simple Log Service endpoint. In this example, the Simple Log Service endpoint for the China (Hangzhou) region is used. Replace the parameter 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-project-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