Todos os produtos
Search
Central de documentação

Simple Log Service:Gerenciar Logstores com o SDK para Java

Última atualização: Jul 08, 2026

O Logstore é a unidade de coleta, armazenamento e consulta no Simple Log Service. Cada Logstore pertence a um projeto, e cada projeto pode conter vários Logstores. Use o SDK para Java para criar, atualizar, consultar e excluir Logstores.

Pré-requisitos

  • O Simple Log Service está ativado.

  • O SDK do Simple Log Service para Python está inicializado.

Precauções

Este exemplo usa o endpoint público do Simple Log Service na região China (Hangzhou). Endpoint: https://cn-hangzhou.log.aliyuncs.com.

Para acessar o Simple Log Service a partir de outros serviços da Alibaba Cloud na mesma região do projeto, use o endpoint interno do Simple Log Service: https://cn-hangzhou-intranet.log.aliyuncs.com.

Para obter mais informações sobre as regiões e os endpoints compatíveis com o Simple Log Service, consulte Endpoint.

Criar um Logstore

O exemplo a seguir cria um Logstore chamado ali-test-logstore.

import com.aliyun.openservices.log.Client;
import com.aliyun.openservices.log.common.LogStore;
import com.aliyun.openservices.log.exception.LogException;
import com.aliyun.openservices.log.request.CreateLogStoreRequest;

public class CreateLogstore {
    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");
        // Specify the project name.
        String projectName = "ali-test-project";
        // Set the endpoint of Simple Log Service. In this example, the endpoint of the China (Hangzhou) region is used. Replace it with the actual endpoint.
        String host = "https://cn-hangzhou.log.aliyuncs.com";

        // Create a Simple Log Service client.
        Client client = new Client(host, accessId, accessKey);

        try {
            // Specify the Logstore name.
            String logstoreName = "ali-test-logstore";
            System.out.println("ready to create logstore");

            // Create a Logstore. Set the data retention period to 60 days and the number of shards to 2. Enable web tracking.
            LogStore logStore = new LogStore(logstoreName, 60, 2, true);
            // Enable auto split for shards.
            logStore.setmAutoSplit(true);
            // Set the maximum number of shards for auto split to 64.
            logStore.setmMaxSplitShard(64);
            // Enable the feature of recording the public IP address.
            logStore.setAppendMeta(true);
            // Set the storage period of data in the hot tier of the Logstore to 30 days.
            logStore.setHotTTL(30);
            // Set the Logstore type to standard.
            logStore.setMode("standard");

            CreateLogStoreRequest request = new CreateLogStoreRequest(projectName, logStore);
            // Create the Logstore.
            client.CreateLogStore(request);

            System.out.println(String.format("create logstore %s success", logstoreName));

        } catch (LogException e) {
            System.out.println("LogException e :" + e.toString());
            System.out.println("error code :" + e.GetErrorCode());
            System.out.println("error message :" + e.GetErrorMessage());
            throw e;
        }
    }
}

Saída retornada:

ready to create logstore
create logstore ali-test-logstore success

Atualizar um Logstore

O exemplo a seguir atualiza as configurações de um Logstore chamado ali-test-logstore.

import com.aliyun.openservices.log.Client;
import com.aliyun.openservices.log.common.LogStore;
import com.aliyun.openservices.log.exception.LogException;
import com.aliyun.openservices.log.request.UpdateLogStoreRequest;

public class UpdateLogstore {
    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");
        // Specify the project name.
        String projectName = "ali-test-project";
        // Set the endpoint of Simple Log Service. In this example, the endpoint of the China (Hangzhou) region is used. Replace it with the actual endpoint.
        String host = "https://cn-hangzhou.log.aliyuncs.com";

        // Create a Simple Log Service client.
        Client client = new Client(host, accessId, accessKey);

        try {
            // Specify the Logstore name.
            String logstoreName = "ali-test-logstore";
            System.out.println("ready to update logstore");

            // Update the storage period of data in the hot tier of the Logstore to 45 days.
            LogStore logStore = new LogStore(logstoreName, 60, 2, true);
            logStore.setHotTTL(45);

            UpdateLogStoreRequest request = new UpdateLogStoreRequest(projectName, logStore);
            // Update the Logstore.
            client.UpdateLogStore(request);

            System.out.println(String.format("update logstore %s success", logstoreName));

        } catch (LogException e) {
            System.out.println("LogException e :" + e.toString());
            System.out.println("error code :" + e.GetErrorCode());
            System.out.println("error message :" + e.GetErrorMessage());
            throw e;
        }
    }
}

Saída retornada:

ready to update logstore
update logstore ali-test-logstore success

Consultar todos os Logstores

O exemplo a seguir lista todos os Logstores em um projeto.

import com.aliyun.openservices.log.Client;
import com.aliyun.openservices.log.exception.LogException;
import com.aliyun.openservices.log.request.ListLogStoresRequest;
import com.aliyun.openservices.log.response.ListLogStoresResponse;

public class ListLogstore {

    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");
        // Specify the project name.
        String projectName = "ali-test-project";
        // Set the endpoint of Simple Log Service. In this example, the endpoint of the China (Hangzhou) region is used. Replace it with the actual endpoint.
        String host = "https://cn-hangzhou.log.aliyuncs.com";

        // Create a Simple Log Service client.
        Client client = new Client(host, accessId, accessKey);

        try {
            System.out.println("ready to list logstore");

            // Query 10 Logstores.
            ListLogStoresRequest request = new ListLogStoresRequest(projectName, 0, 10, "", "None");

            ListLogStoresResponse response = client.ListLogStores(request);

            for (String logStore : response.GetLogStores()) {
                System.out.println(logStore.toString());
            }
        } catch (LogException e) {
            System.out.println("LogException e :" + e.toString());
            System.out.println("error code :" + e.GetErrorCode());
            System.out.println("error message :" + e.GetErrorMessage());
            throw e;
        }
    }
}

Saída retornada:

ready to list logstore
ali-test-logstore

Consultar um Logstore específico

O exemplo a seguir consulta um Logstore específico.

import com.aliyun.openservices.log.Client;
import com.aliyun.openservices.log.exception.LogException;
import com.aliyun.openservices.log.request.GetLogStoreRequest;
import com.aliyun.openservices.log.response.GetLogStoreResponse;

public class GetLogstore {

    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");
        // Specify the project name.
        String projectName = "ali-test-project";
        // Set the endpoint of Simple Log Service. In this example, the endpoint of the China (Hangzhou) region is used. Replace it with the actual endpoint.
        String host = "https://cn-hangzhou.log.aliyuncs.com";

        // Create a Simple Log Service client.
        Client client = new Client(host, accessId, accessKey);

        try {
            // Specify the Logstore name.
            String logStoreName = "ali-test-logstore";
            System.out.println("ready to get logstore");

            // Query the specified Logstore.
            GetLogStoreRequest request = new GetLogStoreRequest(projectName, logStoreName);

            GetLogStoreResponse response = client.GetLogStore(request);

            System.out.println("The Logstore name is : " + response.GetLogStore().GetLogStoreName());

        } catch (LogException e) {
            System.out.println("LogException e :" + e.toString());
            System.out.println("error code :" + e.GetErrorCode());
            System.out.println("error message :" + e.GetErrorMessage());
            throw e;
        }
    }
}

Saída retornada:

ready to get logstore
The Logstore name is : ali-test-logstore

Excluir um Logstore

O exemplo a seguir exclui um Logstore chamado ali-test-logstore.

Importante
  • Após a exclusão de um Logstore, os dados contidos nele são removidos permanentemente e não podem ser recuperados. Proceda com cautela.

  • Antes de excluir um Logstore, remova todas as configurações do Logtail associadas a ele.

  • Se o LogShipper estiver ativado para o Logstore, interrompa a gravação de novos dados antes da exclusão. Certifique-se de que todos os dados existentes no Logstore foram entregues.

  • Se você usar uma conta Alibaba Cloud para excluir um Logstore e receber um erro de permissões insuficientes, envie um ticket para solicitar a exclusão do Logstore.

import com.aliyun.openservices.log.Client;
import com.aliyun.openservices.log.exception.LogException;

public class DeleteLogstore {

    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");
        // Specify the project name.
        String projectName = "ali-test-project";
        // Set the endpoint of Simple Log Service. In this example, the endpoint of the China (Hangzhou) region is used. Replace it with the actual endpoint.
        String host = "https://cn-hangzhou.log.aliyuncs.com";

        // Create a Simple Log Service client.
        Client client = new Client(host, accessId, accessKey);

        try {
            // Specify the Logstore name.
            String logStoreName = "ali-test-logstore";
            System.out.println("ready to delete logstore");

            // Delete the specified Logstore.
            client.DeleteLogStore(projectName,logStoreName);
            System.out.println(String.format("delete logstore %s success", logStoreName));

        } catch (LogException e) {
            System.out.println("LogException e :" + e.toString());
            System.out.println("error code :" + e.GetErrorCode());
            System.out.println("error message :" + e.GetErrorMessage());
            throw e;
        }
    }
}

Saída retornada:

ready to delete logstore
delete logstore ali-test-logstore success

Referências