All Products
Search
Document Center

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

Last Updated:Feb 07, 2025

To upload various types of logs, such as application, operating system, or user logs to Simple Log Service, utilize the PutLogs method from the Simple Log Service SDK for Java. This topic explains the process of using the SDK to write logs to Simple Log Service.

Prerequisites

Limits

  • The Aliyun Log Java Producer uses the PutLogs operation to upload logs, and there is a limit to the size of raw logs that can be written at once. For details, see Data Read and Write.

  • Basic resources of Simple Log Service, such as the number of projects, Logstores, shards, LogtailConfigs, machine groups, and the size and length of LogItems and their keys and values, have limitations. For details, see Basic Resources.

  • Before running code for the first time, enable the indexing feature for your Logstore in the Simple Log Service console and wait for 1 minute before querying logs.

  • If the value length of a field in logs queried from the Simple Log Service console exceeds the limit, the value is truncated and the excess is not used for analysis. For details, see Create Index.

Normal write & specified location write

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

Parameter description

Parameter

Type

Required

Description

project

String

Yes

The destination project.

logStore

String

Yes

The destination Logstore.

topic

String

No

The topic of the log.

logItems

List

Yes

The log or log list that you want to send. The format of the logs to be written is LogItem.

source

String

No

The source.

Note

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

shardHash

String

No

The HashID of the log write location.

Code example

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-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