Topik ini menjelaskan cara menggunakan Simple Log Service (SLS) SDK for Node.js untuk melakukan operasi umum seperti membuat Proyek, membuat logstore, menulis log, dan mengkueri log.
Prasyarat
Simple Log Service telah diaktifkan.
SDK Simple Log Service untuk Node.js telah diinstal. Untuk informasi lebih lanjut, lihat Instal SDK Simple Log Service untuk Node.js.
Penting
Contoh ini menggunakan titik akhir publik wilayah Tiongkok (Hangzhou): https://cn-hangzhou.log.aliyuncs.com. Jika Anda mengakses SLS dari layanan Alibaba Cloud lainnya di wilayah yang sama dengan proyek Anda, gunakan titik akhir internal: https://cn-hangzhou-intranet.log.aliyuncs.com. Untuk informasi lebih lanjut tentang wilayah dan titik akhir yang didukung oleh SLS, lihat Titik Akhir.
Parameter
createProject
createLogStore
createIndex
getLogs
Contoh
Tulis kode Node.js untuk mengumpulkan log
Dalam contoh ini, file bernama SLSQuickStart.js dibuat. File tersebut memanggil operasi API untuk membuat proyek, membuat logstore, membuat indeks, menulis data log, dan menanyakan data log. Berikut adalah contoh kode:
const Client = require('@alicloud/log')
const sls = new Client({
// Dalam contoh ini, ID AccessKey dan Rahasia AccessKey diperoleh dari variabel lingkungan.
accessKeyId: process.env.ALIBABA_CLOUD_ACCESS_KEY_ID,
accessKeySecret: process.env.ALIBABA_CLOUD_ACCESS_KEY_SECRET,
// Titik akhir SLS. Contoh ini menggunakan titik akhir wilayah Tiongkok (Hangzhou). Gantilah dengan titik akhir aktual.
endpoint: 'cn-hangzhou.log.aliyuncs.com'
})
// Diperlukan. Nama proyek.
const projectName = "aliyun-test-node-project"
// Diperlukan. Nama logstore.
const logstoreName = "request_log"
async function test() {
// Buat proyek.
await sls.createProject(projectName, {
description: 'test'
})
// Buat logstore.
await sls.createLogStore(projectName, logstoreName, {
// Diperlukan. Periode retensi data dalam hari. Nilai 3650 menunjukkan bahwa data disimpan secara permanen.
ttl: 3600,
// Diperlukan. Jumlah shard.
shardCount: 2
})
// Buat indeks.
const index = {
"keys": {
"request_method": {
// Menentukan apakah kueri peka huruf besar-kecil. false menunjukkan bahwa kueri tidak peka huruf besar-kecil.
"caseSensitive": false,
// Menentukan apakah akan mengaktifkan analisis statistik untuk bidang tersebut.
"doc_value": true,
"token": ["\n", "\t", ";", ",", "=", ":"],
"type": "text"
}, "status": {
// Menentukan apakah kueri peka huruf besar-kecil. false menunjukkan bahwa kueri tidak peka huruf besar-kecil.
"caseSensitive": false,
// Menentukan apakah akan mengaktifkan analisis statistik untuk bidang tersebut.
"doc_value": true,
"token": ["\n", "\t", ";", ",", "=", ":"],
"type": "long"
}
},
}
await sls.createIndex(projectName, logstoreName, index)
// Tulis log.
const logGroup = {
logs: [
{ content: { request_method: 'GET', status: '200' }, timestamp: Math.floor(new Date().getTime() / 1000) },
{ content: { request_method: 'GET', status: '500' }, timestamp: Math.floor(new Date().getTime() / 1000) },
{ content: { request_method: 'GET', status: '200' }, timestamp: Math.floor(new Date().getTime() / 1000) },
{ content: { request_method: 'POST', status: '500'}, timestamp: Math.floor(new Date().getTime() / 1000) }
],
tags: [{ tag1: 'testTag' }],
topic: 'testTopic',
source: 'testSource'
};
await sls.postLogStoreLogs(projectName, logstoreName, logGroup);
// Contoh kueri 1: Kueri data log selama satu hari terakhir.
const from = new Date();
from.setDate(from.getDate() - 1);
const to = new Date();
const res = await sls.getLogs(projectName, logstoreName, from, to);
// Contoh kueri 2: Gunakan pernyataan kueri untuk menghitung jumlah log dalam 10 menit terakhir.
// const from = new Date();
// from.setSeconds(from.getSeconds() - 600)
// const to = new Date();
// query = '* | select count(*) as count';
// topic = 'testTopic';
// const res = await sls.getLogs(projectName,logstoreName,from,to,{
// query: query,
// topic: topic,
// line: 100,
// offset: 0,
// reverse: false,
// powersql: false
// });
console.log(res)
}
// Jalankan fungsi.
test()
Berikut adalah contoh tanggapan:
[
{
request_method: 'GET',
status: '200',
__topic__: 'testTopic',
__source__: 'testSource',
'__tag__:tag1': 'testTag',
__time__: '1744882259'
},
{
request_method: 'GET',
status: '500',
__topic__: 'testTopic',
__source__: 'testSource',
'__tag__:tag1': 'testTag',
__time__: '1744882259'
},
{
request_method: 'GET',
status: '200',
__topic__: 'testTopic',
__source__: 'testSource',
'__tag__:tag1': 'testTag',
__time__: '1744882259'
},
{
request_method: 'POST',
status: '500',
__topic__: 'testTopic',
__source__: 'testSource',
'__tag__:tag1': 'testTag',
__time__: '1744882259'
}
]Tabel berikut memberikan lebih banyak contoh kode untuk referensi Anda.
Sumber kode GitHub | Deskripsi |
Contoh cara membuat proyek, logstore, dan indeks, menulis log, menanyakan log dan logstore, serta mendapatkan distribusi log. |
Kumpulkan log Node.js menggunakan Logtail
Untuk contoh cara menggunakan Logtail untuk mengumpulkan log log4js dari aplikasi Node.js, lihat Kumpulkan log Node.js.