Gunakan Simple Log Service Node.js SDK untuk membuat proyek dan Logstore, menulis log, serta mengkueri log.
Prasyarat
-
Simple Log Service telah diaktifkan. Aktifkan Simple Log Service.
Simple Log Service SDK untuk Node.js telah diinstal. Untuk informasi selengkapnya, lihat Instal Node.js SDK.
Catatan penggunaan
Contoh ini menggunakan titik akhir publik China (Hangzhou): https://cn-hangzhou.log.aliyuncs.com. Untuk mengakses Simple Log Service dari layanan Alibaba Cloud lainnya dalam wilayah yang sama, gunakan titik akhir internal: https://cn-hangzhou-intranet.log.aliyuncs.com. Untuk informasi selengkapnya mengenai wilayah dan titik akhir yang didukung, lihat Titik Akhir.
Parameter
createProject
createLogStore
createIndex
getLogs
Contoh
Kumpulkan log dengan kode Node.js
Contoh berikut menggunakan skrip Node.js bernama SLSQuickStart.js untuk membuat proyek, Logstore, dan indeks, lalu menulis dan mengkueri data log.
const Client = require('@alicloud/log')
const sls = new Client({
// Pada contoh ini, ID AccessKey dan Rahasia AccessKey dibaca dari variabel lingkungan.
accessKeyId: process.env.ALIBABA_CLOUD_ACCESS_KEY_ID,
accessKeySecret: process.env.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'
})
// Wajib. Nama proyek.
const projectName = "aliyun-test-node-project"
// Wajib. Nama Logstore.
const logstoreName = "request_log"
async function test() {
// Buat proyek.
await sls.createProject(projectName, {
description: 'test'
})
// Buat Logstore.
await sls.createLogStore(projectName, logstoreName, {
// Wajib. Periode retensi data dalam hari. Nilai 3650 berarti data disimpan secara permanen.
ttl: 3600,
// Wajib. Jumlah shard.
shardCount: 2
})
// Buat indeks.
const index = {
"keys": {
"request_method": {
// Menentukan apakah kueri peka huruf besar/kecil. `false` membuat kueri tidak peka huruf besar/kecil.
"caseSensitive": false,
// Menentukan apakah akan mengaktifkan analisis statistik untuk bidang ini.
"doc_value": true,
"token": ["\n", "\t", ";", ",", "=", ":"],
"type": "text"
}, "status": {
// Menentukan apakah kueri peka huruf besar/kecil. `false` membuat kueri tidak peka huruf besar/kecil.
"caseSensitive": false,
// Menentukan apakah akan mengaktifkan analisis statistik untuk bidang ini.
"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 log dari hari kemarin.
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 analitik 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()
Contoh respons:
[
{
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 mencantumkan lebih banyak contoh kode.
|
Kode sumber GitHub |
Deskripsi |
|
Buat proyek, Logstore, dan indeks. Tulis dan kueri log serta Logstore. Dapatkan distribusi log. |
Kumpulkan log Node.js dengan Logtail
Untuk informasi tentang cara menggunakan Logtail untuk mengumpulkan log log4js dari aplikasi Node.js, lihat Kumpulkan log Node.js.