更新时间:2024-02-01 GMT+08:00

查询日志

功能介绍

该接口用于查询不同维度(例如集群、IP、应用等)下的日志内容,支持分页查询。

调用方法

请参见如何调用API

URI

POST /v1/{project_id}/als/action

表1 路径参数

参数

是否必选

参数类型

描述

project_id

String

租户从IAM申请到的projectid,一般为32位字符串。

表2 Query参数

参数

是否必选

参数类型

描述

type

String

日志接口调用方式,当值为"querylogs"时接口功能为查询日志内容。

请求参数

表3 请求Header参数

参数

是否必选

参数类型

描述

X-Auth-Token

String

从IAM服务获取的用户Token。

Content-Type

String

指定类型为application/json。

枚举值:

  • application/json

表4 请求Body参数

参数

是否必选

参数类型

描述

category

String

取值范围 app_log,node _log,custom_log 日志类型字段:app_log:应用日志 node_log:主机日志 custom_log:自定义配置路径日志。

endTime

Long

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

hideSyslog

Integer

取值范围 0、1 。搜索时是否隐藏系统日志,默认0为隐藏1为显示。

keyWord

String

1.支持关键词精确搜索。关键词指相邻两个分词符之间的单词。 2.支持关键词模糊匹配搜索,例如输入“RROR”或“ERRO?”或“ROR”或“ERR*”或“ER*OR”。 3.支持短语精确搜索,例如输入“Start to refresh alm Statistic”。 4.支持关键词的“与”、“或”组合搜索。格式为“query&&logs”或“query||logs”。 说明: 当前默认分词符有

, '";=()[]{}@&<>/:\n\t\r

lineNum

String

日志单行序列号第一次查询时不需要此参数,后续分页查询时需要使用可从上次查询的返回信息中获取.

pageSize/size

String

表示每次查询的日志条数不填时默认为5000,建议您设置为100。 第一次查询时使用pageSize 后续分页查询时使用size。

searchKey

SearchKey object

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

startTime

Long

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

type

String

表示此次查询为分页查询,第一次查询时不需要此参数,后续分页查询时需要使用。

isDesc

Boolean

标识按照lineNum升序查询还是降序查询。

true:降序(lineNum由大到小:时间从新到老)。

false:升序(lineNum由小到大:即时间从老到新)。

表5 SearchKey

参数

是否必选

参数类型

描述

appName

String

应用名称。

clusterId

String

CCE集群ID。

hostIP

String

日志所在虚拟机IP。

nameSpace

String

CCE容器集群的命名空间。

pathFile

String

日志文件名称。

podName

String

容器实例名称。

响应参数

状态码: 200

表6 响应Body参数

参数

参数类型

描述

errorCode

String

响应码,SVCSTG_AMS_2000000代表正常返回。

errorMessage

String

响应信息描述。

result

String

查询结果元数据信息,包括返回总数及结果。

状态码: 400

表7 响应Body参数

参数

参数类型

描述

errorCode

String

响应码。

errorMessage

String

响应信息描述。

状态码: 401

表8 响应Body参数

参数

参数类型

描述

errorCode

String

响应码。

errorMessage

String

响应信息描述。

状态码: 403

表9 响应Body参数

参数

参数类型

描述

errorCode

String

响应码。

errorMessage

String

响应信息描述。

状态码: 500

表10 响应Body参数

参数

参数类型

描述

errorCode

String

响应码。

errorMessage

String

响应信息描述。

状态码: 503

表11 响应Body参数

参数

参数类型

描述

errorCode

String

响应码。

errorMessage

String

响应信息描述。

请求示例

  • 查询集群“c69xxxc-5xxx-1xxx-8xxx5-02xxxxx40”下的应用日志

    "POST https://{endpoint}/v1/{project_id}/als/action?type=querylogs"
    
    {
      "category" : "app_log",
      "endTime" : 15389000003,
      "hideSyslog" : 0,
      "keyWord" : "",
      "searchKey" : {
        "clusterId" : "c69xxxc-5xxx-1xxx-8xxx5-02xxxxx40"
      },
      "startTime" : 15389000003
    }
  • 示例2(分页查询) 说明: 1.分页查询时需要增加三个参数:lineNum(上一次查询出的内容中最后一条日志的序列号)、type(取值"next")和size。 2.category、searchKey、keyWord、startTime、endTime等条件需要保持和第一次查询时相同。 3.如果需要继续分页查询,则修改请求参数中的lineNum为上次查询结果中最后一条日志的lineNum的值即可,以此类推。

    /v1/{project_id}/als/action?type=querylogs
    
    {
      "category" : "app_log",
      "searchKey" : {
        "clusterId" : "874xxx9a2-xxxf-xxx-8xxe-02xxxxx3"
      },
      "keyWord" : "",
      "startTime" : 156946300095,
      "endTime" : 15694600008895,
      "lineNum" : "1569463900000047",
      "type" : "next",
      "size" : 100,
      "hideSyslog" : 0
    }

响应示例

状态码: 200

OK 请求响应成功。

{
  "errorCode" : "SVCSTG.ALS.200.200",
  "errorMessage" : "Query data success",
  "result" : [ {
    "data" : [ {
      "appName" : "axxs0712",
      "category" : "apx",
      "clusterId" : "c6xxxx7c-54cd-11e8-8055-025xxx1e40",
      "collectTime" : 153900000983,
      "containerName" : "contsssner-0",
      "hostIP" : "1xx.xxx.0.1xxx",
      "hostId" : "c11xxxxx11-0000b-4925-bef4-d0xxxx9b0",
      "hostName" : "1x2.168.0.xxx",
      "lineNum" : "1xxx23xxxxxx2VW5xxxxxx0ZWdlcg==",
      "logContent" : "warn:2018/10/09 06:57:01 helloworld.go:108: the main processis running now.",
      "logContentSize" : null,
      "loghash" : "4xxxxx0d40a83c17f262540xxxxxxxxfeaa30eb",
      "nameSpace" : "default",
      "pathFile" : "/xxx/xxx/xxx/xxx/xxx/xxx.trxe",
      "podName" : "axxx12-7xxf884-qxxwp",
      "serviceID" : ""
    } ],
    "total" : 5000
  } ]
}

SDK代码示例

SDK代码示例如下。

  • 查询集群“c69xxxc-5xxx-1xxx-8xxx5-02xxxxx40”下的应用日志

     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    package com.huaweicloud.sdk.test;
    
    import com.huaweicloud.sdk.core.auth.ICredential;
    import com.huaweicloud.sdk.core.auth.BasicCredentials;
    import com.huaweicloud.sdk.core.exception.ConnectionException;
    import com.huaweicloud.sdk.core.exception.RequestTimeoutException;
    import com.huaweicloud.sdk.core.exception.ServiceResponseException;
    import com.huaweicloud.sdk.aom.v2.region.AomRegion;
    import com.huaweicloud.sdk.aom.v2.*;
    import com.huaweicloud.sdk.aom.v2.model.*;
    
    
    public class ListLogItemsSolution {
    
        public static void main(String[] args) {
            // The AK and SK used for authentication are hard-coded or stored in plaintext, which has great security risks. It is recommended that the AK and SK be stored in ciphertext in configuration files or environment variables and decrypted during use to ensure security.
            // In this example, AK and SK are stored in environment variables for authentication. Before running this example, set environment variables CLOUD_SDK_AK and CLOUD_SDK_SK in the local environment
            String ak = System.getenv("CLOUD_SDK_AK");
            String sk = System.getenv("CLOUD_SDK_SK");
    
            ICredential auth = new BasicCredentials()
                    .withAk(ak)
                    .withSk(sk);
    
            AomClient client = AomClient.newBuilder()
                    .withCredential(auth)
                    .withRegion(AomRegion.valueOf("<YOUR REGION>"))
                    .build();
            ListLogItemsRequest request = new ListLogItemsRequest();
            request.withType("<type>");
            QueryBodyParam body = new QueryBodyParam();
            SearchKey searchKeybody = new SearchKey();
            searchKeybody.withClusterId("c69xxxc-5xxx-1xxx-8xxx5-02xxxxx40");
            body.withStartTime(15389000003L);
            body.withSearchKey(searchKeybody);
            body.withKeyWord("");
            body.withHideSyslog(0);
            body.withEndTime(15389000003L);
            body.withCategory("app_log");
            request.withBody(body);
            try {
                ListLogItemsResponse response = client.listLogItems(request);
                System.out.println(response.toString());
            } catch (ConnectionException e) {
                e.printStackTrace();
            } catch (RequestTimeoutException e) {
                e.printStackTrace();
            } catch (ServiceResponseException e) {
                e.printStackTrace();
                System.out.println(e.getHttpStatusCode());
                System.out.println(e.getRequestId());
                System.out.println(e.getErrorCode());
                System.out.println(e.getErrorMsg());
            }
        }
    }
    
  • 示例2(分页查询) 说明: 1.分页查询时需要增加三个参数:lineNum(上一次查询出的内容中最后一条日志的序列号)、type(取值"next")和size。 2.category、searchKey、keyWord、startTime、endTime等条件需要保持和第一次查询时相同。 3.如果需要继续分页查询,则修改请求参数中的lineNum为上次查询结果中最后一条日志的lineNum的值即可,以此类推。

     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    package com.huaweicloud.sdk.test;
    
    import com.huaweicloud.sdk.core.auth.ICredential;
    import com.huaweicloud.sdk.core.auth.BasicCredentials;
    import com.huaweicloud.sdk.core.exception.ConnectionException;
    import com.huaweicloud.sdk.core.exception.RequestTimeoutException;
    import com.huaweicloud.sdk.core.exception.ServiceResponseException;
    import com.huaweicloud.sdk.aom.v2.region.AomRegion;
    import com.huaweicloud.sdk.aom.v2.*;
    import com.huaweicloud.sdk.aom.v2.model.*;
    
    
    public class ListLogItemsSolution {
    
        public static void main(String[] args) {
            // The AK and SK used for authentication are hard-coded or stored in plaintext, which has great security risks. It is recommended that the AK and SK be stored in ciphertext in configuration files or environment variables and decrypted during use to ensure security.
            // In this example, AK and SK are stored in environment variables for authentication. Before running this example, set environment variables CLOUD_SDK_AK and CLOUD_SDK_SK in the local environment
            String ak = System.getenv("CLOUD_SDK_AK");
            String sk = System.getenv("CLOUD_SDK_SK");
    
            ICredential auth = new BasicCredentials()
                    .withAk(ak)
                    .withSk(sk);
    
            AomClient client = AomClient.newBuilder()
                    .withCredential(auth)
                    .withRegion(AomRegion.valueOf("<YOUR REGION>"))
                    .build();
            ListLogItemsRequest request = new ListLogItemsRequest();
            request.withType("<type>");
            QueryBodyParam body = new QueryBodyParam();
            SearchKey searchKeybody = new SearchKey();
            searchKeybody.withClusterId("874xxx9a2-xxxf-xxx-8xxe-02xxxxx3");
            body.withType("next");
            body.withStartTime(156946300095L);
            body.withSearchKey(searchKeybody);
            body.withLineNum("1569463900000047");
            body.withKeyWord("");
            body.withHideSyslog(0);
            body.withEndTime(15694600008895L);
            body.withCategory("app_log");
            request.withBody(body);
            try {
                ListLogItemsResponse response = client.listLogItems(request);
                System.out.println(response.toString());
            } catch (ConnectionException e) {
                e.printStackTrace();
            } catch (RequestTimeoutException e) {
                e.printStackTrace();
            } catch (ServiceResponseException e) {
                e.printStackTrace();
                System.out.println(e.getHttpStatusCode());
                System.out.println(e.getRequestId());
                System.out.println(e.getErrorCode());
                System.out.println(e.getErrorMsg());
            }
        }
    }
    
  • 查询集群“c69xxxc-5xxx-1xxx-8xxx5-02xxxxx40”下的应用日志

     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    # coding: utf-8
    
    from huaweicloudsdkcore.auth.credentials import BasicCredentials
    from huaweicloudsdkaom.v2.region.aom_region import AomRegion
    from huaweicloudsdkcore.exceptions import exceptions
    from huaweicloudsdkaom.v2 import *
    
    if __name__ == "__main__":
        # The AK and SK used for authentication are hard-coded or stored in plaintext, which has great security risks. It is recommended that the AK and SK be stored in ciphertext in configuration files or environment variables and decrypted during use to ensure security.
        # In this example, AK and SK are stored in environment variables for authentication. Before running this example, set environment variables CLOUD_SDK_AK and CLOUD_SDK_SK in the local environment
        ak = os.getenv("CLOUD_SDK_AK")
        sk = os.getenv("CLOUD_SDK_SK")
    
        credentials = BasicCredentials(ak, sk) \
    
        client = AomClient.new_builder() \
            .with_credentials(credentials) \
            .with_region(AomRegion.value_of("<YOUR REGION>")) \
            .build()
    
        try:
            request = ListLogItemsRequest()
            request.type = "<type>"
            searchKeybody = SearchKey(
                cluster_id="c69xxxc-5xxx-1xxx-8xxx5-02xxxxx40"
            )
            request.body = QueryBodyParam(
                start_time=15389000003,
                search_key=searchKeybody,
                key_word="",
                hide_syslog=0,
                end_time=15389000003,
                category="app_log"
            )
            response = client.list_log_items(request)
            print(response)
        except exceptions.ClientRequestException as e:
            print(e.status_code)
            print(e.request_id)
            print(e.error_code)
            print(e.error_msg)
    
  • 示例2(分页查询) 说明: 1.分页查询时需要增加三个参数:lineNum(上一次查询出的内容中最后一条日志的序列号)、type(取值"next")和size。 2.category、searchKey、keyWord、startTime、endTime等条件需要保持和第一次查询时相同。 3.如果需要继续分页查询,则修改请求参数中的lineNum为上次查询结果中最后一条日志的lineNum的值即可,以此类推。

     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    # coding: utf-8
    
    from huaweicloudsdkcore.auth.credentials import BasicCredentials
    from huaweicloudsdkaom.v2.region.aom_region import AomRegion
    from huaweicloudsdkcore.exceptions import exceptions
    from huaweicloudsdkaom.v2 import *
    
    if __name__ == "__main__":
        # The AK and SK used for authentication are hard-coded or stored in plaintext, which has great security risks. It is recommended that the AK and SK be stored in ciphertext in configuration files or environment variables and decrypted during use to ensure security.
        # In this example, AK and SK are stored in environment variables for authentication. Before running this example, set environment variables CLOUD_SDK_AK and CLOUD_SDK_SK in the local environment
        ak = os.getenv("CLOUD_SDK_AK")
        sk = os.getenv("CLOUD_SDK_SK")
    
        credentials = BasicCredentials(ak, sk) \
    
        client = AomClient.new_builder() \
            .with_credentials(credentials) \
            .with_region(AomRegion.value_of("<YOUR REGION>")) \
            .build()
    
        try:
            request = ListLogItemsRequest()
            request.type = "<type>"
            searchKeybody = SearchKey(
                cluster_id="874xxx9a2-xxxf-xxx-8xxe-02xxxxx3"
            )
            request.body = QueryBodyParam(
                type="next",
                start_time=156946300095,
                search_key=searchKeybody,
                line_num="1569463900000047",
                key_word="",
                hide_syslog=0,
                end_time=15694600008895,
                category="app_log"
            )
            response = client.list_log_items(request)
            print(response)
        except exceptions.ClientRequestException as e:
            print(e.status_code)
            print(e.request_id)
            print(e.error_code)
            print(e.error_msg)
    
  • 查询集群“c69xxxc-5xxx-1xxx-8xxx5-02xxxxx40”下的应用日志

     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    package main
    
    import (
    	"fmt"
    	"github.com/huaweicloud/huaweicloud-sdk-go-v3/core/auth/basic"
        aom "github.com/huaweicloud/huaweicloud-sdk-go-v3/services/aom/v2"
    	"github.com/huaweicloud/huaweicloud-sdk-go-v3/services/aom/v2/model"
        region "github.com/huaweicloud/huaweicloud-sdk-go-v3/services/aom/v2/region"
    )
    
    func main() {
        // The AK and SK used for authentication are hard-coded or stored in plaintext, which has great security risks. It is recommended that the AK and SK be stored in ciphertext in configuration files or environment variables and decrypted during use to ensure security.
        // In this example, AK and SK are stored in environment variables for authentication. Before running this example, set environment variables CLOUD_SDK_AK and CLOUD_SDK_SK in the local environment
        ak := os.Getenv("CLOUD_SDK_AK")
        sk := os.Getenv("CLOUD_SDK_SK")
    
        auth := basic.NewCredentialsBuilder().
            WithAk(ak).
            WithSk(sk).
            Build()
    
        client := aom.NewAomClient(
            aom.AomClientBuilder().
                WithRegion(region.ValueOf("<YOUR REGION>")).
                WithCredential(auth).
                Build())
    
        request := &model.ListLogItemsRequest{}
    	request.Type = "<type>"
    	searchKeybody := &model.SearchKey{
    		ClusterId: "c69xxxc-5xxx-1xxx-8xxx5-02xxxxx40",
    	}
    	keyWordQueryBodyParam:= ""
    	hideSyslogQueryBodyParam:= int32(0)
    	request.Body = &model.QueryBodyParam{
    		StartTime: int64(15389000003),
    		SearchKey: searchKeybody,
    		KeyWord: &keyWordQueryBodyParam,
    		HideSyslog: &hideSyslogQueryBodyParam,
    		EndTime: int64(15389000003),
    		Category: "app_log",
    	}
    	response, err := client.ListLogItems(request)
    	if err == nil {
            fmt.Printf("%+v\n", response)
        } else {
            fmt.Println(err)
        }
    }
    
  • 示例2(分页查询) 说明: 1.分页查询时需要增加三个参数:lineNum(上一次查询出的内容中最后一条日志的序列号)、type(取值"next")和size。 2.category、searchKey、keyWord、startTime、endTime等条件需要保持和第一次查询时相同。 3.如果需要继续分页查询,则修改请求参数中的lineNum为上次查询结果中最后一条日志的lineNum的值即可,以此类推。

     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    package main
    
    import (
    	"fmt"
    	"github.com/huaweicloud/huaweicloud-sdk-go-v3/core/auth/basic"
        aom "github.com/huaweicloud/huaweicloud-sdk-go-v3/services/aom/v2"
    	"github.com/huaweicloud/huaweicloud-sdk-go-v3/services/aom/v2/model"
        region "github.com/huaweicloud/huaweicloud-sdk-go-v3/services/aom/v2/region"
    )
    
    func main() {
        // The AK and SK used for authentication are hard-coded or stored in plaintext, which has great security risks. It is recommended that the AK and SK be stored in ciphertext in configuration files or environment variables and decrypted during use to ensure security.
        // In this example, AK and SK are stored in environment variables for authentication. Before running this example, set environment variables CLOUD_SDK_AK and CLOUD_SDK_SK in the local environment
        ak := os.Getenv("CLOUD_SDK_AK")
        sk := os.Getenv("CLOUD_SDK_SK")
    
        auth := basic.NewCredentialsBuilder().
            WithAk(ak).
            WithSk(sk).
            Build()
    
        client := aom.NewAomClient(
            aom.AomClientBuilder().
                WithRegion(region.ValueOf("<YOUR REGION>")).
                WithCredential(auth).
                Build())
    
        request := &model.ListLogItemsRequest{}
    	request.Type = "<type>"
    	searchKeybody := &model.SearchKey{
    		ClusterId: "874xxx9a2-xxxf-xxx-8xxe-02xxxxx3",
    	}
    	typeQueryBodyParam:= "next"
    	lineNumQueryBodyParam:= "1569463900000047"
    	keyWordQueryBodyParam:= ""
    	hideSyslogQueryBodyParam:= int32(0)
    	request.Body = &model.QueryBodyParam{
    		Type: &typeQueryBodyParam,
    		StartTime: int64(156946300095),
    		SearchKey: searchKeybody,
    		LineNum: &lineNumQueryBodyParam,
    		KeyWord: &keyWordQueryBodyParam,
    		HideSyslog: &hideSyslogQueryBodyParam,
    		EndTime: int64(15694600008895),
    		Category: "app_log",
    	}
    	response, err := client.ListLogItems(request)
    	if err == nil {
            fmt.Printf("%+v\n", response)
        } else {
            fmt.Println(err)
        }
    }
    

更多编程语言的SDK代码示例,请参见API Explorer的代码示例页签,可生成自动对应的SDK代码示例。

状态码

状态码

描述

200

OK 请求响应成功。

400

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

401

Unauthorized 在客户端提供认证信息后,返回该状态码,表明服务端指出客户端所提供的认证信息不正确或非法。

403

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

500

InternalServerError 表明服务端能被请求访问到,但是不能理解用户的请求。

503

ServiceUnavailable 被请求的服务无效。建议直接修改该请求,不要重试该请求。

错误码

请参见错误码