このトピックでは、Simple Log Service (SLS) SDK for Node.js を使用して、プロジェクトの作成、Logstore の作成、ログの書き込み、ログのクエリなどの一般的な操作を実行する方法について説明します。
前提条件
Simple Log Service が 有効化されています。
Simple Log Service SDK for Node.js がインストールされています。詳細については、「Simple Log Service SDK for Node.js をインストールする」をご参照ください。
重要
この例では、中国 (杭州) リージョンのパブリックエンドポイント https://cn-hangzhou.log.aliyuncs.com を使用します。プロジェクトと同じリージョンにある別の Alibaba Cloud サービスから SLS にアクセスする場合は、内部エンドポイント https://cn-hangzhou-intranet.log.aliyuncs.com を使用します。SLS がサポートするリージョンとエンドポイントの詳細については、「エンドポイント」をご参照ください。
パラメーター
createProject
createLogStore
createIndex
getLogs
例
Node.js コードを記述してログを収集する
この例では、SLSQuickStart.js という名前のファイルが作成されます。このファイルは API 操作を呼び出して、プロジェクトの作成、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,
// SLS のエンドポイント。この例では、中国 (杭州) リージョンのエンドポイントを使用します。実際のエンドポイントに置き換えてください。
endpoint: 'cn-hangzhou.log.aliyuncs.com'
})
// 必須。プロジェクトの名前。
const projectName = "aliyun-test-node-project"
// 必須。Logstore の名前。
const logstoreName = "request_log"
async function test() {
// プロジェクトを作成します。
await sls.createProject(projectName, {
description: 'test'
})
// Logstore を作成します。
await sls.createLogStore(projectName, logstoreName, {
// 必須。データ保持期間 (日数)。値 3650 は、データが永続的に保存されることを示します。
ttl: 3600,
// 必須。シャードの数。
shardCount: 2
})
// インデックスを作成します。
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: クエリ文を使用して、過去 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)
}
// 関数を実行します。
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 ソースコード | 説明 |
プロジェクト、Logstore、インデックスの作成、ログの書き込み、ログと Logstore のクエリ、ログの分散の取得方法の例。 |
Logtail を使用して Node.js ログを収集する
Logtail を使用して Node.js アプリケーションから log4js ログを収集する方法の例については、「Node.js ログの収集」をご参照ください。