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 for Python para criar, atualizar, consultar e excluir Logstores.
Pré-requisitos
Simple Log Service SDK for Python instalado. Para mais informações, consulte Instalar o Simple Log Service SDK for Python.
Projeto criado. Para mais informações, consulte Código de exemplo para criar um projeto.
Código de exemplo para criar um Logstore
O código de exemplo a seguir demonstra como criar um Logstore chamado ali-test-logstore:
from aliyun.log import LogClient
import os
# Configure environment variables. In this example, the AccessKey ID and AccessKey secret are obtained from environment variables.
accessKeyId = os.environ.get('ALIBABA_CLOUD_ACCESS_KEY_ID', '')
accessKey = os.environ.get('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.
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 the 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 retain default settings 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()
Resultados esperados:
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-logstore3
shard_count: 2
ttl: 30
get logstore success
Código de exemplo para modificar um Logstore
O exemplo a seguir ilustra como alterar as configurações de um Logstore específico:
from aliyun.log import LogClient
import os
# Configure environment variables. In this example, the AccessKey ID and AccessKey secret are obtained from environment variables.
accessKeyId = os.environ.get('ALIBABA_CLOUD_ACCESS_KEY_ID', '')
accessKey = os.environ.get('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.
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"
# Modify the Logstore.
def update_logstore():
print("ready to update logstore %s" %logstore_name)
# Change 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__':
# Modify the Logstore.
update_logstore()
# Query the Logstore.
get_logstore()
Resultados esperados:
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-logstore3
shard_count: 2
ttl: 60
get logstore success
Código de exemplo para consultar todos os Logstores
Use o código abaixo para listar todos os Logstores disponíveis:
from aliyun.log import LogClient
import os
# Configure environment variables. In this example, the AccessKey ID and AccessKey secret are obtained from environment variables.
accessKeyId = os.environ.get('ALIBABA_CLOUD_ACCESS_KEY_ID', '')
accessKey = os.environ.get('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.
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__':
# Query 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")
Resultados esperados:
ready to list logstore
ali-test-logstore
ali-test-logstore2
ali-test-webtracking
list logstore success
Código de exemplo para excluir um Logstore
O trecho a seguir mostra como excluir o Logstore ali-test-logstore.
from aliyun.log import LogClient
import os
# Configure environment variables. In this example, the AccessKey ID and AccessKey secret are obtained from environment variables.
accessKeyId = os.environ.get('ALIBABA_CLOUD_ACCESS_KEY_ID', '')
accessKey = os.environ.get('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.
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)
Resultados esperados:
ready to delete logstore
delete logstore ali-test-logstore success