This topic describes how to use the Simple Log Service (SLS) SDK for Node.js to perform common operations, such as creating a project, creating a logstore, writing logs, and querying logs.
Prerequisites
Simple Log Service is activated.
Simple Log Service SDK for Node.js is installed. For more information, see Install Simple Log Service SDK for Node.js.
Important
This example uses the public endpoint of the China (Hangzhou) region: https://cn-hangzhou.log.aliyuncs.com. If you are accessing SLS from another Alibaba Cloud service in the same region as your project, use the internal endpoint: https://cn-hangzhou-intranet.log.aliyuncs.com. For more information about the regions and endpoints that SLS supports, see Endpoints.
Parameters
createProject
createLogStore
createIndex
getLogs
Examples
Write Node.js code to collect logs
In this example, a file named SLSQuickStart.js is created. The file calls API operations to create a project, create a logstore, create an index, write log data, and query log data. The following code provides an example:
const Client = require('@alicloud/log')
const sls = new Client({
// In this example, the AccessKey ID and AccessKey secret are obtained from environment variables.
accessKeyId: process.env.ALIBABA_CLOUD_ACCESS_KEY_ID,
accessKeySecret: process.env.ALIBABA_CLOUD_ACCESS_KEY_SECRET,
// The endpoint of SLS. This example uses the endpoint of the China (Hangzhou) region. Replace it with the actual endpoint.
endpoint: 'cn-hangzhou.log.aliyuncs.com'
})
// Required. The name of the project.
const projectName = "aliyun-test-node-project"
// Required. The name of the logstore.
const logstoreName = "request_log"
async function test() {
// Create a project.
await sls.createProject(projectName, {
description: 'test'
})
// Create a logstore.
await sls.createLogStore(projectName, logstoreName, {
// Required. The data retention period in days. A value of 3650 indicates that the data is permanently stored.
ttl: 3600,
// Required. The number of shards.
shardCount: 2
})
// Create an index.
const index = {
"keys": {
"request_method": {
// Specifies whether the query is case-sensitive. false indicates that the query is case-insensitive.
"caseSensitive": false,
// Specifies whether to enable statistical analysis for the field.
"doc_value": true,
"token": ["\n", "\t", ";", ",", "=", ":"],
"type": "text"
}, "status": {
// Specifies whether the query is case-sensitive. false indicates that the query is case-insensitive.
"caseSensitive": false,
// Specifies whether to enable statistical analysis for the field.
"doc_value": true,
"token": ["\n", "\t", ";", ",", "=", ":"],
"type": "long"
}
},
}
await sls.createIndex(projectName, logstoreName, index)
// Write logs.
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);
// Query example 1: Query the log data of the last day.
const from = new Date();
from.setDate(from.getDate() - 1);
const to = new Date();
const res = await sls.getLogs(projectName, logstoreName, from, to);
// Query example 2: Use a query statement to count the number of logs in the last 10 minutes.
// 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)
}
// Run the function.
test()
The following code shows a sample response:
[
{
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'
}
]The following table provides more code examples for your reference.
GitHub source code | Description |
Examples on how to create projects, logstores, and indexes, write logs, query logs and logstores, and obtain the distribution of logs. |
Collect Node.js logs using Logtail
For an example of how to use Logtail to collect log4js logs from a Node.js application, see Collect Node.js logs.