文档

使用Python SDK管理日志库Logstore

更新时间:

日志库(Logstore)是日志服务中数据的采集、存储和查询单元。每个Logstore隶属于一个Project,每个Project中可创建多个Logstore。本文通过代码示例介绍如何创建、修改、查询、删除Logstore等。

前提条件

  • 已创建RAM用户并完成授权。具体操作,请参见创建RAM用户并完成授权

  • 已配置环境变量ALIBABA_CLOUD_ACCESS_KEY_IDALIBABA_CLOUD_ACCESS_KEY_SECRET。具体操作,请参见配置环境变量

    重要
    • 阿里云账号的AccessKey拥有所有API的访问权限,建议您使用RAM用户的AccessKey进行API访问或日常运维。

    • 强烈建议不要把AccessKey ID和AccessKey Secret保存到工程代码里,否则可能导致AccessKey泄露,威胁您账号下所有资源的安全。

  • 已安装日志服务Python SDK。具体操作,请参见安装Python SDK

  • 已创建项目Project。具体操作,请参见创建Project示例代码

注意事项

本示例以华东1(杭州)的公网Endpoint为例,其公网Endpoint为https://cn-hangzhou.log.aliyuncs.com。如果您通过与Project同地域的其他阿里云产品访问日志服务,请使用内网Endpointhttps://cn-hangzhou-intranet.log.aliyuncs.com。关于日志服务支持的地域与Endpoint的对应关系,请参见服务入口

创建Logstore示例代码

以下代码用于创建名为ali-test-logstore的Logstore。

from aliyun.log import LogClient
import os

# 本示例从环境变量中获取AccessKey ID和AccessKey Secret。
accessKeyId = os.environ.get('ALIBABA_CLOUD_ACCESS_KEY_ID', '')
accessKey = os.environ.get('ALIBABA_CLOUD_ACCESS_KEY_SECRET', '')
# 日志服务的服务接入点。此处以杭州为例,其它地域请根据实际情况填写。
endpoint = "cn-hangzhou.log.aliyuncs.com"
# 创建日志服务Client。
client = LogClient(endpoint, accessKeyId, accessKey)

# Project名称。
project_name = "ali-test-project"
# Logstore名称。
logstore_name = "ali-test-logstore"

# 创建Logstore。
def create_logstore():
    print("ready to create logstore %s" %logstore_name)
    # 创建标准型Logstore,数据保存时间为30天,其他参数使用默认值。
    client.create_logstore(project_name, logstore_name, ttl = 30, hot_ttl=-1)
    print("create logstore %s success " %logstore_name)

# 查询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__':
    # 创建Logstore。
    create_logstore()
    # 查询Logstore。
    get_logstore()

预期结果如下:

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

修改Logstore示例代码

以下代码用于修改指定Logstore的配置。

from aliyun.log import LogClient
import os

# 本示例从环境变量中获取AccessKey ID和AccessKey Secret。
accessKeyId = os.environ.get('ALIBABA_CLOUD_ACCESS_KEY_ID', '')
accessKey = os.environ.get('ALIBABA_CLOUD_ACCESS_KEY_SECRET', '')
# 日志服务的服务接入点。此处以杭州为例,其它地域请根据实际情况填写。
endpoint = "cn-hangzhou.log.aliyuncs.com"
# 创建日志服务Client。
client = LogClient(endpoint, accessKeyId, accessKey)

# Project名称。
project_name = "ali-test-project"
# Logstore名称。
logstore_name = "ali-test-logstore"

# 修改Logstore。
def update_logstore():
    print("ready to update logstore %s" %logstore_name)
    # 更新数据保存时间为60天。
    client.update_logstore(project_name,logstore_name,ttl=60)
    print("update logstore %s success " %logstore_name)

# 查询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__':
    # 修改Logstore。
    update_logstore()
    # 查询Logstore。
    get_logstore()

预期结果如下:

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

查询所有Logstore示例代码

以下代码用于查询所有Logstore。

from aliyun.log import LogClient
import os

# 本示例从环境变量中获取AccessKey ID和AccessKey Secret。
accessKeyId = os.environ.get('ALIBABA_CLOUD_ACCESS_KEY_ID', '')
accessKey = os.environ.get('ALIBABA_CLOUD_ACCESS_KEY_SECRET', '')
# 日志服务的服务接入点。此处以杭州为例,其它地域请根据实际情况填写。
endpoint = "cn-hangzhou.log.aliyuncs.com"
# 创建日志服务Client。
client = LogClient(endpoint, accessKeyId, accessKey)

# Project名称。
project_name = "ali-test-project"

if __name__ == '__main__':
    # 查询所有Logstore列表。
    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")

预期结果如下:

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

删除Logstore示例代码

以下代码用于删除名为ali-test-logstore的Logstore。

from aliyun.log import LogClient
import os

# 本示例从环境变量中获取AccessKey ID和AccessKey Secret。
accessKeyId = os.environ.get('ALIBABA_CLOUD_ACCESS_KEY_ID', '')
accessKey = os.environ.get('ALIBABA_CLOUD_ACCESS_KEY_SECRET', '')
# 日志服务的服务接入点。此处以杭州为例,其它地域请根据实际情况填写。
endpoint = "cn-hangzhou.log.aliyuncs.com"
# 创建日志服务Client。
client = LogClient(endpoint, accessKeyId, accessKey)

# Project名称。
project_name = "ali-test-project"

# Logstore名称。
logstore_name = "ali-test-logstore"

if __name__ == '__main__':

    # 删除Logstore。
    print("ready to delete logstore")
    client.delete_logstore(project_name, logstore_name)
    print("delete logstore %s success " %logstore_name)

预期结果如下:

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

相关文档

  • 本页导读 (1)
文档反馈