Updated on 2024-04-12 GMT+08:00

Querying Logs

Function

This API is used to query logs in a specified log stream.

URI

POST /v2/{project_id}/groups/{log_group_id}/streams/{log_stream_id}/content/query

Table 1 Path Parameters

Parameter

Mandatory

Type

Description

project_id

Yes

String

[Project ]ID. For details about how to obtain a project ID, see Obtaining the Account Tenant ID, Log Group ID, and Log Stream ID.

Default: None

Minimum: 32

Maximum: 32

log_group_id

Yes

String

Log group ID. For details about how to obtain a log group ID, see Obtaining the Account Tenant ID, Log Group ID, and Log Stream ID.

Default: None

Minimum: 36

Maximum: 36

log_stream_id

Yes

String

Log stream ID. For details about how to obtain a log stream ID, see Obtaining the Account Tenant ID, Log Group ID, and Log Stream ID.

Default: None

Minimum: 36

Maximum: 36

Request Parameters

Table 2 Request header parameters

Parameter

Mandatory

Type

Description

X-Auth-Token

Yes

String

User token obtained from IAM.

Default: None

Minimum: 1000

Maximum: 2000

Content-Type

Yes

String

Set this parameter to application/json;charset=UTF-8.

Default: None

Minimum: 30

Maximum: 30

Table 3 Request body parameters

Parameter

Mandatory

Type

Description

start_time

Yes

String

Start time of the query (UTC, in milliseconds). Note: Maximum query time range: 30 days

end_time

Yes

String

End time of the query (UTC, in milliseconds). Note: Maximum query time range: 30 days

labels

No

Map<String,String>

Filter criteria, which vary between log sources.

keywords

No

String

Keyword used for log search. A keyword is a word between two adjacent delimiters, for example, error.

Enumeration values:

  • error

line_num

No

String

Sequence number of a log event. This parameter is not required for the first query, but is required for subsequent pagination queries. The value can be obtained from the response of the last query. The value of line_num should be between the values of start_time and end_time.

Minimum: 19

Maximum: 19

is_desc

No

Boolean

Whether the search order is descending or ascending. The default value is false, indicating that search results are displayed in ascending order.

Default: false

search_type

No

String

The value is init (default value) for the first query, or forwards or backwards for a pagination query. This parameter is used together with is_desc for pagination queries.

Enumeration values:

  • forwards

  • backwards

limit

No

Integer

Number of logs to be queried each time. The value is 50 when this parameter is not set. You are advised to set this parameter to 100.

Minimum: 1

Maximum: 5000

Default: 50

highlight

No

Boolean

Whether the keyword is highlighted. The default value is true, indicating that the keyword is highlighted.

Default: true

is_count

No

Boolean

Whether the number of log events is counted. The default value is false, indicating that the number of log events is not counted.

Enumeration values:

  • true

Response Parameters

Status code: 200

Table 4 Response body parameters

Parameter

Type

Description

logs

LogContents object

Log information.

count

Integer

Number of logs.

isQueryComplete

Boolean

Indicates whether the query is complete.

Table 5 LogContents

Parameter

Type

Description

content

String

Raw log data.

Minimum: 1

Maximum: 10000

line_num

String

Sequence number of a log line.

Minimum: 19

Maximum: 19

labels

Map<String,String>

Indicates the log labels.

Status code: 400

Table 6 Response body parameters

Parameter

Type

Description

error_code

String

Error code.

error_msg

String

Error message.

Status code: 401

Table 7 Response body parameters

Parameter

Type

Description

error_code

String

Error code.

error_msg

String

Error message.

Status code: 403

Table 8 Response body parameters

Parameter

Type

Description

error_code

String

Error code.

error_msg

String

Error message.

Status code: 500

Table 9 Response body parameters

Parameter

Type

Description

error_code

String

Error code.

error_msg

String

Error message.

Status code: 503

Table 10 Response body parameters

Parameter

Type

Description

error_code

String

Error code.

error_msg

String

Error message.

Example Requests

  • Log content.

    POST https://{endpoint}/v2/{project_id}/groups/{log_group_id}/streams/{log_stream_id}/content/query
    
    2020-07-25/14:44:42 this log is Error NO 1
    2020-07-25/14:44:43 this log is Error NO 2
    2020-07-25/14:44:44 this log is Error NO 3
    2020-07-25/14:44:45 this log is Error NO 4
    2020-07-25/14:44:46 this log is Error NO 5
    2020-07-25/14:44:47 this log is Error NO 6
    2020-07-25/14:44:48 this log is Error NO 7
    2020-07-25/14:44:49 this log is Error NO 8
    2020-07-25/14:44:50 this log is Error NO 9
    2020-07-25/14:44:51 this log is Error NO 10
  • For a pagination query (Assume that the search starts from the log event containing NO 5. Log events containing NO 2, NO 3, and NO 4 are the target log events):

    v2/{project_id}/groups/{log_group_id}/streams/{log_stream_id}/content/query
    
    {
      "start_time" : 1595659200000,
      "end_time" : 1595659500000,
      "labels" : {
        "hostName" : "ecs-kwxtest"
      },
      "keywords" : "log",
      "line_num" : "1595659490239433658",
      "is_desc" : "true",
      "search_type" : "forwards",
      "limit" : "3",
      "is_count" : true
    }
  • For a pagination query (Assume that the search starts from the log event containing NO 5. Log events containing NO 4, NO 3, and NO 2 are the target log events):

    v2/{project_id}/groups/{log_group_id}/streams/{log_stream_id}/content/query
    
    {
      "start_time" : 1595659200000,
      "end_time" : 1595659500000,
      "labels" : {
        "hostName" : "ecs-kwxtest"
      },
      "keywords" : "log",
      "limit" : 10,
      "is_count" : true
    }
  • Querying logs for the first time.

    v2/{project_id}/groups/{log_group_id}/streams/{log_stream_id}/content/query
    
    {
      "start_time" : 1595659200000,
      "end_time" : 1595659500000,
      "labels" : {
        "hostName" : "ecs-kwxtest"
      },
      "keywords" : "log",
      "line_num" : "1595659490239433658",
      "is_desc" : "false",
      "search_type" : "forwards",
      "limit" : "3",
      "is_count" : true
    }
  • For a pagination query (Assume that the search starts from the log event containing NO 5. Log events containing NO 6, NO 7, and NO 8 are the target log events):

    v2/{project_id}/groups/{log_group_id}/streams/{log_stream_id}/content/query
    
    {
      "start_time" : 1595659200000,
      "end_time" : 1595659500000,
      "labels" : {
        "hostName" : "ecs-kwxtest"
      },
      "keywords" : "log",
      "line_num" : "1595659490239433658",
      "is_desc" : "true",
      "search_type" : "forwards",
      "limit" : "3",
      "is_count" : true
    }
  • For a pagination query (Assume that the search starts from the log event containing NO 5. Log events containing NO 8, NO 7, and NO 6 are the target log events):

    v2/{project_id}/groups/{log_group_id}/streams/{log_stream_id}/content/query
    
    {
      "start_time" : 1595659200000,
      "end_time" : 1595659500000,
      "labels" : {
        "hostName" : "ecs-kwxtest"
      },
      "keywords" : "log",
      "line_num" : "1595659490239433658",
      "is_desc" : "false",
      "search_type" : "backwards",
      "limit" : "3",
      "is_count" : true
    }

Example Responses

Status code: 200

The request is successful.

Querying logs for the first time.
{ 
  "count": 32,
  "logs": [
        {
            "content": "2020-07-25/14:44:42 this <HighLightTag>log</HighLightTag> is Error NO 1\n",
            "labels": {
                "hostName": "ecs-kwxtest",
                "hostIP": "192.168.0.156",
                "appName": "default_appname",
                "containerName": "CONFIG_FILE",
                "clusterName": "CONFIG_FILE",
                "hostId": "9787ef31-fd7b-4eff-ba71-72d580f11f55",
                "podName": "default_procname",
                "clusterId": "CONFIG_FILE",
                "nameSpace": "CONFIG_FILE",
                "category": "LTS"
            },
            "line_num": "1595659490239433654"
        },
        {
            "content": "2020-07-25/14:44:43 this <HighLightTag>log</HighLightTag> is Error NO 2\n",
            "labels": {
                "hostName": "ecs-kwxtest",
                "hostIP": "192.168.0.156",
                "appName": "default_appname",
                "containerName": "CONFIG_FILE",
                "clusterName": "CONFIG_FILE",
                "hostId": "9787ef31-fd7b-4eff-ba71-72d580f11f55",
                "podName": "default_procname",
                "clusterId": "CONFIG_FILE",
                "nameSpace": "CONFIG_FILE",
                "category": "LTS"
            },
            "line_num": "1595659490239433655"
        },
        {
            "content": "2020-07-25/14:44:44 this <HighLightTag>log</HighLightTag> is Error NO 3\n",
            "labels": {
                "hostName": "ecs-kwxtest",
                "hostIP": "192.168.0.156",
                "appName": "default_appname",
                "containerName": "CONFIG_FILE",
                "clusterName": "CONFIG_FILE",
                "hostId": "9787ef31-fd7b-4eff-ba71-72d580f11f55",
                "podName": "default_procname",
                "clusterId": "CONFIG_FILE",
                "nameSpace": "CONFIG_FILE",
                "category": "LTS"
            },
            "line_num": "1595659490239433656"
        },
        {
            "content": "2020-07-25/14:44:45 this <HighLightTag>log</HighLightTag> is Error NO 4\n",
            "labels": {
                "hostName": "ecs-kwxtest",
                "hostIP": "192.168.0.156",
                "appName": "default_appname",
                "containerName": "CONFIG_FILE",
                "clusterName": "CONFIG_FILE",
                "hostId": "9787ef31-fd7b-4eff-ba71-72d580f11f55",
                "podName": "default_procname",
                "clusterId": "CONFIG_FILE",
                "nameSpace": "CONFIG_FILE",
                "category": "LTS"
            },
            "line_num": "1595659490239433657"
        },
        {
            "content": "2020-07-25/14:44:46 this <HighLightTag>log</HighLightTag> is Error NO 5\n",
            "labels": {
                "hostName": "ecs-kwxtest",
                "hostIP": "192.168.0.156",
                "appName": "default_appname",
                "containerName": "CONFIG_FILE",
                "clusterName": "CONFIG_FILE",
                "hostId": "9787ef31-fd7b-4eff-ba71-72d580f11f55",
                "podName": "default_procname",
                "clusterId": "CONFIG_FILE",
                "nameSpace": "CONFIG_FILE",
                "category": "LTS"
            },
            "line_num": "1595659490239433658"
        },
        {
            "content": "2020-07-25/14:44:47 this <HighLightTag>log</HighLightTag> is Error NO 6\n",
            "labels": {
                "hostName": "ecs-kwxtest",
                "hostIP": "192.168.0.156",
                "appName": "default_appname",
                "containerName": "CONFIG_FILE",
                "clusterName": "CONFIG_FILE",
                "hostId": "9787ef31-fd7b-4eff-ba71-72d580f11f55",
                "podName": "default_procname",
                "clusterId": "CONFIG_FILE",
                "nameSpace": "CONFIG_FILE",
                "category": "LTS"
            },
            "line_num": "1595659490239433659"
        },
        {
            "content": "2020-07-25/14:44:48 this <HighLightTag>log</HighLightTag> is Error NO 7\n",
            "labels": {
                "hostName": "ecs-kwxtest",
                "hostIP": "192.168.0.156",
                "appName": "default_appname",
                "containerName": "CONFIG_FILE",
                "clusterName": "CONFIG_FILE",
                "hostId": "9787ef31-fd7b-4eff-ba71-72d580f11f55",
                "podName": "default_procname",
                "clusterId": "CONFIG_FILE",
                "nameSpace": "CONFIG_FILE",
                "category": "LTS"
            },
            "line_num": "1595659490239433660"
        },
        {
            "content": "2020-07-25/14:44:49 this <HighLightTag>log</HighLightTag> is Error NO 8\n",
            "labels": {
                "hostName": "ecs-kwxtest",
                "hostIP": "192.168.0.156",
                "appName": "default_appname",
                "containerName": "CONFIG_FILE",
                "clusterName": "CONFIG_FILE",
                "hostId": "9787ef31-fd7b-4eff-ba71-72d580f11f55",
                "podName": "default_procname",
                "clusterId": "CONFIG_FILE",
                "nameSpace": "CONFIG_FILE",
                "category": "LTS"
            },
            "line_num": "1595659490239433661"
        },
        {
            "content": "2020-07-25/14:44:50 this <HighLightTag>log</HighLightTag> is Error NO 9\n",
            "labels": {
                "hostName": "ecs-kwxtest",
                "hostIP": "192.168.0.156",
                "appName": "default_appname",
                "containerName": "CONFIG_FILE",
                "clusterName": "CONFIG_FILE",
                "hostId": "9787ef31-fd7b-4eff-ba71-72d580f11f55",
                "podName": "default_procname",
                "clusterId": "CONFIG_FILE",
                "nameSpace": "CONFIG_FILE",
                "category": "LTS"
            },
            "line_num": "1595659490839420574"
        },
        {
            "content": "2020-07-25/14:44:51 this <HighLightTag>log</HighLightTag> is Error NO 10\n",
            "labels": {
                "hostName": "ecs-kwxtest",
                "hostIP": "192.168.0.156",
                "appName": "default_appname",
                "containerName": "CONFIG_FILE",
                "clusterName": "CONFIG_FILE",
                "hostId": "9787ef31-fd7b-4eff-ba71-72d580f11f55",
                "podName": "default_procname",
                "clusterId": "CONFIG_FILE",
                "nameSpace": "CONFIG_FILE",
                "category": "LTS"
            },
            "line_num": "1595659491839412667"
        }
]
}

For a pagination query (Assume that the search starts from the log event containing **NO 5**. Log events containing **NO 6**, **NO 7**, and **NO 8** are the target log events):
{
    "count": 32,
    "logs": [
        {
            "content": "2020-07-25/14:44:47 this <HighLightTag>log</HighLightTag> is Error NO 6\n",
            "labels": {
                "hostName": "ecs-kwxtest",
                "hostIP": "192.168.0.156",
                "appName": "default_appname",
                "containerName": "CONFIG_FILE",
                "clusterName": "CONFIG_FILE",
                "hostId": "9787ef31-fd7b-4eff-ba71-72d580f11f55",
                "podName": "default_procname",
                "clusterId": "CONFIG_FILE",
                "nameSpace": "CONFIG_FILE",
                "category": "LTS"
            },
            "line_num": "1595659490239433659"
        },
        {
            "content": "2020-07-25/14:44:48 this <HighLightTag>log</HighLightTag> is Error NO 7\n",
            "labels": {
                "hostName": "ecs-kwxtest",
                "hostIP": "192.168.0.156",
                "appName": "default_appname",
                "containerName": "CONFIG_FILE",
                "clusterName": "CONFIG_FILE",
                "hostId": "9787ef31-fd7b-4eff-ba71-72d580f11f55",
                "podName": "default_procname",
                "clusterId": "CONFIG_FILE",
                "nameSpace": "CONFIG_FILE",
                "category": "LTS"
            },
            "line_num": "1595659490239433660"
        },
        {
            "content": "2020-07-25/14:44:49 this <HighLightTag>log</HighLightTag> is Error NO 8\n",
            "labels": {
                "hostName": "ecs-kwxtest",
                "hostIP": "192.168.0.156",
                "appName": "default_appname",
                "containerName": "CONFIG_FILE",
                "clusterName": "CONFIG_FILE",
                "hostId": "9787ef31-fd7b-4eff-ba71-72d580f11f55",
                "podName": "default_procname",
                "clusterId": "CONFIG_FILE",
                "nameSpace": "CONFIG_FILE",
                "category": "LTS"
            },
            "line_num": "1595659490239433661"
        }
    ]
}     

For a pagination query (Assume that the search starts from the log event containing **NO 5**. Log events containing **NO 8**, **NO 7**, and **NO 6** are the target log events):
{
    "count": 32,
    "logs": [
        {
            "content": "2020-07-25/14:44:49 this <HighLightTag>log</HighLightTag> is Error NO 8\n",
            "labels": {
                "hostName": "ecs-kwxtest",
                "hostIP": "192.168.0.156",
                "appName": "default_appname",
                "containerName": "CONFIG_FILE",
                "clusterName": "CONFIG_FILE",
                "hostId": "9787ef31-fd7b-4eff-ba71-72d580f11f55",
                "podName": "default_procname",
                "clusterId": "CONFIG_FILE",
                "nameSpace": "CONFIG_FILE",
                "category": "LTS"
            },
            "line_num": "1595659490239433661"
        },
        {
            "content": "2020-07-25/14:44:48 this <HighLightTag>log</HighLightTag> is Error NO 7\n",
            "labels": {
                "hostName": "ecs-kwxtest",
                "hostIP": "192.168.0.156",
                "appName": "default_appname",
                "containerName": "CONFIG_FILE",
                "clusterName": "CONFIG_FILE",
                "hostId": "9787ef31-fd7b-4eff-ba71-72d580f11f55",
                "podName": "default_procname",
                "clusterId": "CONFIG_FILE",
                "nameSpace": "CONFIG_FILE",
                "category": "LTS"
            },
            "line_num": "1595659490239433660"
        },
        {
            "content": "2020-07-25/14:44:47 this <HighLightTag>log</HighLightTag> is Error NO 6\n",
            "labels": {
                "hostName": "ecs-kwxtest",
                "hostIP": "192.168.0.156",
                "appName": "default_appname",
                "containerName": "CONFIG_FILE",
                "clusterName": "CONFIG_FILE",
                "hostId": "9787ef31-fd7b-4eff-ba71-72d580f11f55",
                "podName": "default_procname",
                "clusterId": "CONFIG_FILE",
                "nameSpace": "CONFIG_FILE",
                "category": "LTS"
            },
            "line_num": "1595659490239433659"
        }
    ]
}

For a pagination query (Assume that the search starts from the log event containing **NO 5**. Log events containing **NO 2**, **NO 3**, and **NO 4** are the target log events):
{
    "count": 32,
    "logs": [
        {
            "content": "2020-07-25/14:44:43 this <HighLightTag>log</HighLightTag> is Error NO 2\n",
            "labels": {
                "hostName": "ecs-kwxtest",
                "hostIP": "192.168.0.156",
                "appName": "default_appname",
                "containerName": "CONFIG_FILE",
                "clusterName": "CONFIG_FILE",
                "hostId": "9787ef31-fd7b-4eff-ba71-72d580f11f55",
                "podName": "default_procname",
                "clusterId": "CONFIG_FILE",
                "nameSpace": "CONFIG_FILE",
                "category": "LTS"
            },
            "line_num": "1595659490239433655"
        },
        {
            "content": "2020-07-25/14:44:44 this <HighLightTag>log</HighLightTag> is Error NO 3\n",
            "labels": {
                "hostName": "ecs-kwxtest",
                "hostIP": "192.168.0.156",
                "appName": "default_appname",
                "containerName": "CONFIG_FILE",
                "clusterName": "CONFIG_FILE",
                "hostId": "9787ef31-fd7b-4eff-ba71-72d580f11f55",
                "podName": "default_procname",
                "clusterId": "CONFIG_FILE",
                "nameSpace": "CONFIG_FILE",
                "category": "LTS"
            },
            "line_num": "1595659490239433656"
        },
        {
            "content": "2020-07-25/14:44:45 this <HighLightTag>log</HighLightTag> is Error NO 4\n",
            "labels": {
                "hostName": "ecs-kwxtest",
                "hostIP": "192.168.0.156",
                "appName": "default_appname",
                "containerName": "CONFIG_FILE",
                "clusterName": "CONFIG_FILE",
                "hostId": "9787ef31-fd7b-4eff-ba71-72d580f11f55",
                "podName": "default_procname",
                "clusterId": "CONFIG_FILE",
                "nameSpace": "CONFIG_FILE",
                "category": "LTS"
            },
            "line_num": "1595659490239433657"
        }
    ]
}

For a pagination query (Assume that the search starts from the log event containing **NO 5**. Log events containing **NO 4**, **NO 3**, and **NO 2** are the target log events):
{
    "count": 32,
    "logs": [
        {
            "content": "2020-07-25/14:44:45 this <HighLightTag>log</HighLightTag> is Error NO 4\n",
            "labels": {
                "hostName": "ecs-kwxtest",
                "hostIP": "192.168.0.156",
                "appName": "default_appname",
                "containerName": "CONFIG_FILE",
                "clusterName": "CONFIG_FILE",
                "hostId": "9787ef31-fd7b-4eff-ba71-72d580f11f55",
                "podName": "default_procname",
                "clusterId": "CONFIG_FILE",
                "nameSpace": "CONFIG_FILE",
                "category": "LTS"
            },
            "line_num": "1595659490239433657"
        },
        {
            "content": "2020-07-25/14:44:44 this <HighLightTag>log</HighLightTag> is Error NO 3\n",
            "labels": {
                "hostName": "ecs-kwxtest",
                "hostIP": "192.168.0.156",
                "appName": "default_appname",
                "containerName": "CONFIG_FILE",
                "clusterName": "CONFIG_FILE",
                "hostId": "9787ef31-fd7b-4eff-ba71-72d580f11f55",
                "podName": "default_procname",
                "clusterId": "CONFIG_FILE",
                "nameSpace": "CONFIG_FILE",
                "category": "LTS"
            },
            "line_num": "1595659490239433656"
        },
        {
            "content": "2020-07-25/14:44:43 this <HighLightTag>log</HighLightTag> is Error NO 2\n",
            "labels": {
                "hostName": "ecs-kwxtest",
                "hostIP": "192.168.0.156",
                "appName": "default_appname",
                "containerName": "CONFIG_FILE",
                "clusterName": "CONFIG_FILE",
                "hostId": "9787ef31-fd7b-4eff-ba71-72d580f11f55",
                "podName": "default_procname",
                "clusterId": "CONFIG_FILE",
                "nameSpace": "CONFIG_FILE",
                "category": "LTS"
            },
            "line_num": "1595659490239433655"
        }
    ]
}

Status code: 400

BadRequest. Invalid request. Modify the request based on the description in error_msg before a retry.

{
  "error_code" : "LTS.0009",
  "error_msg" : "Failed to validate the request body"
}

Status code: 401

AuthFailed. Authentication failed. Check the token and try again.

{
  "error_code" : "LTS.0003",
  "error_msg" : "Invalid token"
}

Status code: 403

Forbidden.The request is rejected.The server has received the request and understood it, but refuses to respond to it. The client should not repeat the request without modifications.

{
  "error_code" : "LTS.0001",
  "error_msg" : "Invalid projectId"
}

Status code: 500

InternalServerError. The server has received the request but encountered an internal error.

{
  "error_code" : "LTS.0202",
  "error_msg" : "Failed to query lts log"
}

Status Codes

Status Code

Description

200

The request is successful.

400

BadRequest. Invalid request. Modify the request based on the description in error_msg before a retry.

401

AuthFailed. Authentication failed. Check the token and try again.

403

Forbidden.The request is rejected.The server has received the request and understood it, but refuses to respond to it. The client should not repeat the request without modifications.

500

InternalServerError. The server has received the request but encountered an internal error.

503

ServiceUnavailable. The requested service is unavailable.

Error Codes

See Error Codes.