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

前提条件

  • 已开通日志服务。更多信息,请参见开通日志服务
  • 已创建并获取AccessKey。更多信息,请参见访问密钥

    阿里云账号AccessKey拥有所有API的访问权限,风险很高。强烈建议您创建并使用RAM用户进行API访问或日常运维。RAM用户需具备操作日志服务资源的权限。具体操作,请参见为RAM用户授权

  • 已安装Python开发环境。更多信息,请参见Python官网

    日志服务Python SDK支持Pypy2、Pypy3、Python2.6、Python2.7、Python3.3、Python3.4、Python3.5、Python3.6、Python3.7、Python3.8和Python3.9版本。您可以执行python -V命令检查您已安装的Python版本。

  • 已安装日志服务Python SDK。具体操作,请参见安装Java SDK
  • 已创建项目Project。具体操作,请参见创建Project示例代码

注意事项

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

创建Logstore示例代码

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

from aliyun.log import LogClient

# 阿里云账号AccessKey拥有所有API的访问权限,风险很高。强烈建议您创建并使用RAM用户进行API访问或日常运维。
accessKeyId = "yourAccessKeyId"
accessKey = "yourAccessKeySecret"
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

# 阿里云账号AccessKey拥有所有API的访问权限,风险很高。强烈建议您创建并使用RAM用户进行API访问或日常运维。
accessKeyId = "yourAccessKeyId"
accessKey = "yourAccessKeySecret"
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

# 阿里云账号AccessKey拥有所有API的访问权限,风险很高。强烈建议您创建并使用RAM用户进行API访问或日常运维。
accessKeyId = "yourAccessKeyId"
accessKey = "yourAccessKeySecret"
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

# 阿里云账号AccessKey拥有所有API的访问权限,风险很高。强烈建议您创建并使用RAM用户进行API访问或日常运维。
accessKeyId = "yourAccessKeyId"
accessKey = "yourAccessKeySecret"
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

相关文档