All Products
Search
Document Center

Simple Log Service:Getting started with Simple Log Service SDK for Node.js

Last Updated:May 12, 2025

This topic describes how to get started with Simple Log Service SDK for Node.js, and perform common operations such as create a project and logstore, and write and query logs.

Prerequisites

Considerations

This example uses the public endpoint for the Singapore region ap-southeast-1.log.aliyuncs.com. If you access Simple Log Service from another Alibaba Cloud service in the same region as your project, use the internal endpoint ap-southeast-1-intranet.log.aliyuncs.com. For information about the correspondence between regions and endpoints, see Endpoints.

Examples

  • Write Node.js code to collect logs.

    In this example, create a SLSQuickStart.js file and call interfaces to create a project, logstore, and index, and write and query log data. Sample code:

    
    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,
        // Specify a Simple Log Service endpoint. 
        endpoint: 'ap-southeast-1.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 retention period of data. Unit: days. If you set the ttl parameter to 3650, data is permanently stored.
            ttl: 3600,
            // Required. The number of shards.
            shardCount: 2
        })
        // Create an index.
        const index = {
            "keys": {
                "request_method": {
                    // Specifies whether the field is case-sensitive. The value false indicates that the field is not case-sensitive.
                    "caseSensitive": false,
                    // Specifies whether to enable statistical analysis for the field
                    "doc_value": true,
                    "token": ["\n", "\t", ";", ",", "=", ":"],
                    "type": "text"
                }, "status": {
                    // Specifies whether the field is case-sensitive. The value false indicates that the field is not case-sensitive.
                    "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 logs.
          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)
    }
    // Call the following functions:
    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 provides additional sample code for your reference or direct use.

    GitHub source code

    Description

    integration.test.js

    Examples on how to create projects, logstores, and indexes, write logs, query logs and logstores, and obtain the distribution of logs.

  • Use Logtail to collect Node.js logs from the log4js module in Node.js.