このトピックでは、Simple Log Service SDK for Node.js を使い始める方法と、プロジェクトとログストアの作成、ログの書き込みとクエリなどの一般的な操作を実行する方法について説明します。
前提条件
考慮事項
この例では、シンガポールリージョン ap-southeast-1.log.aliyuncs.com
のパブリックエンドポイントを使用しています。プロジェクトと同じリージョンにある別の Alibaba Cloud サービスから Simple Log Service にアクセスする場合は、内部エンドポイント ap-southeast-1-intranet.log.aliyuncs.com
を使用します。リージョンとエンドポイントの対応関係については、「エンドポイント」をご参照ください。
例
Node.js コードを記述してログを収集します。
この例では、SLSQuickStart.js ファイルを作成し、インターフェイスを呼び出して、プロジェクト、ログストア、およびインデックスを作成し、ログデータの書き込みとクエリを実行します。サンプルコード:
const Client = require('@alicloud/log') const sls = new Client({ // この例では、AccessKey ID と AccessKey シークレットは環境変数から取得されます。 accessKeyId: process.env.ALIBABA_CLOUD_ACCESS_KEY_ID, accessKeySecret: process.env.ALIBABA_CLOUD_ACCESS_KEY_SECRET, // Simple Log Service エンドポイントを指定します。 endpoint: 'ap-southeast-1.log.aliyuncs.com' }) // 必須。プロジェクトの名前。 const projectName = "aliyun-test-node-project" // 必須。ログストアの名前。 const logstoreName = "request_log" async function test() { // プロジェクトを作成します。 await sls.createProject(projectName, { description: 'test' }) // ログストアを作成します。 await sls.createLogStore(projectName, logstoreName, { // 必須。データの保存期間。単位:日。ttl パラメーターを 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); // ログをクエリします。 const from = new Date(); from.setDate(from.getDate() - 1); const to = new Date(); const res = await sls.getLogs(projectName, logstoreName, from, to); 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 ソースコード
説明
プロジェクト、ログストア、およびインデックスの作成、ログの書き込み、ログとログストアのクエリ、ログの分布の取得方法の例。
Logtail を使用して、Node.js の log4js モジュールから Node.js ログを収集する。