Simple Log Service Node.js SDK を使用して、プロジェクトおよび Logstore の作成、ログの書き込み、およびログのクエリを行います。
前提条件
-
Simple Log Service が有効化されていること。Simple Log Service の有効化をご参照ください。
Simple Log Service SDK for Node.js がインストールされていること。詳細については、Node.js SDK のインストールをご参照ください。
注意事項
この例では中国 (杭州) リージョンのパブリックエンドポイント https://cn-hangzhou.log.aliyuncs.com を使用します。同一リージョン内の他の Alibaba Cloud サービスから Simple Log Service にアクセスする場合は、内部エンドポイント https://cn-hangzhou-intranet.log.aliyuncs.com を使用してください。サポートされているリージョンとエンドポイントの詳細については、「エンドポイント」をご参照ください。
パラメーター
createProject
createLogStore
createIndex
getLogs
コード例
Node.js コードによるログ収集
以下の例では、SLSQuickStart.js という名前の Node.js スクリプトを使用して、プロジェクト、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,
// Simple Log Service のエンドポイント。この例では中国 (杭州) リージョンのエンドポイントを使用します。ご利用のリージョンのエンドポイントに置き換えてください。
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 ログの収集」をご参照ください。