Querying Logs
Function
This API is used to query logs by different dimensions, such as by cluster, IP address, or application. Pagination queries are supported.
Calling Method
For details, see Calling APIs.
URI
POST /v1/{project_id}/als/action
Parameter |
Mandatory |
Type |
Description |
---|---|---|---|
project_id |
Yes |
String |
Project ID obtained from IAM. Generally, a project ID contains 32 characters. |
Parameter |
Mandatory |
Type |
Description |
---|---|---|---|
type |
Yes |
String |
Log API call mode. When the value is querylogs, this API is used to query logs. |
Request Parameters
Parameter |
Mandatory |
Type |
Description |
---|---|---|---|
X-Auth-Token |
Yes |
String |
User token obtained from IAM. |
Content-Type |
Yes |
String |
Content type, which is application/json. |
Parameter |
Mandatory |
Type |
Description |
---|---|---|---|
category |
Yes |
String |
Log type. Values: app_log: application log. node_log: node log.custom_log: log in a custom path. |
endTime |
Yes |
Long |
End time of the query (UTC, in ms). |
hideSyslog |
No |
Integer |
Whether to hide system logs during log queries. 0: Hide system logs. 1: Show system logs. |
keyWord |
No |
String |
Keyword for search.
|
lineNum |
No |
String |
Sequence number of the final log in the last query result. This parameter is not required for the first query, but is required for subsequent pagination queries. |
pageSize/size |
No |
String |
Number of logs queried each time. Default value: 5000. Recommended value: 100. For the first query, pageSize is used. For subsequent pagination queries, size is used. |
searchKey |
Yes |
SearchKey object |
Log filter criteria, which vary according to log sources. |
startTime |
Yes |
Long |
Start time of the query (UTC, in ms). |
type |
No |
String |
Pagination query. This parameter is not required for the first query, but is required for subsequent pagination queries. |
isDesc |
No |
Boolean |
Whether to query logs based on lineNum in ascending or descending order. true: lineNum in descending order (from the latest time to the earliest time) false: lineNum in ascending order (from the earliest time to the latest time) |
Parameter |
Mandatory |
Type |
Description |
---|---|---|---|
appName |
No |
String |
Application name. |
clusterId |
Yes |
String |
CCE cluster ID. |
hostIP |
No |
String |
IP address of the VM where logs are located. |
nameSpace |
No |
String |
CCE cluster namespace. |
pathFile |
No |
String |
Log file name. |
podName |
No |
String |
Container instance name. |
Response Parameters
Status code: 200
Parameter |
Type |
Description |
---|---|---|
errorCode |
String |
Response code. SVCSTG_AMS_2000000: Success response. |
errorMessage |
String |
Response message. |
result |
String |
Metadata, including results and the total number of returned records. |
Status code: 400
Parameter |
Type |
Description |
---|---|---|
errorCode |
String |
Response code. |
errorMessage |
String |
Response message. |
Status code: 401
Parameter |
Type |
Description |
---|---|---|
errorCode |
String |
Response code. |
errorMessage |
String |
Response message. |
Status code: 403
Parameter |
Type |
Description |
---|---|---|
errorCode |
String |
Response code. |
errorMessage |
String |
Response message. |
Status code: 500
Parameter |
Type |
Description |
---|---|---|
errorCode |
String |
Response code. |
errorMessage |
String |
Response message. |
Status code: 503
Parameter |
Type |
Description |
---|---|---|
errorCode |
String |
Response code. |
errorMessage |
String |
Response message. |
Example Requests
-
Example 1: Query application logs under a cluster.
"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 }
-
Example 2: Perform pagination queries.
Notes:
-
For pagination queries, the lineNum (sequence number of the final log in the last query result), type (value: next), and size parameters need to be added.
-
The values of category, searchKey, keyWord, startTime, and endTime must be the same as those in the first query.
-
To implement another pagination query, change the value of lineNum to the sequence number of the final log in the last query result. The rest may be deduced by analogy.
/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 }
-
Example Responses
Status code: 200
OK: The request is successful.
{ "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 Sample Code
The SDK sample code is as follows.
Java
-
Example 1: Query application logs under a cluster.
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
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"); String projectId = "{project_id}"; ICredential auth = new BasicCredentials() .withProjectId(projectId) .withAk(ak) .withSk(sk); AomClient client = AomClient.newBuilder() .withCredential(auth) .withRegion(AomRegion.valueOf("<YOUR REGION>")) .build(); ListLogItemsRequest request = new ListLogItemsRequest(); 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()); } } }
-
Example 2: Perform pagination queries.
Notes:
-
For pagination queries, the lineNum (sequence number of the final log in the last query result), type (value: next), and size parameters need to be added.
-
The values of category, searchKey, keyWord, startTime, and endTime must be the same as those in the first query.
-
To implement another pagination query, change the value of lineNum to the sequence number of the final log in the last query result. The rest may be deduced by analogy.
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 59
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"); String projectId = "{project_id}"; ICredential auth = new BasicCredentials() .withProjectId(projectId) .withAk(ak) .withSk(sk); AomClient client = AomClient.newBuilder() .withCredential(auth) .withRegion(AomRegion.valueOf("<YOUR REGION>")) .build(); ListLogItemsRequest request = new ListLogItemsRequest(); 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()); } } }
-
Python
-
Example 1: Query application logs under a cluster.
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
# coding: utf-8 import os 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.environ["CLOUD_SDK_AK"] sk = os.environ["CLOUD_SDK_SK"] projectId = "{project_id}" credentials = BasicCredentials(ak, sk, projectId) client = AomClient.new_builder() \ .with_credentials(credentials) \ .with_region(AomRegion.value_of("<YOUR REGION>")) \ .build() try: request = ListLogItemsRequest() 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)
-
Example 2: Perform pagination queries.
Notes:
-
For pagination queries, the lineNum (sequence number of the final log in the last query result), type (value: next), and size parameters need to be added.
-
The values of category, searchKey, keyWord, startTime, and endTime must be the same as those in the first query.
-
To implement another pagination query, change the value of lineNum to the sequence number of the final log in the last query result. The rest may be deduced by analogy.
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
# coding: utf-8 import os 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.environ["CLOUD_SDK_AK"] sk = os.environ["CLOUD_SDK_SK"] projectId = "{project_id}" credentials = BasicCredentials(ak, sk, projectId) client = AomClient.new_builder() \ .with_credentials(credentials) \ .with_region(AomRegion.value_of("<YOUR REGION>")) \ .build() try: request = ListLogItemsRequest() 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)
-
Go
-
Example 1: Query application logs under a cluster.
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
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") projectId := "{project_id}" auth := basic.NewCredentialsBuilder(). WithAk(ak). WithSk(sk). WithProjectId(projectId). Build() client := aom.NewAomClient( aom.AomClientBuilder(). WithRegion(region.ValueOf("<YOUR REGION>")). WithCredential(auth). Build()) request := &model.ListLogItemsRequest{} 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) } }
-
Example 2: Perform pagination queries.
Notes:
-
For pagination queries, the lineNum (sequence number of the final log in the last query result), type (value: next), and size parameters need to be added.
-
The values of category, searchKey, keyWord, startTime, and endTime must be the same as those in the first query.
-
To implement another pagination query, change the value of lineNum to the sequence number of the final log in the last query result. The rest may be deduced by analogy.
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
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") projectId := "{project_id}" auth := basic.NewCredentialsBuilder(). WithAk(ak). WithSk(sk). WithProjectId(projectId). Build() client := aom.NewAomClient( aom.AomClientBuilder(). WithRegion(region.ValueOf("<YOUR REGION>")). WithCredential(auth). Build()) request := &model.ListLogItemsRequest{} 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) } }
-
More
For SDK sample code of more programming languages, see the Sample Code tab in API Explorer. SDK sample code can be automatically generated.
Status Codes
Status Code |
Description |
---|---|
200 |
OK: The request is successful. |
400 |
Bad Request: The request is invalid. The client should not repeat the request without modifications. |
401 |
Unauthorized: The authentication information is incorrect or invalid. |
403 |
Forbidden: The request is rejected. The server has received the request and understood it, but the server refuses to respond to it. The client should not repeat the request without modifications. |
500 |
Internal Server Error: The server is able to receive the request but unable to understand the request. |
503 |
Service Unavailable: The requested service is invalid. The client should not repeat the request without modifications. |
Error Codes
See Error Codes.
Feedback
Was this page helpful?
Provide feedbackThank you very much for your feedback. We will continue working to improve the documentation.See the reply and handling status in My Cloud VOC.
For any further questions, feel free to contact us through the chatbot.
Chatbot