All Products
Search
Document Center

Simple Log Service:Mengelola logstore dengan SDK Python

Last Updated:Sep 12, 2026

Penyimpanan log adalah unit pengumpulan data, penyimpanan, dan kueri di Simple Log Service. Setiap penyimpanan log termasuk dalam satu proyek, dan setiap proyek dapat berisi beberapa penyimpanan log. Anda dapat menggunakan Java SDK untuk membuat, memperbarui, mengkueri, dan menghapus penyimpanan log.

Prasyarat

Perhatian

Pada contoh ini digunakan endpoint publik Simple Log Service untuk wilayah China (Hangzhou): https://cn-hangzhou.log.aliyuncs.com.

Jika Anda mengakses Simple Log Service dari layanan Alibaba Cloud lain yang berada di wilayah yang sama dengan proyek Anda, gunakan endpoint internal Simple Log Service: https://cn-hangzhou-intranet.log.aliyuncs.com.

Untuk informasi selengkapnya mengenai wilayah dan endpoint Simple Log Service yang didukung, lihat Endpoint.

Buat logstore

Contoh berikut membuat Logstore bernama ali-test-logstore.

from aliyun.log import LogClient
import os

# Contoh ini mengambil ID AccessKey dan Rahasia AccessKey dari variabel lingkungan.
accessKeyId = os.environ.get('ALIBABA_CLOUD_ACCESS_KEY_ID', '')
accessKey = os.environ.get('ALIBABA_CLOUD_ACCESS_KEY_SECRET', '')
# Titik akhir Simple Log Service. Contoh ini menggunakan titik akhir untuk wilayah China (Hangzhou). Ganti dengan titik akhir wilayah Anda.
endpoint = "cn-hangzhou.log.aliyuncs.com"
# Buat client Simple Log Service.
client = LogClient(endpoint, accessKeyId, accessKey)

# Nama proyek.
project_name = "ali-test-project"
# Nama Logstore.
logstore_name = "ali-test-logstore"

# Buat Logstore.
def create_logstore():
    print("ready to create logstore %s" %logstore_name)
    # Buat Logstore standar, atur periode retensi data menjadi 30 hari, dan gunakan nilai default untuk parameter lainnya.
    client.create_logstore(project_name, logstore_name, ttl = 30, hot_ttl=-1)
    print("create logstore %s success " %logstore_name)

# Kueri 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__':
    # Buat Logstore.
    create_logstore()
    # Kueri Logstore.
    get_logstore()

Output yang diharapkan:

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

Perbarui logstore

Contoh berikut memperbarui konfigurasi Logstore.

from aliyun.log import LogClient
import os

# Contoh ini mengambil ID AccessKey dan Rahasia AccessKey dari variabel lingkungan.
accessKeyId = os.environ.get('ALIBABA_CLOUD_ACCESS_KEY_ID', '')
accessKey = os.environ.get('ALIBABA_CLOUD_ACCESS_KEY_SECRET', '')
# Titik akhir Simple Log Service. Contoh ini menggunakan titik akhir untuk wilayah China (Hangzhou). Ganti dengan titik akhir wilayah Anda.
endpoint = "cn-hangzhou.log.aliyuncs.com"
# Buat client Simple Log Service.
client = LogClient(endpoint, accessKeyId, accessKey)

# Nama proyek.
project_name = "ali-test-project"
# Nama Logstore.
logstore_name = "ali-test-logstore"

# Perbarui Logstore.
def update_logstore():
    print("ready to update logstore %s" %logstore_name)
    # Perbarui periode retensi data menjadi 60 hari.
    client.update_logstore(project_name,logstore_name,ttl=60)
    print("update logstore %s success " %logstore_name)

# Kueri 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__':
    # Perbarui Logstore.
    update_logstore()
    # Kueri Logstore.
    get_logstore()

Output yang diharapkan:

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

Daftar semua logstore

Contoh berikut menampilkan daftar semua Logstore dalam suatu proyek.

from aliyun.log import LogClient
import os

# Contoh ini mengambil ID AccessKey dan Rahasia AccessKey dari variabel lingkungan.
accessKeyId = os.environ.get('ALIBABA_CLOUD_ACCESS_KEY_ID', '')
accessKey = os.environ.get('ALIBABA_CLOUD_ACCESS_KEY_SECRET', '')
# Titik akhir Simple Log Service. Contoh ini menggunakan titik akhir untuk wilayah China (Hangzhou). Ganti dengan titik akhir wilayah Anda.
endpoint = "cn-hangzhou.log.aliyuncs.com"
# Buat client Simple Log Service.
client = LogClient(endpoint, accessKeyId, accessKey)

# Nama proyek.
project_name = "ali-test-project"

if __name__ == '__main__':
    # Tampilkan daftar semua 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")

Output yang diharapkan:

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

Hapus logstore

Contoh berikut menghapus Logstore bernama ali-test-logstore.

from aliyun.log import LogClient
import os

# Contoh ini mengambil ID AccessKey dan Rahasia AccessKey dari variabel lingkungan.
accessKeyId = os.environ.get('ALIBABA_CLOUD_ACCESS_KEY_ID', '')
accessKey = os.environ.get('ALIBABA_CLOUD_ACCESS_KEY_SECRET', '')
# Titik akhir Simple Log Service. Contoh ini menggunakan titik akhir untuk wilayah China (Hangzhou). Ganti dengan titik akhir wilayah Anda.
endpoint = "cn-hangzhou.log.aliyuncs.com"
# Buat client Simple Log Service.
client = LogClient(endpoint, accessKeyId, accessKey)

# Nama proyek.
project_name = "ali-test-project"

# Nama Logstore.
logstore_name = "ali-test-logstore"

if __name__ == '__main__':

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

Output yang diharapkan:

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

Dokumen terkait