全部产品
Search
文档中心

Simple Log Service:Gunakan Simple Log Service SDK untuk Python untuk mengelola Logstores

更新时间:Jul 06, 2025

Logstore dalam Simple Log Service adalah unit yang digunakan untuk mengumpulkan, menyimpan, dan menanyakan data. Setiap Logstore termasuk ke dalam sebuah proyek. Anda dapat membuat beberapa Logstores dalam satu proyek. Topik ini menjelaskan cara membuat, memodifikasi, menanyakan, dan menghapus Logstore dengan menggunakan Simple Log Service SDK untuk Python serta memberikan contoh kode.

Prasyarat

  • Pengguna Resource Access Management (RAM) telah dibuat, dan izin yang diperlukan telah diberikan kepada pengguna RAM. Untuk informasi lebih lanjut, lihat Buat Pengguna RAM dan Berikan Izin kepada Pengguna RAM.

  • Variabel lingkungan ALIBABA_CLOUD_ACCESS_KEY_ID dan ALIBABA_CLOUD_ACCESS_KEY_SECRET telah dikonfigurasi. Untuk informasi lebih lanjut, lihat Konfigurasikan Variabel Lingkungan di Linux, macOS, dan Windows.

    Penting
    • Pasangan AccessKey akun Alibaba Cloud memiliki izin untuk semua operasi API. Kami merekomendasikan agar Anda menggunakan pasangan AccessKey dari pengguna RAM untuk memanggil operasi API atau melakukan pemeliharaan rutin O&M.

    • Kami merekomendasikan agar Anda tidak menyimpan ID AccessKey atau rahasia AccessKey dalam kode proyek Anda. Jika tidak, pasangan AccessKey mungkin bocor, dan keamanan semua sumber daya dalam akun Anda mungkin terganggu.

  • Simple Log Service SDK untuk Python telah diinstal. Untuk informasi lebih lanjut, lihat Instal Simple Log Service SDK untuk Python.

  • Sebuah proyek telah dibuat. Untuk informasi lebih lanjut, lihat Contoh Kode yang Digunakan untuk Membuat Proyek.

Catatan Penggunaan

Dalam contoh ini, titik akhir publik Simple Log Service untuk wilayah China (Hangzhou) digunakan, yaitu https://cn-hangzhou.log.aliyuncs.com. Jika Anda ingin mengakses Simple Log Service dengan menggunakan layanan Alibaba Cloud lainnya yang berada di wilayah yang sama dengan proyek Anda, Anda dapat menggunakan titik akhir internal Simple Log Service, yaitu https://cn-hangzhou-intranet.log.aliyuncs.com. Untuk informasi lebih lanjut tentang wilayah dan titik akhir yang didukung oleh Simple Log Service, lihat Titik Akhir.

Contoh kode yang digunakan untuk membuat Logstore

Berikut ini adalah contoh kode untuk membuat Logstore bernama ali-test-logstore:

from aliyun.log import LogClient
import os

# Konfigurasikan variabel lingkungan. Dalam contoh ini, ID AccessKey dan rahasia AccessKey diperoleh 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. Dalam contoh ini, titik akhir Simple Log Service untuk wilayah China (Hangzhou) digunakan. Ganti nilai parameter dengan titik akhir sebenarnya.
endpoint = "cn-hangzhou.log.aliyuncs.com"
# Buat klien 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("siap untuk membuat logstore %s" %logstore_name)
    # Buat Logstore Standar. Atur periode retensi data menjadi 30 hari dan pertahankan pengaturan default untuk parameter lainnya.
    client.create_logstore(project_name, logstore_name, ttl = 30, hot_ttl=-1)
    print("buat logstore %s berhasil " %logstore_name)

# Tanyakan Logstore.
def get_logstore():
    print("siap untuk mendapatkan logstore")
    res = client.get_logstore(project_name, logstore_name)
    res.log_print()
    print("dapatkan logstore berhasil ")


if __name__ == '__main__':
    # Buat Logstore.
    create_logstore()
    # Tanyakan Logstore.
    get_logstore()

Hasil yang Diharapkan:

siap untuk membuat logstore ali-test-logstore
buat logstore ali-test-logstore berhasil
siap untuk mendapatkan 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
dapatkan logstore berhasil

Contoh kode yang digunakan untuk memodifikasi Logstore

Berikut ini adalah contoh kode untuk memodifikasi konfigurasi Logstore tertentu:

from aliyun.log import LogClient
import os

# Konfigurasikan variabel lingkungan. Dalam contoh ini, ID AccessKey dan rahasia AccessKey diperoleh 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. Dalam contoh ini, titik akhir Simple Log Service untuk wilayah China (Hangzhou) digunakan. Ganti nilai parameter dengan titik akhir sebenarnya.
endpoint = "cn-hangzhou.log.aliyuncs.com"
# Buat klien Simple Log Service.
client = LogClient(endpoint, accessKeyId, accessKey)

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

# Modifikasi Logstore.
def update_logstore():
    print("siap untuk memperbarui logstore %s" %logstore_name)
    # Ubah periode retensi data menjadi 60 hari.
    client.update_logstore(project_name,logstore_name,ttl=60)
    print("memperbarui logstore %s berhasil " %logstore_name)

# Tanyakan Logstore.
def get_logstore():
    print("siap untuk mendapatkan logstore")
    res = client.get_logstore(project_name, logstore_name)
    res.log_print()
    print("dapatkan logstore berhasil ")


if __name__ == '__main__':
    # Modifikasi Logstore.
    update_logstore()
    # Tanyakan Logstore.
    get_logstore()

Hasil yang Diharapkan:

siap untuk memperbarui logstore ali-test-logstore
memperbarui logstore ali-test-logstore berhasil
siap untuk mendapatkan 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
dapatkan logstore berhasil

Contoh kode yang digunakan untuk menanyakan semua Logstores

Berikut ini adalah contoh kode untuk menanyakan semua Logstores:

from aliyun.log import LogClient
import os

# Konfigurasikan variabel lingkungan. Dalam contoh ini, ID AccessKey dan rahasia AccessKey diperoleh 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. Dalam contoh ini, titik akhir Simple Log Service untuk wilayah China (Hangzhou) digunakan. Ganti nilai parameter dengan titik akhir sebenarnya.
endpoint = "cn-hangzhou.log.aliyuncs.com"
# Buat klien Simple Log Service.
client = LogClient(endpoint, accessKeyId, accessKey)

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

if __name__ == '__main__':
    # Tanyakan semua Logstores.
    print("siap untuk mencantumkan logstore")
    res = client.list_logstore(project_name, None, 0, 100)

    for logstore in res.get_logstores():
        print(logstore)

    print("cantumkan logstore berhasil")

Hasil yang Diharapkan:

siap untuk mencantumkan logstore
ali-test-logstore
ali-test-logstore2
ali-test-webtracking
cantumkan logstore berhasil

Contoh kode yang digunakan untuk menghapus Logstore

Berikut ini adalah contoh kode untuk menghapus Logstore ali-test-logstore:

from aliyun.log import LogClient
import os

# Konfigurasikan variabel lingkungan. Dalam contoh ini, ID AccessKey dan rahasia AccessKey diperoleh 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. Dalam contoh ini, titik akhir Simple Log Service untuk wilayah China (Hangzhou) digunakan. Ganti nilai parameter dengan titik akhir sebenarnya.
endpoint = "cn-hangzhou.log.aliyuncs.com"
# Buat klien 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("siap untuk menghapus logstore")
    client.delete_logstore(project_name, logstore_name)
    print("hapus logstore %s berhasil " %logstore_name)

Hasil yang Diharapkan:

siap untuk menghapus logstore
hapus logstore ali-test-logstore berhasil

Referensi

  • Alibaba Cloud OpenAPI Explorer menyediakan kemampuan debugging, SDK, contoh, dan dokumen terkait. Anda dapat menggunakan OpenAPI Explorer untuk men-debug operasi API Simple Log Service tanpa perlu secara manual mengenkapsulasi atau menandatangani permintaan. Untuk informasi lebih lanjut, kunjungi OpenAPI Portal.

  • Simple Log Service menyediakan antarmuka baris perintah (CLI) untuk memenuhi persyaratan konfigurasi otomatis dalam Simple Log Service. Untuk informasi lebih lanjut, lihat Simple Log Service CLI.

  • Untuk informasi tentang operasi API terkait Logstore, lihat topik-topik berikut:

  • Untuk informasi lebih lanjut tentang contoh kode, lihat Alibaba Cloud Simple Log Service SDK untuk Python di GitHub.