Query logs (GetLogsV2)
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.
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.
| Parameter | Type | Required | Example | Description |
|---|---|---|---|---|
| --project | String | Yes | aliyun-test-project | The name of the project. |
| --logstore | String | Yes | aliyun-test-logstore | The name of the Logstore. |
| --from | Integer | Yes | 1740000000 | The start time of the query. Specify a Unix timestamp in seconds. |
| --to | Integer | Yes | 1740003600 | The end time of the query. Specify a Unix timestamp in seconds. |
| --accept-encoding | String | Yes | lz4 | The encoding format. Set this parameter to lz4. |
| --query | String | No | * | 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. |
| --line | Integer | No | 100 | The maximum number of log entries to return per request. |
| --offset | Integer | No | 0 | The pagination offset. This parameter applies only to the keyword search mode. |
| --reverse | Boolean | No | true | The 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 100Sample 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"
}
}