Todos os produtos
Search
Central de documentação

Simple Log Service:Usar o Simple Log Service SDK for Java para gravar logs

Última atualização: Jun 23, 2026

O método PutLogs do Simple Log Service SDK for Java permite fazer upload de logs operacionais de aplicações, logs de sistema operacional e logs de usuários no Simple Log Service. Este tópico descreve como usar o Simple Log Service SDK for Java para gravar logs no Simple Log Service.

Pré-requisitos

Gravação geral e gravação em locais específicos

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

Parâmetros

Parâmetro

Tipo

Obrigatório

Descrição

project

String

Sim

O projeto de destino.

logStore

String

Sim

O Logstore de destino.

topic

String

Não

O tópico dos logs.

logItems

List

Sim

O log ou logs para upload. O upload deve ser feito no formato LogItem.

source

String

Não

A origem dos logs.

null

Se esse parâmetro ficar vazio, o endereço IP do host onde o produtor reside será usado automaticamente.

shardHash

String

Não

O ID de hash do local para upload dos logs.

Código de exemplo

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);
    }
}

Próximos passos

Referências