Manage projects with the Python SDK

Updated at:

An SLS project is a resource management unit for Logstores, Metricstores, and machine groups. Access SLS resources through the project endpoint.

Prerequisites

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 project

The following example creates a project named ali-test-project-python.

from aliyun.log import LogClient
import os

# This example retrieves credentials from environment variables.
# For production environments, we recommend using a more secure method, such as STS tokens or RAM roles.
accessKeyId = os.environ.get('ALIBABA_CLOUD_ACCESS_KEY_ID', '')
accessKey = os.environ.get('ALIBABA_CLOUD_ACCESS_KEY_SECRET', '')
# The service endpoint for Log Service. This example uses the endpoint for the China (Hangzhou) region. 
# Replace it with the endpoint for your region.
endpoint = "cn-hangzhou.log.aliyuncs.com"
# Initialize the Log Service client.
client = LogClient(endpoint, accessKeyId, accessKey)

# The name of the project.
project_name = "ali-test-project-python"

# Create a 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)

# Get project details.
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()
    # Get the project details to verify the creation.
    get_project()

Expected output:

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

List projects

The following example lists all projects in the current region.

from aliyun.log import LogClient, ListProjectResponse
import os

# This example retrieves credentials from environment variables.
accessKeyId = os.environ.get('ALIBABA_CLOUD_ACCESS_KEY_ID', '')
accessKey = os.environ.get('ALIBABA_CLOUD_ACCESS_KEY_SECRET', '')
# The service endpoint for Log Service. This example uses the endpoint for the China (Hangzhou) region.
endpoint = "cn-hangzhou.log.aliyuncs.com"
# Initialize the Log Service client.
client = LogClient(endpoint, accessKeyId, accessKey)

if __name__ == '__main__':
    # List 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 output:

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

Query logs in a project

The following example queries logs in a specific Logstore within a project.

from aliyun.log import LogClient, GetProjectLogsRequest
import os

# This example retrieves credentials from environment variables.
accessKeyId = os.environ.get('ALIBABA_CLOUD_ACCESS_KEY_ID', '')
accessKey = os.environ.get('ALIBABA_CLOUD_ACCESS_KEY_SECRET', '')
# The service endpoint for Log Service. This example uses the endpoint for the China (Hangzhou) region.
endpoint = "cn-hangzhou.log.aliyuncs.com"
# Initialize the 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)
    # This SQL statement queries the top 10 hosts with 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 output:

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

Delete a project

The following example deletes the project named ali-test-project-python.

from aliyun.log import LogClient
import os

# This example retrieves credentials from environment variables.
accessKeyId = os.environ.get('ALIBABA_CLOUD_ACCESS_KEY_ID', '')
accessKey = os.environ.get('ALIBABA_CLOUD_ACCESS_KEY_SECRET', '')
# The service endpoint for Log Service. This example uses the endpoint for the China (Hangzhou) region.
endpoint = "cn-hangzhou.log.aliyuncs.com"
# Initialize the 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 output:

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

Related documents