更新时间:2022-06-17 GMT+08:00

查询日志

功能介绍

该接口用于查询指定日志流下的日志内容。

URI

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

表1 路径参数

参数

是否必选

参数类型

描述

project_id

String

项目ID,获取方式请参见:获取帐号ID、项目ID、日志组ID、日志流ID

缺省值:None

最小长度:32

最大长度:32

log_group_id

String

日志组ID。

缺省值:None

最小长度:36

最大长度:36

log_stream_id

String

日志流ID。

缺省值:None

最小长度:36

最大长度:36

请求参数

表2 请求Header参数

参数

是否必选

参数类型

描述

X-Auth-Token

String

从IAM获取的用户Token。

缺省值:None

最小长度:1000

最大长度:2000

Content-Type

String

该字段填为:application/json;charset=utf8。

缺省值:None

最小长度:29

最大长度:29

表3 请求Body参数

参数

是否必选

参数类型

描述

start_time

String

搜索起始时间(UTC时间,毫秒级)。
说明:

查询时间最大为30天。

end_time

String

搜索结束时间(UTC时间,毫秒级)。

说明:

查询时间最大为30天。

labels

Map<String,String>

日志过滤条件集合,不同日志来源所需字段不同。

keywords

String

支持关键词精确搜索。关键词指相邻两个分词符之间的单词。

枚举值:

  • error

line_num

String

日志单行序列号,第一次查询时不需要此参数,后续分页查询时需要使用,可从上次查询的返回信息中获取。line_num应在start_time和end_time之间。

最小长度:19

最大长度:19

is_desc

Boolean

顺序或者倒序查询, 默认为false(顺序查询)

枚举值:

  • false

search_type

String

首次查询为 “init”, 分页查询时为 “forwards”或者“backwards”, 默认为首次查询“init”, 与“is_desc”参数配合进行分页查询。

枚举值:

  • forwards

limit

Integer

表示每次查询的日志条数,不填时默认为50,建议您设置为100。

最小值:1

最大值:5000

is_count

Boolean

日志条数统计。默认为false(不统计),true为统计日志条数。

枚举值:true

响应参数

状态码为 200 时:

表4 响应Body参数

参数

参数类型

描述

logs

Array of LogContents objects

日志信息。

count

Integer

日志条数。

表5 LogContents

参数

参数类型

描述

content

String

日志原数据。

最小长度:1

最大长度:10000

line_num

String

日志单行序列号。

最小长度:19

最大长度:19

labels

Map<String,String>

该条日志包含的labels,查询到的日志不同所包含的字段不同。

状态码为 400 时:

表6 响应Body参数

参数

参数类型

描述

error_code

String

错误码。

枚举值:

  • LTS.0403

error_msg

String

调用失败响应信息描述。

枚举值:

  • Invalid projectId

状态码为 401 时:

表7 响应Body参数

参数

参数类型

描述

error_code

String

错误码。

枚举值:

  • LTS.0403

error_msg

String

调用失败响应信息描述。

枚举值:

  • Invalid projectId

状态码为 403 时:

表8 响应Body参数

参数

参数类型

描述

error_code

String

错误码。

枚举值:

  • LTS.0403

error_msg

String

调用失败响应信息描述。

枚举值:

  • Invalid projectId

状态码为 500 时:

表9 响应Body参数

参数

参数类型

描述

error_code

String

错误码。

枚举值:

  • LTS.0403

error_msg

String

调用失败响应信息描述。

枚举值:

  • Invalid projectId

请求示例

日志内容:

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

POST https://{endpoint}/v2/{project_id}/groups/{log_group_id}/streams/{log_stream_id}/content/query 
  • 首次查询日志
    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
    }
  • 分页查询, 以包含“NO 5”的日志为起始点, 查询 “NO 6”、“NO 7”、“NO 8”
    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
    }
  • 分页查询, 以包含“NO 5”的日志为起始点, 查询“NO 8”、“NO 7”、“NO 6”
    { 
      "start_time": 1595659200000, 
      "end_time": 1595659500000,
      "labels":  
          { 
            "hostName": "ecs-kwxtest" 
          }, 
      "keywords": "log",
      "line_num": "1595659490239433658",
      "is_desc": "true",
      "search_type": "backwards",
      "limit": "3",
      "is_count":true
    }
  • 分页查询, 以包含“NO 5”的日志为起始点,查询“NO 2”、“NO 3”、“NO 4”
    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
    }
  • 分页查询, 以包含“NO 5”的日志为起始点, 查询 “NO 4”、“NO 3”、“NO 2”
    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
    }

响应示例

  • 首次查询日志
    { 
      "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"
            }
    ]
    }
  • 分页查询, 以包含“NO 5”的日志为起始点, 查询 “NO 6”、“NO 7”、“NO 8”
    {
        "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"
            }
        ]
    }     
  • 分页查询, 以包含“NO 5”的日志为起始点, 查询 “NO 8”、“NO 7”、“NO 6”
    {
        "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"
            }
        ]
    }
  • 分页查询, 以包含“NO 5”的日志为起始点,查询“NO 2”、“NO 3”、“NO 4”
    {
        "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"
            }
        ]
    }
  • 分页查询, 以包含“NO 5”的日志为起始点, 查询 “NO 4”、“NO 3”、“NO 2”
    {
        "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"
            }
        ]
    }

状态码为 400 时:

BadRequest。非法请求。建议根据error_msg直接修改该请求,不要重试该请求。

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

状态码为 401 时:

AuthFailed。鉴权失败, 请确认token后再次请求 。

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

状态码为 403 时:

Forbidden。请求被拒绝访问。 返回该状态码,表明请求能够到达服务端,且服务端能够理解用户请求,但是拒绝做更多的事情,因为该请求被设置为拒绝访问,建议直接修改该请求,不要重试该请求。

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

状态码为 500 时:

InternalServerError。 表明服务端能被请求访问到,但是服务内部出错。

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

状态码

状态码

描述

200

请求响应成功。

400

BadRequest。非法请求。建议根据error_msg直接修改该请求,不要重试该请求。

401

AuthFailed。鉴权失败,请确认token后再次请求。

403

Forbidden。请求被拒绝访问。返回该状态码,表明请求能够到达服务端,且服务端能够理解用户请求,但是拒绝做更多的事情,因为该请求被设置为拒绝访问,建议直接修改该请求,不要重试该请求。

500

InternalServerError。表明服务端能被请求访问到,但是服务内部出错。

503

ServiceUnavailable。被请求的服务无效,服务不可用。

错误码

请参见错误码