本文介紹如何快速使用Log ServiceNode.js SDK完成常見操作,包括建立專案(Project)、建立日誌庫(Logstore)、寫入日誌和查詢日誌等。
前提條件
已開通Log Service。更多資訊,請參見開通Log Service。
已安裝Log ServiceNode.js SDK。具體操作,請參見安裝Node.js SDK。
注意事項
本樣本以華東1(杭州)的公網Endpoint為例,其公網Endpoint為https://cn-hangzhou.log.aliyuncs.com。如果您通過與Project同地區的其他阿里雲產品訪問Log Service,請使用內網Endpointhttps://cn-hangzhou-intranet.log.aliyuncs.com。關於Log Service支援的地區與Endpoint的對應關係,請參見服務入口。
參數說明
createProject
createLogStore
createIndex
getLogs
樣本
編寫Node.js代碼採集日誌
本樣本中,建立一個SLSQuickStart.js檔案,並調用介面分別完成建立Project、建立Logstore、建立索引、寫入日誌資料和查詢日誌資料。以下為範例程式碼:
const Client = require('@alicloud/log')
const sls = new Client({
// 本樣本從環境變數中擷取AccessKey ID和AccessKey Secret。
accessKeyId: process.env.ALIBABA_CLOUD_ACCESS_KEY_ID,
accessKeySecret: process.env.ALIBABA_CLOUD_ACCESS_KEY_SECRET,
//Log Service的網域名稱。此處以杭州為例,其它地區請根據實際情況填寫。
endpoint: 'cn-hangzhou.log.aliyuncs.com'
})
// 必選,Project名稱。
const projectName = "aliyun-test-node-project"
// 必選,Logstore名稱。
const logstoreName = "request_log"
async function test() {
// 建立Project
await sls.createProject(projectName, {
description: 'test'
})
// 建立Logstore
await sls.createLogStore(projectName, logstoreName, {
// 必選,設定資料儲存時間長度,單位為天。如果ttl配置為3650,表示永久儲存。
ttl: 3600,
// 必選,設定Shard數量。
shardCount: 2
})
// 建立index
const index = {
"keys": {
"request_method": {
// 是否大小寫敏感 false為大小寫不敏感。
"caseSensitive": false,
// 是否對該欄位開啟統計分析
"doc_value": true,
"token": ["\n", "\t", ";", ",", "=", ":"],
"type": "text"
}, "status": {
// 是否大小寫敏感 false為大小寫不敏感。
"caseSensitive": false,
// 是否對該欄位開啟統計分析
"doc_value": true,
"token": ["\n", "\t", ";", ",", "=", ":"],
"type": "long"
}
},
}
await sls.createIndex(projectName, logstoreName, index)
// 寫入日誌
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);
// 查詢日誌樣本1:查詢最近1天的日誌資料。
const from = new Date();
from.setDate(from.getDate() - 1);
const to = new Date();
const res = await sls.getLogs(projectName, logstoreName, from, to);
// 查詢日誌樣本2:通過query分析語句,統計最近10分鐘的日誌條數。
// 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)
}
// 運行function。
test()
返回結果樣本如下:
[
{
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'
}
]更多範例程式碼如下表,方便您參考或直接使用。
GitHub源碼 | 說明 |
建立Project、建立Logstore、建立索引、寫入日誌、查詢日誌、查詢Logstore、擷取日誌分布情況等相關樣本。 |
通過Logtail採集Node.js日誌
通過Logtail方式,以採集Node.js的log4js日誌為例,請參見採集Node.js日誌。