A Logstore is the unit for data collection, storage, and query in Simple Log Service. Each Logstore belongs to a project, and each project can contain multiple Logstores. You can use the Java SDK to create, update, query, and delete Logstores.
Prerequisites
Simple Log Service is activated.
Simple Log Service SDK for Python is initialized.
Simple Log Service SDK for Python is installed. For more information, see Install Simple Log Service SDK for Python.
Simple Log Service is activated. For more information, see Activate Simple Log Service.
Precautions
In this example, the public Simple Log Service endpoint for the China (Hangzhou) region is used. Endpoint: https://cn-hangzhou.log.aliyuncs.com.
If you want to access Simple Log Service from other Alibaba Cloud services that reside in the same region as your project, you can use the internal Simple Log Service endpoint, which is https://cn-hangzhou-intranet.log.aliyuncs.com.
For more information about the supported regions and endpoints of Simple Log Service, see Endpoint.
Create a logstore
The following example creates a Logstore named 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()Expected output:
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 successUpdate a logstore
The following example updates the configuration of a 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()Expected output:
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 successList all logstores
The following example lists all Logstores in a project.
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")Expected output:
ready to list logstore
ali-test-logstore
ali-test-logstore2
ali-test-webtracking
list logstore successDelete a logstore
The following example deletes a Logstore named 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)Expected output:
ready to delete logstore
delete logstore ali-test-logstore successRelated documents
In addition to its native SDK, SLS also supports the common Alibaba Cloud SDKs. For more information, see Simple Log Service_SDK Center_Alibaba Cloud OpenAPI Explorer.