Topik ini menjelaskan cara menggunakan SDK Simple Log Service untuk Python guna menjalankan operasi umum, seperti membuat proyek, membuat Logstore, menulis log, dan mengkueri log.
Prasyarat
Buat dan otorisasi pengguna RAM. Untuk informasi selengkapnya, lihat Buat RAM user dan berikan izin.
Konfigurasikan variabel lingkungan ALIBABA_CLOUD_ACCESS_KEY_ID dan ALIBABA_CLOUD_ACCESS_KEY_SECRET. Untuk informasi selengkapnya, lihat Konfigurasikan variabel lingkungan di Linux, macOS, dan Windows.
PentingPasangan AccessKey milik Akun Alibaba Cloud memberikan izin penuh untuk semua operasi API. Kami menyarankan Anda menggunakan pasangan AccessKey dari pengguna RAM untuk panggilan API atau operasi dan pemeliharaan (O&M) rutin.
Demi keamanan, kami sangat menyarankan agar Anda tidak menyematkan ID AccessKey dan Secret AccessKey secara langsung dalam kode proyek Anda. Menyematkan kredensial dapat menyebabkan paparan yang tidak disengaja, sehingga membahayakan keamanan seluruh sumber daya dalam akun Anda.
Instal SDK Simple Log Service untuk Python. Untuk informasi selengkapnya, lihat Instal SDK Simple Log Service untuk Python.
Contoh
Tulis kode Python untuk mengumpulkan log
Pada contoh ini, Anda membuat file bernama SLSQuickStart.py. Skrip ini memanggil operasi API untuk membuat proyek, membuat Logstore, membuat indeks, menulis data log, dan mengkueri data log. Kode berikut merupakan contohnya:
from aliyun.log import LogClient, PutLogsRequest, LogItem, GetLogsRequest, IndexConfig import time import os # Contoh ini mengambil ID AccessKey dan Secret AccessKey dari variabel lingkungan. accessKeyId = os.environ.get('ALIBABA_CLOUD_ACCESS_KEY_ID', '') accessKey = os.environ.get('ALIBABA_CLOUD_ACCESS_KEY_SECRET', '') # Titik akhir untuk Simple Log Service. Contoh ini menggunakan titik akhir untuk wilayah China (Hangzhou). Ganti dengan titik akhir aktual Anda. endpoint = "cn-hangzhou.log.aliyuncs.com" # Buat klien Simple Log Service. client = LogClient(endpoint, accessKeyId, accessKey) # Nama proyek. project_name = "aliyun-test-project" # Nama Logstore. logstore_name = "aliyun-test-logstore" # Pernyataan kueri. query = "*| select dev,id from " + logstore_name # Konfigurasi indeks. logstore_index = {'line': { 'token': [',', ' ', "'", '"', ';', '=', '(', ')', '[', ']', '{', '}', '?', '@', '&', '<', '>', '/', ':', '\n', '\t', '\r'], 'caseSensitive': False, 'chn': False}, 'keys': {'dev': {'type': 'text', 'token': [',', ' ', "'", '"', ';', '=', '(', ')', '[', ']', '{', '}', '?', '@', '&', '<', '>', '/', ':', '\n', '\t', '\r'], 'caseSensitive': False, 'alias': '', 'doc_value': True, 'chn': False}, 'id': {'type': 'long', 'alias': '', 'doc_value': True}}, 'log_reduce': False, 'max_text_len': 2048} # Parameter from_time dan to_time menentukan rentang waktu untuk kueri, dalam format timestamp Unix. from_time = int(time.time()) - 3600 to_time = time.time() + 3600 # Buat proyek. def create_project(): print("ready to create project %s" % project_name) client.create_project(project_name, project_des="") print("create project %s success " % project_name) time.sleep(60) # Buat Logstore. def create_logstore(): print("ready to create logstore %s" % logstore_name) client.create_logstore(project_name, logstore_name, ttl=3, shard_count=2) print("create logstore %s success " % project_name) time.sleep(30) # Buat indeks. def create_index(): print("ready to create index for %s" % logstore_name) index_config = IndexConfig() index_config.from_json(logstore_index) client.create_index(project_name, logstore_name, index_config) print("create index for %s success " % logstore_name) time.sleep(60 * 2) # Tulis data ke Logstore. def put_logs(): print("ready to put logs for %s" % logstore_name) log_group = [] for i in range(0, 100): log_item = LogItem() contents = [ ('dev', 'test_put'), ('id', str(i)) ] log_item.set_contents(contents) log_group.append(log_item) request = PutLogsRequest(project_name, logstore_name, "", "", log_group, compress=False) client.put_logs(request) print("put logs for %s success " % logstore_name) time.sleep(60) # Kueri log. def get_logs(): print("ready to query logs from logstore %s" % logstore_name) request = GetLogsRequest(project_name, logstore_name, from_time, to_time, query=query) response = client.get_logs(request) for log in response.get_logs(): for k, v in log.contents.items(): print("%s : %s" % (k, v)) print("*********************") if __name__ == '__main__': # Buat proyek. create_project() # Buat Logstore. create_logstore() # Buat indeks. create_index() # Tulis data ke Logstore. put_logs() # Kueri log. get_logs()Contoh output:
ready to create project aliyun-test-project create project aliyun-test-project success ready to create logstore aliyun-test-logstore create logstore aliyun-test-project success ready to create index for aliyun-test-logstore create index for aliyun-test-logstore success ready to put logs for aliyun-test-logstore put logs for aliyun-test-logstore success ready to query logs from logstore aliyun-test-logstore dev : test_put id : 0 ********************* dev : test_put id : 1 ********************* dev : test_put id : 2 ********************* dev : test_put id : 3 ********************* ........Untuk contoh kode lainnya, lihat aliyun-log-python-sdk.
Gunakan Logtail untuk mengumpulkan log Python
Anda dapat menggunakan Logtail untuk mengumpulkan log dari modul logging Python. Untuk informasi selengkapnya, lihat Kumpulkan log Python.
Referensi
Jika panggilan API gagal, server akan mengembalikan kode kesalahan yang dapat Anda gunakan untuk troubleshooting. Untuk informasi selengkapnya, lihat Kode kesalahan API.
Selain SDK eksklusifnya, Simple Log Service juga mendukung SDK Alibaba Cloud umum. Untuk informasi lebih lanjut tentang cara menggunakan SDK Alibaba Cloud, lihat Pusat SDK Simple Log Service – Portal Pengembang OpenAPI Alibaba Cloud.
Simple Log Service juga menyediakan command-line interface (CLI) untuk konfigurasi otomatis. Untuk informasi selengkapnya, lihat CLI Simple Log Service.