Simple Log Service Node.js SDK quick start

更新时间:
复制 MD 格式

Use the Simple Log Service Node.js SDK to create a project and a Logstore, write logs, and query logs.

Prerequisites

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

Request parameters

Parameter

Type

Required

Description

projectName

String

Yes

The name of the project. The name must be globally unique. You cannot change the name after you create the project.

The name must meet the following requirements:

  • The name can contain lowercase letters, digits, and hyphens (-).

  • The name must start with a lowercase letter and end with a lowercase letter or a digit.

  • The name must be 3 to 63 characters in length.

description

String

Yes

The description of the project. The description can be up to 64 characters long and cannot contain angle brackets (<>), apostrophes ('), backslashes (\), double quotation marks ("), or two consecutive backslashes (\\).

resourceGroupId

String

No

The ID of the resource group. If you omit this parameter, the default resource group is used. For more information, see Create a resource group.

dataRedundancyType

String

No

The data redundancy type. Default: locally redundant storage. Some regions support both locally redundant storage and zone-redundant storage. The data redundancy type cannot be changed after the project is created. For more information, see Storage redundancy.

  • LRS: locally redundant storage

  • ZRS: zone-redundant storage

Response parameters

For information about the response parameters, see CreateProject.

createLogStore

Request parameters

Parameter

Type

Required

Description

projectName

String

Yes

The name of the project. The project in Simple Log Service is used to isolate the resources of different users and control access to specific resources. See Manage projects.

logstoreName

String

Yes

The name of the logstore. The name must be globally unique. You cannot change the name after you create the project.

The name must meet the following requirements:

  • Can contain lowercase letters, digits, hyphens (-), and underscores (_).

  • Must start with a lowercase letter and end with a lowercase letter or a digit.

  • Must be 3 to 63 characters in length.

ttl

int

No

The Data Retention Period in days. The value must be an integer from 1 to 3,650. A value of 3,650 indicates permanent storage. When the specified retention period elapses, the logs are deleted.

The data retention period (ttl) is the sum of the following periods:

  • hot storage period (hotTtl)

  • infrequent access storage period (infrequentAccessTtl)

  • archive storage period

shardCount

int

No

The number of shards. Valid values: 1 to 10. For more information, see Shard.

enableTracking

bool

No

Specifies whether to enable WebTracking.

  • True: Enables WebTracking. The Logstore accepts anonymous write requests from the internet. Because these requests are not authenticated, dirty data may be written to the Logstore.

  • False (default): Disables WebTracking.

Note

WebTracking allows you to collect access information from browsers, iOS apps, and Android apps. For more information, see Use Web Tracking to collect front-end logs.

appendMeta

bool

No

Specifies whether to enable the feature to Log Public IP.

  • True: Enables the feature. Simple Log Service adds the following information to the tag field of logs:

    • __client_ip__: The public IP address of the client.

    • __receive_time__: The time when the log arrives at the server, in UNIX timestamp format.

  • False (default): Disables the feature.

autoSplit

bool

No

Specifies whether to enable Automatic Sharding.

maxSplitShard

int

No

The Maximum Shards for a shard. If you enable Automatic Sharding, a shard can be automatically split into a maximum of 256 shards.

Important

This parameter is required if autoSplit is set to True.

encryptConf

dict

No

The data structure for encryption configurations. It includes the enable, encrypt_type, and user_cmk_info parameters. For more information, see EncryptConf and Data encryption.

telemetryType

String

No

The observable data type. Valid values:

  • None (default): Log data.

  • metric: Metric data. If you set the value to metric, only the following parameters take effect:

    • logstoreName

    • ttl

    • shardCount

    • autoSplit

    • maxSplitShard

    • appendMeta

Important

You cannot modify this parameter after the Logstore is created.

hotTtl

int

No

The hot storage period of data in the Logstore, in days. The minimum value is 7, and the value cannot exceed the value of ttl. A value of -1 indicates that all data within the retention period (ttl) is stored in the hot storage tier.

After the hot storage period expires, data moves to infrequent access storage. For more information about hot storage, infrequent access storage, archive storage, and the data conversion process, see Manage intelligent tiered storage.

  • Data must remain in hot storage for at least 7 days before it can be moved to infrequent access storage. Data must remain in infrequent access storage for at least 30 days before it can be moved to archive storage.

  • Data must remain in hot storage for at least 30 days before it can be moved to archive storage.

mode

String

No

Simple Log Service provides two types of Logstores: Standard and Query.

  • Standard (default): Supports the full data analytics capabilities of Simple Log Service, including real-time monitoring, interactive analysis, and end-to-end observability.

  • Query: Supports high-performance queries at about half the indexing cost of the Standard type. This type does not support SELECT statements and is suitable for scenarios with large data volumes, long retention periods, or no log analysis requirements.

For more information, see Logstore types.

infrequentAccessTtl

int

No

The infrequent access storage period of data in the Logstore, in days. For more information, see Manage intelligent tiered storage.

Response parameters

For information about the response parameters, see CreateLogStore.

createIndex

Request parameters

Parameter

Type

Required

Description

projectName

String

Yes

The name of the project. The project in Simple Log Service is used to isolate the resources of different users and control access to specific resources. See Manage projects.

logstoreName

String

Yes

The name of the logstore. The logstore in Simple Log Service is used to collect, store, and query logs. See Manage Logstores.

index

index

Yes

The index configuration.

Response parameters

For information about the response parameters, see CreateIndex.

getLogs

Request parameters

Parameter

Type

Required

Description

projectName

String

Yes

The name of the project. The project in Simple Log Service is used to isolate the resources of different users and control access to specific resources. See Manage projects.

logstoreName

String

Yes

The name of the logstore. The logstore in Simple Log Service is used to collect, store, and query logs. See Manage Logstores.

from

int

Yes

The beginning of the time range to query. The value is a UNIX timestamp.

Note
  • The time when the logstore receives logs. The __tag__:__receive_time__ field is a reserved field of Simple Log Service.

  • The time range defined by the start time and the end time. The time range is a left-closed, right-open interval. The interval includes the start time of the time range, but not the end time. If both are the same, the interval is invalid and an error is returned.

  • To ensure that full data can be queried, specify a query time range that is accurate to the minute. If you also specify a time range in an analytic statement, the time range is used for query and analysis.

  • If you want to specify a time range that is accurate to the second, you can use date and time functions to convert the time format. Examples:

    • * | SELECT * FROM log WHERE from_unixtime(__time__) > from_unixtime(1664186624) AND from_unixtime(__time__) < now()

    • * | SELECT * FROM log WHERE __time__ > to_unixtime(date_parse('2022-10-19 15:46:05', '%Y-%m-%d %H:%i:%s')) AND __time__ < to_unixtime(now())

to

int

Yes

The end of the time range to query. The value is a UNIX timestamp.

Note
  • The time when the logstore receives logs. The __tag__:__receive_time__ field is a reserved field of Simple Log Service.

  • The time range defined by the start time and the end time. The time range is a left-closed, right-open interval. The interval includes the start time of the time range, but not the end time. If both are the same, the interval is invalid and an error is returned.

  • To ensure that full data can be queried, specify a query time range that is accurate to the minute. If you also specify a time range in an analytic statement, the time range is used for query and analysis.

  • If you want to specify a time range that is accurate to the second, you can use date and time functions to convert the time format. Examples:

    • * | SELECT * FROM log WHERE from_unixtime(__time__) > from_unixtime(1664186624) AND from_unixtime(__time__) < now()

    • * | SELECT * FROM log WHERE __time__ > to_unixtime(date_parse('2022-10-19 15:46:05', '%Y-%m-%d %H:%i:%s')) AND __time__ < to_unixtime(now())

topic

String

No

The topic of logs. The default value is an empty string. For more information, see Log topics.

query

String

No

The search or analytic statement. For more information, see Query and analyze logs. To use Dedicated SQL, add set session parallel_sql=true; to the analytic statement. Example: * | set session parallel_sql=true; select count(*) as pv. For common query and analysis issues, see Common errors when you query and analyze logs.

Note

When the query parameter contains an analytic statement (SQL statement), the line and offset parameters are ignored. We recommend that you set these parameters to 0 and use the LIMIT clause in the SQL statement for pagination. For more information, see Paginate query and analysis results.

line

int

No

This parameter applies only when the query parameter contains a search statement. It specifies the maximum number of logs to return. Value range: 0–100. Default: 100.

offset

int

No

This parameter applies only when the query parameter contains a search statement. It specifies the number of logs to skip before returning results. Default: 0.

reverse

bool

No

Specifies whether to return logs in descending order of their timestamps. The precision is to the minute.

  • True: Returns logs in descending order of their timestamps.

  • False (default): Returns logs in ascending order of their timestamps.

Important
  • If the query parameter contains a search statement, the reverse parameter is valid and specifies the sorting order for the returned logs.

  • If the query parameter contains an analytic statement, the reverse parameter is ignored. The sort order is specified by the ORDER BY clause in the analytic statement.

powerSql

bool

No

Specifies whether to use Dedicated SQL. For more information, see High-performance and high-precision query and analysis (Dedicated SQL).

  • True: Use Dedicated SQL.

  • False (default): Use standard SQL.

You can also enable Dedicated SQL within the query parameter.

Response parameters

For information about the response parameters, see 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

integration.test.js

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.