A Logstore in 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 Log Service SDK for Java and provides sample code.

Prerequisites

  • Log Service is activated. For more information, see Activate Log Service.
  • An AccessKey pair is created and obtained. For more information, see AccessKey pair.

    An Alibaba Cloud account has permissions to call all API operations. If the AccessKey pair of your Alibaba Cloud account is leaked, your data may be exposed to high security risks. We recommend that you log on as a RAM user that has permissions to call API operations or perform routine O&M tasks. Make sure that the RAM user is granted the permissions to manage Log Service resources. For more information, see Create a RAM user and authorize the RAM user to access Log Service.

  • A Python development environment is installed. For more information, visit the official website of Python.

    Log Service SDK for Python supports the following versions of Python: PyPy 2, PyPy 3, Python 2.6, Python 2.7, Python 3.3, Python 3.4, Python 3.5, Python 3.6, and Python 3.7. You can run the python-V command to check the version of Python that is installed.

  • Log Service SDK for Python is installed. Fore more information, see Install Log Service SDK for Java.
  • A project is created. For more information, see Sample code that is used to create a project.

Usage notes

In this example, the public Log Service endpoint for the China (Hangzhou) region is used. Endpoint: https://cn-hangzhou.log.aliyuncs.com. If you want to access a project by using other Alibaba Cloud services in the same region as Log Service, use an internal endpoint in the following format: https://cn-hangzhou-intranet.log.aliyuncs.com. For information about the mappings between the supported regions and endpoints in 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

# An Alibaba Cloud account has permissions to call all API operations. If the AccessKey pair of your Alibaba Cloud account is leaked, your data may be exposed to high security risks. We recommend that you create and use a RAM user to call API operations or perform routine O&M. 
accessKeyId = "yourAccessKeyId"
accessKey = "yourAccessKeySecret"
endpoint = "cn-hangzhou.log.aliyuncs.com"
# Create a 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 result:

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

# An Alibaba Cloud account has permissions to call all API operations. If the AccessKey pair of your Alibaba Cloud account is leaked, your data may be exposed to high security risks. We recommend that you create and use a RAM user to call API operations or perform routine O&M. 
accessKeyId = "yourAccessKeyId"
accessKey = "yourAccessKeySecret"
endpoint = "cn-hangzhou.log.aliyuncs.com"
# Create a 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 result:

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, ListProjectResponse

# An Alibaba Cloud account has permissions to call all API operations. If the AccessKey pair of your Alibaba Cloud account is leaked, your data may be exposed to high security risks. We recommend that you create and use a RAM user to call API operations or perform routine O&M. 
accessKeyId = "yourAccessKeyId"
accessKey = "yourAccessKeySecret"
endpoint = "cn-hangzhou.log.aliyuncs.com"
# Create a 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 result:

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

# An Alibaba Cloud account has permissions to call all API operations. If the AccessKey pair of your Alibaba Cloud account is leaked, your data may be exposed to high security risks. We recommend that you create and use a RAM user to call API operations or perform routine O&M. 
accessKeyId = "yourAccessKeyId"
accessKey = "yourAccessKeySecret"
endpoint = "cn-hangzhou.log.aliyuncs.com"
# Create a 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 result:

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

References

  • Alibaba Cloud OpenAPI Explorer provides debugging capabilities, SDKs, examples, and related documents. You can use OpenAPI Explorer to debug Log Service API operations without the need to manually encapsulate and sign requests. For more information, visit OpenAPI Explorer.
  • Log Service provides the command-line interface (CLI) to meet the requirements for automated configurations in Log Service. For more information, see Log Service CLI.
  • For information about the Logstore-related API operations, see the following topics:
  • For more information about sample code, see Aliyun Log Python On GitHub.