Use the Simple Log Service Node.js SDK to create a project and a Logstore, write logs, and query logs.
Prerequisites
-
Simple Log Service is activated. Activate Simple Log Service.
Simple Log Service SDK for Node.js is installed. For more information, see Install the Node.js SDK.
Usage notes
This example uses the China (Hangzhou) public endpoint: https://cn-hangzhou.log.aliyuncs.com. To access Simple Log Service from another Alibaba Cloud service in the same region, use the internal endpoint: https://cn-hangzhou-intranet.log.aliyuncs.com. For more information about supported regions and endpoints, see Endpoints.
Parameters
createProject
createLogStore
createIndex
getLogs
Examples
Collect logs with Node.js code
The following example uses a Node.js script named SLSQuickStart.js to create a project, a Logstore, and an index, and then write and query log data.
const Client = require('@alicloud/log')
const sls = new Client({
// In this example, the AccessKey ID and AccessKey secret are read from environment variables.
accessKeyId: process.env.ALIBABA_CLOUD_ACCESS_KEY_ID,
accessKeySecret: process.env.ALIBABA_CLOUD_ACCESS_KEY_SECRET,
// The Simple Log Service endpoint. This example uses the endpoint for the China (Hangzhou) region. Replace this with the endpoint for your region.
endpoint: 'cn-hangzhou.log.aliyuncs.com'
})
// Required. The project name.
const projectName = "aliyun-test-node-project"
// Required. The logstore name.
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 means the data is stored permanently.
ttl: 3600,
// Required. The number of shards.
shardCount: 2
})
// Create an index.
const index = {
"keys": {
"request_method": {
// Specifies whether queries are case-sensitive. `false` makes queries case-insensitive.
"caseSensitive": false,
// Specifies whether to enable statistical analysis for this field.
"doc_value": true,
"token": ["\n", "\t", ";", ",", "=", ":"],
"type": "text"
}, "status": {
// Specifies whether queries are case-sensitive. `false` makes queries case-insensitive.
"caseSensitive": false,
// Specifies whether to enable statistical analysis for this 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 logs from the past 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 an analytic 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()
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 lists more code examples.
|
GitHub source code |
Description |
|
Create projects, Logstores, and indexes. Write and query logs and Logstores. Get log distributions. |
Collect Node.js logs with Logtail
For information about how to use Logtail to collect log4js logs from a Node.js application, see Collect Node.js logs.