全部產品
Search
文件中心

Simple Log Service:使用Python SDK管理日誌庫LogStore

更新時間:Jan 06, 2026

日誌庫(Logstore)是Log Service中資料的採集、儲存和查詢單元。每個Logstore隸屬於一個Project,每個Project中可建立多個Logstore。本文通過程式碼範例介紹如何建立、修改、查詢、刪除Logstore等。

前提條件

您已完成以下操作:

注意事項

本樣本以華東1(杭州)的公網Endpoint為例,其公網Endpoint為https://cn-hangzhou.log.aliyuncs.com

如果您通過與Project同地區的其他阿里雲產品訪問Log Service,請使用內網Endpointhttps://cn-hangzhou-intranet.log.aliyuncs.com

關於Log Service支援的地區與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', '')
# Log Service的服務存取點。此處以杭州為例,其它地區請根據實際情況填寫。
endpoint = "cn-hangzhou.log.aliyuncs.com"
# 建立Log ServiceClient。
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', '')
# Log Service的服務存取點。此處以杭州為例,其它地區請根據實際情況填寫。
endpoint = "cn-hangzhou.log.aliyuncs.com"
# 建立Log ServiceClient。
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', '')
# Log Service的服務存取點。此處以杭州為例,其它地區請根據實際情況填寫。
endpoint = "cn-hangzhou.log.aliyuncs.com"
# 建立Log ServiceClient。
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', '')
# Log Service的服務存取點。此處以杭州為例,其它地區請根據實際情況填寫。
endpoint = "cn-hangzhou.log.aliyuncs.com"
# 建立Log ServiceClient。
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

相關文檔