すべてのプロダクト
Search
ドキュメントセンター

Simple Log Service:Python SDK を使用した Logstore の管理

最終更新日:Sep 12, 2026

Logstore は、Simple Log Service におけるデータ収集、保存、およびクエリの単位です。各 Logstore はプロジェクトに属し、各プロジェクトには複数の Logstore を含めることができます。Python SDK を使用して、Logstore の作成、更新、クエリ、および削除を実行できます。

前提条件

注意事項

この例では、中国 (杭州) リージョンのパブリックエンドポイントを使用しています。エンドポイント: https://cn-hangzhou.log.aliyuncs.com

プロジェクトと同じリージョンにある他の Alibaba Cloud サービスから Simple Log Service にアクセスする場合は、内部エンドポイント https://cn-hangzhou-intranet.log.aliyuncs.com を使用できます。

Simple Log Service がサポートするリージョンとエンドポイントの詳細については、「エンドポイント」をご参照ください。

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', '')
# Simple Log Service のエンドポイント。この例では中国 (杭州) リージョンのエンドポイントを使用します。お使いのリージョンのエンドポイントに置き換えてください。
endpoint = "cn-hangzhou.log.aliyuncs.com"
client = LogClient(endpoint, accessKeyId, accessKey)

# プロジェクトの名前。
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-logstore
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', '')
# Simple Log Service のエンドポイント。この例では中国 (杭州) リージョンのエンドポイントを使用します。お使いのリージョンのエンドポイントに置き換えてください。
endpoint = "cn-hangzhou.log.aliyuncs.com"
# Simple Log Service クライアントを作成します。
client = LogClient(endpoint, accessKeyId, accessKey)

# プロジェクトの名前。
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-logstore
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', '')
# Simple Log Service のエンドポイント。この例では中国 (杭州) リージョンのエンドポイントを使用します。お使いのリージョンのエンドポイントに置き換えてください。
endpoint = "cn-hangzhou.log.aliyuncs.com"
# Simple Log Service クライアントを作成します。
client = LogClient(endpoint, accessKeyId, accessKey)

# プロジェクトの名前。
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', '')
# Simple Log Service のエンドポイント。この例では中国 (杭州) リージョンのエンドポイントを使用します。お使いのリージョンのエンドポイントに置き換えてください。
endpoint = "cn-hangzhou.log.aliyuncs.com"
# Simple Log Service クライアントを作成します。
client = LogClient(endpoint, accessKeyId, accessKey)

# プロジェクトの名前。
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

関連ドキュメント