All Products
Search
Document Center

Simple Log Service:Use Simple Log Service SDK for Python to manage Logstores

Last Updated:Sep 18, 2024

A Logstore in Simple Log Service is a unit that is used to collect, store, and query data. Each Logstore belongs to a project. You can create multiple Logstores in a project. This topic describes how to create, modify, query, and delete a Logstore by using Simple Log Service SDK for Java and provides sample code.

Prerequisites

  • A Resource Access Management (RAM) user is created, and the required permissions are granted to the RAM user. For more information, see Create a RAM user and grant permissions to the RAM user.

  • The ALIBABA_CLOUD_ACCESS_KEY_ID and ALIBABA_CLOUD_ACCESS_KEY_SECRET environment variables are configured. For more information, see Configure environment variables in Linux, macOS, and Windows.

    Important
    • The AccessKey pair of an Alibaba Cloud account has permissions on all API operations. We recommend that you use the AccessKey pair of a RAM user to call API operations or perform routine O&M.

    • We recommend that you do not save the AccessKey ID or AccessKey secret in your project code. Otherwise, the AccessKey pair may be leaked, and the security of all resources within your account may be compromised.

  • Simple Log Service SDK for Python is installed. For more information, see Install Simple Log Service SDK for Python.

  • A project is created. For more information, see Sample code that is used to create a project.

Usage notes

In this example, the public Simple Log Service endpoint for the China (Hangzhou) region is used, which is https://cn-hangzhou.log.aliyuncs.com. If you want to access Simple Log Service by using 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 Endpoints.

Sample code that is used to create a Logstore

The following sample code provides an example on how to create a Logstore named 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()

Expected results:

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

Sample code that is used to modify a Logstore

The following sample code provides an example on how to modify the configurations of a specified 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"

# 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()

Expected results:

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

Sample code that is used to query all Logstores

The following sample code provides an example on how to query all Logstores:

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")

Expected results:

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

Sample code that is used to delete a Logstore

The following sample code provides an example on how to delete the ali-test-logstore 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)

Expected results:

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

References