Todos os produtos
Search
Central de documentação

Simple Log Service:Use o Simple Log Service SDK for Python para gerenciar projetos

Última atualização: Jul 03, 2026

Um projeto do SLS é uma unidade de gerenciamento de recursos para Logstores, Metricstores e grupos de máquinas. Acesse os recursos do SLS pelo endpoint do projeto.

Pré-requisitos

Código de exemplo para criar um projeto

O código a seguir mostra como criar um projeto chamado 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()

Resultados esperados:

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

Código de exemplo para consultar todos os projetos

O exemplo abaixo ilustra a consulta de todos os projetos existentes:

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

Resultados esperados:

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

Código de exemplo para consultar logs em um projeto

Este trecho demonstra como recuperar logs de um Logstore específico dentro de um projeto:

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)

Resultados esperados:

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

Código de exemplo para excluir um projeto

Use o código a seguir para excluir o projeto 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)

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)

Resultados esperados:

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

Referências