All Products
Search
Document Center

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

Last Updated:Sep 18, 2024

A project in Simple Log Service is a unit that is used to manage resources such as Logstores, Metricstores, and machine groups. You can use the endpoint of a project to access the resources of Simple Log Service. This topic describes how to create, modify, query, and delete a project by using Simple Log Service SDK for Java and provides the sample code.

Prerequisites

  • Simple Log Service is activated. For more information, see Activate Simple Log Service.

  • 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.

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 project

The following sample code provides an example on how to create a project named ali-test-project-python:

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

# Create the project. 
def create_project():
    print("ready to create project %s" %project_name)
    client.create_project(project_name, project_des="this is a project created from ide.")
    print("create project %s success " %project_name)

# Query the project. 
def get_project():
    print("ready to get project")

    res = client.get_project(project_name)
    print("the project name is :" + res.get_projectname())
    print("the project region is :" + res.get_region())
    print("the project create time is :" + res.get_create_time())
    print("the project status is :" + res.get_status())
    print("the project desc is :" + res.get_description())
    print("get project success ")


if __name__ == '__main__':
    # Create the project. 
    create_project()
    # Query the project. 
    get_project()

Expected results:

ready to create project ali-test-project-python
create project ali-test-project-python success
ready to get project
the project name is :ali-test-project-python
the project region is :cn-hangzhou
the project create time is :2022-12-12 17:45:57
the project status is :Normal
the project desc is :this is a project created from ide.
get project success

Sample code that is used to query all projects

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

from aliyun.log import LogClient, ListProjectResponse
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)

if __name__ == '__main__':
    # Query all projects. 
    print("ready to list project")
    res = client.list_project(0, 100, "")

    for project in res.get_projects():
        print(project)

    print("list project success")

Expected results:

ready to list project
{'projectName': 'ali-test-project-python', 'status': 'Normal', 'owner': '', 'description': '', 'region': 'cn-hangzhou', 'createTime': '1660810539', 'lastModifyTime': '1665978308'}
{'projectName': 'acktest', 'status': 'Normal', 'owner': '', 'description': '', 'region': 'cn-hangzhou', 'createTime': '1659084879', 'lastModifyTime': '1665978308'}
{'projectName': 'k8s-log-c143ee0f20b634206bf931507e0785394', 'status': 'Normal', 'owner': '', 'description': '', 'region': 'cn-hangzhou', 'createTime': '1659078881', 'lastModifyTime': '1665978308'}
{'projectName': 'sls-alert-1379186349531844-cn-hangzhou', 'status': 'Normal', 'owner': '', 'description': '', 'region': 'cn-hangzhou', 'createTime': '1658715011', 'lastModifyTime': '1665978308'}
{'projectName': 'k8s-log-c16bb522e6c794809bcc87182ea45246a', 'status': 'Normal', 'owner': '', 'description': 'k8s log project, created by alibaba cloud log controller', 'region': 'cn-hangzhou', 'createTime': '1657265089', 'lastModifyTime': '1665978308'}
list project success

Sample code that is used to query the logs in a project

The following sample code provides an example on how to query the logs of a specified Logstore in a specified project:

from aliyun.log import LogClient, GetProjectLogsRequest
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__':
    print("ready to get project log" + project_name)
    # The SQL statement used to query the top 10 hosts that initiate the most requests in a specified time range. 
    sql = "SELECT host, COUNT(*) as pv FROM " + logstore_name + " where __time__ > 1670899865 and __time__ <1670900765 group by host order by pv limit 10"
    request = GetProjectLogsRequest(project_name, sql, False)
    response = client.get_project_logs(request)
    print(response)
    for QueriedLog in response.get_logs():
        print(QueriedLog.get_contents())
    print("get project %s log success " %project_name)

Expected results:

ready to get project logali-test-project
{'host': 'www.example.aliyundoc.com', 'pv': '1'}
{'host': 'www.demo.aliyundoc.com', 'pv': '1'}
{'host': 'www.learn.aliyundoc.com', 'pv': '1'}
{'host': 'www.guide.aliyundoc.com', 'pv': '1'}
{'host': 'www.developer.aliyundoc.com', 'pv': '1'}
{'host': 'www.pull.aliyundoc.com', 'pv': '1'}
{'host': 'www.push.aliyundoc.com', 'pv': '1'}
{'host': 'www.playback.aliyundoc.com', 'pv': '1'}
{'host': 'www.replay.aliyundoc.com', 'pv': '1'}
{'host': 'www.webview.aliyundoc.com', 'pv': '1'}
get project ali-test-project log success

Sample code that is used to delete a project

The following sample code provides an example on how to delete the ali-test-project-python project:

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)

if __name__ == '__main__':
    # The name of the project. 
    project_name = "ali-test-project-python"
    # Delete the project. 
    print("ready to delete project")
    client.delete_project(project_name)
    print("delete project %s success " %project_name)

Expected results:

ready to delete project
delete project ali-test-project-python success

References

  • If the response that is returned by Log Service contains error information after you call an API operation, the call fails. You can handle errors based on the error codes that are returned when API calls fail. For more information, see Error codes.
  • 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 or sign requests. For more information, visit OpenAPI Portal.
  • 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 more information about project-related API operations, see the following topics:

  • For more information about sample code, see Alibaba Cloud Simple Log Service SDK for Python on GitHub.