Query logs (GetLogsV2)

Updated at:

Run CLI commands to query logs in a specified Logstore. Two query modes are supported: keyword search and SQL analysis.

Prerequisites

Before you begin, make sure that:

  • Simple Log Service (SLS) is activated. For more information, see Get started with SLS.

  • The SLS CLI is installed and configured. For more information, see Install and configure the new SLS CLI.

  • An SLS project and Logstore are created, and indexes are configured. For more information, see Create a project, Create a Logstore, and Create an index.

Note

The following requirements apply:

  • The Logstore must have indexes configured before you can query logs.

  • The time range is a left-closed, right-open interval: [from, to).

Command syntax

aliyun sls get-logs-v2 --project=<value> --logstore=<value> --from=<value> --to=<value> --accept-encoding=lz4 [--query=<value>] [--line=<value>] [--offset=<value>] [--reverse=<value>]

Parameters

The following table describes the required and command-specific parameters.

ParameterTypeRequiredExampleDescription
--projectStringYesaliyun-test-projectThe name of the project.
--logstoreStringYesaliyun-test-logstoreThe name of the Logstore.
--fromIntegerYes1740000000The start time of the query. Specify a Unix timestamp in seconds.
--toIntegerYes1740003600The end time of the query. Specify a Unix timestamp in seconds.
--accept-encodingStringYeslz4The encoding format. Set this parameter to lz4.
--queryStringNo*The query expression. For keyword search, use the query syntax. For SQL analysis, use the <query syntax> | <SQL statement> format. For more information, see Query and analysis syntax.
--lineIntegerNo100The maximum number of log entries to return per request.
--offsetIntegerNo0The pagination offset. This parameter applies only to the keyword search mode.
--reverseBooleanNotrueThe sort order of the results. This parameter applies only to the keyword search mode. Set this parameter to true to sort results from newest to oldest, or false to sort from oldest to newest.

For information about global parameters, see Global parameters.

Command examples

Query all logs

  • Sample command

aliyun sls get-logs-v2 --project="aliyun-test-project" --logstore="aliyun-test-logstore" \
  --accept-encoding lz4 --from 1740000000 --to 1740003600 --line 100
  • Sample response

{
  "logs": [
    {
      "__time__": "1740000001",
      "__source__": "192.168.1.10",
      "level": "info",
      "message": "request completed"
    }
  ],
  "meta": {
    "progress": "Complete"
  }
}

Filter by field

  • Sample command

aliyun sls get-logs-v2 --project="aliyun-test-project" --logstore="aliyun-test-logstore" \
  --accept-encoding lz4 --from 1740000000 --to 1740003600 --line 100 \
  --query '* and "timeout" and status: "500"'
  • Sample response

{
  "logs": [
    {
      "__time__": "1740000200",
      "__source__": "192.168.1.12",
      "status": "500",
      "message": "timeout error"
    }
  ],
  "meta": {
    "progress": "Complete"
  }
}

SQL mode: Count all logs

  • Sample command

aliyun sls get-logs-v2 --project="aliyun-test-project" --logstore="aliyun-test-logstore" \
  --accept-encoding lz4 --from 1740000000 --to 1740003600 \
  --query '* | select count(*) as total from log'
  • Sample response

{
  "logs": [
    {
      "total": "1250"
    }
  ],
  "meta": {
    "progress": "Complete"
  }
}

SQL mode: Group and count logs

  • Sample command

aliyun sls get-logs-v2 --project="aliyun-test-project" --logstore="aliyun-test-logstore" \
  --accept-encoding lz4 --from 1740000000 --to 1740003600 \
  --query '* | select request_uri, count(*) as cnt from log group by request_uri order by cnt desc limit 20'
  • Sample response

{
  "logs": [
    {
      "request_uri": "/api/user",
      "cnt": "520"
    },
    {
      "request_uri": "/api/order",
      "cnt": "380"
    }
  ],
  "meta": {
    "progress": "Complete"
  }
}