Buat proyek dan Logstore, tulis log, serta kueri log dengan SDK Python Simple Log Service.
Prasyarat
-
Pengguna RAM dengan izin yang diperlukan. Buat pengguna RAM dan berikan izin.
-
Variabel lingkungan ALIBABA_CLOUD_ACCESS_KEY_ID dan ALIBABA_CLOUD_ACCESS_KEY_SECRET telah dikonfigurasi. Konfigurasikan variabel lingkungan di Linux, macOS, dan Windows.
PentingPasangan AccessKey Akun Alibaba Cloud memberikan izin penuh untuk semua operasi API. Gunakan pasangan AccessKey pengguna RAM untuk panggilan API atau operasi dan pemeliharaan (O&M) rutin.
Jangan menyematkan ID AccessKey dan Secret AccessKey secara langsung dalam kode proyek Anda. Menyematkan kredensial dapat menyebabkan kebocoran tidak disengaja, sehingga membahayakan keamanan seluruh sumber daya dalam akun Anda.
-
SDK Python Simple Log Service telah diinstal. Instal SDK Simple Log Service untuk Python.
Contoh
-
Tulis kode Python untuk mengumpulkan log
Buat file SLSQuickStart.py. Skrip ini membuat proyek, Logstore, dan indeks, menulis log, lalu menjalankan kueri:
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 client 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()Output contoh:
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 ********************* ........Repositori GitHub aliyun-log-python-sdk berisi lebih banyak contoh.
-
Gunakan Logtail untuk mengumpulkan log Python
Kumpulkan log dari modul logging Python dengan Logtail. Kumpulkan log Python.
Referensi
-
Atasi kegagalan panggilan API dengan kode kesalahan yang dikembalikan server. Kode kesalahan API.
-
Simple Log Service mendukung SDK eksklusif maupun SDK umum Alibaba Cloud. Pusat SDK Simple Log Service – Portal Pengembang OpenAPI Alibaba Cloud.
-
Otomatisasi manajemen konfigurasi dengan CLI Simple Log Service.