Todos los productos
Search
Centro de documentos

Simple Log Service:Gestión de logstores con el SDK de Python

Última actualización: Sep 15, 2026

Un logstore es la unidad de recopilación, almacenamiento y consulta de datos en Simple Log Service. Cada logstore pertenece a un proyecto y cada proyecto puede contener varios logstores. Con el SDK de Java usted puede crear, actualizar, consultar y eliminar logstores.

Requisitos previos

Precauciones

En este ejemplo se utiliza el endpoint público de Simple Log Service correspondiente a la región China (Hangzhou). Endpoint: https://cn-hangzhou.log.aliyuncs.com.

Si necesita acceder a Simple Log Service desde otros servicios de Alibaba Cloud ubicados en la misma región que su proyecto, utilice el endpoint interno de Simple Log Service: https://cn-hangzhou-intranet.log.aliyuncs.com.

Para conocer las regiones y los endpoints compatibles con Simple Log Service, consulte Endpoint.

Creación de un logstore

A continuación se muestra un ejemplo que crea un logstore llamado ali-test-logstore.

from aliyun.log import LogClient
import os

# This example retrieves the AccessKey ID and AccessKey secret from environment variables.
accessKeyId = os.environ.get('ALIBABA_CLOUD_ACCESS_KEY_ID', '')
accessKey = os.environ.get('ALIBABA_CLOUD_ACCESS_KEY_SECRET', '')
# The endpoint of Simple Log Service. This example uses the endpoint for the China (Hangzhou) region. Replace this with the endpoint for your region.
endpoint = "cn-hangzhou.log.aliyuncs.com"
# Create a Simple Log Service client.
client = LogClient(endpoint, accessKeyId, accessKey)

# The name of the project.
project_name = "ali-test-project"
# The name of the Logstore.
logstore_name = "ali-test-logstore"

# Create a Logstore.
def create_logstore():
    print("ready to create logstore %s" %logstore_name)
    # Create a standard Logstore, set the data retention period to 30 days, and use the default values for other parameters.
    client.create_logstore(project_name, logstore_name, ttl = 30, hot_ttl=-1)
    print("create logstore %s success " %logstore_name)

# Query the Logstore.
def get_logstore():
    print("ready to get logstore")
    res = client.get_logstore(project_name, logstore_name)
    res.log_print()
    print("get logstore success ")

if __name__ == '__main__':
    # Create the Logstore.
    create_logstore()
    # Query the Logstore.
    get_logstore()

Salida esperada:

ready to create logstore ali-test-logstore
create logstore ali-test-logstore success
ready to get logstore
GetLogStoreResponse:
headers: {'Server': 'Tengine', 'Content-Type': 'application/json', 'Content-Length': '319', 'Connection': 'keep-alive', 'Access-Control-Allow-Origin': '*', 'Date': 'Fri, 16 Dec 2022 07:04:37 GMT', 'x-log-time': '1671174277', 'x-log-requestid': '639C18851A0CDA516EFA437B'}
logstore_name: ali-test-logstore
shard_count: 2
ttl: 30
get logstore success

Actualización de un logstore

A continuación se muestra un ejemplo que actualiza la configuración de un logstore.

from aliyun.log import LogClient
import os

# This example retrieves the AccessKey ID and AccessKey secret from environment variables.
accessKeyId = os.environ.get('ALIBABA_CLOUD_ACCESS_KEY_ID', '')
accessKey = os.environ.get('ALIBABA_CLOUD_ACCESS_KEY_SECRET', '')
# The endpoint of Simple Log Service. This example uses the endpoint for the China (Hangzhou) region. Replace this with the endpoint for your region.
endpoint = "cn-hangzhou.log.aliyuncs.com"
# Create a Simple Log Service client.
client = LogClient(endpoint, accessKeyId, accessKey)

# The name of the project.
project_name = "ali-test-project"
# The name of the Logstore.
logstore_name = "ali-test-logstore"

# Update the Logstore.
def update_logstore():
    print("ready to update logstore %s" %logstore_name)
    # Update the data retention period to 60 days.
    client.update_logstore(project_name,logstore_name,ttl=60)
    print("update logstore %s success " %logstore_name)

# Query the Logstore.
def get_logstore():
    print("ready to get logstore")
    res = client.get_logstore(project_name, logstore_name)
    res.log_print()
    print("get logstore success ")

if __name__ == '__main__':
    # Update the Logstore.
    update_logstore()
    # Query the Logstore.
    get_logstore()

Salida esperada:

ready to update logstore ali-test-logstore
update logstore ali-test-logstore success
ready to get logstore
GetLogStoreResponse:
headers: {'Server': 'Tengine', 'Content-Type': 'application/json', 'Content-Length': '319', 'Connection': 'keep-alive', 'Access-Control-Allow-Origin': '*', 'Date': 'Fri, 16 Dec 2022 07:10:02 GMT', 'x-log-time': '1671174602', 'x-log-requestid': '639C19CAED4C6B234D66E5C1'}
logstore_name: ali-test-logstore
shard_count: 2
ttl: 60
get logstore success

Listado de todos los logstores

A continuación se muestra un ejemplo que lista todos los logstores de un proyecto.

from aliyun.log import LogClient
import os

# This example retrieves the AccessKey ID and AccessKey secret from environment variables.
accessKeyId = os.environ.get('ALIBABA_CLOUD_ACCESS_KEY_ID', '')
accessKey = os.environ.get('ALIBABA_CLOUD_ACCESS_KEY_SECRET', '')
# The endpoint of Simple Log Service. This example uses the endpoint for the China (Hangzhou) region. Replace this with the endpoint for your region.
endpoint = "cn-hangzhou.log.aliyuncs.com"
# Create a Simple Log Service client.
client = LogClient(endpoint, accessKeyId, accessKey)

# The name of the project.
project_name = "ali-test-project"

if __name__ == '__main__':
    # List all Logstores.
    print("ready to list logstore")
    res = client.list_logstore(project_name, None, 0, 100)

    for logstore in res.get_logstores():
        print(logstore)

    print("list logstore success")

Salida esperada:

ready to list logstore
ali-test-logstore
ali-test-logstore2
ali-test-webtracking
list logstore success

Eliminación de un logstore

A continuación se muestra un ejemplo que elimina un logstore llamado ali-test-logstore.

from aliyun.log import LogClient
import os

# This example retrieves the AccessKey ID and AccessKey secret from environment variables.
accessKeyId = os.environ.get('ALIBABA_CLOUD_ACCESS_KEY_ID', '')
accessKey = os.environ.get('ALIBABA_CLOUD_ACCESS_KEY_SECRET', '')
# The endpoint of Simple Log Service. This example uses the endpoint for the China (Hangzhou) region. Replace this with the endpoint for your region.
endpoint = "cn-hangzhou.log.aliyuncs.com"
# Create a Simple Log Service client.
client = LogClient(endpoint, accessKeyId, accessKey)

# The name of the project.
project_name = "ali-test-project"

# The name of the Logstore.
logstore_name = "ali-test-logstore"

if __name__ == '__main__':

    # Delete the Logstore.
    print("ready to delete logstore")
    client.delete_logstore(project_name, logstore_name)
    print("delete logstore %s success " %logstore_name)

Salida esperada:

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

Documentos relacionados