完成日誌採集後,您可以調用GetLogs介面查詢採集到的日誌。本文介紹GetLogs介面樣本。
前提條件
您已完成以下操作:
注意事項
本樣本以華東1(杭州)的公網Endpoint為例,其公網Endpoint為
https://cn-hangzhou.log.aliyuncs.com。如果您通過與Project同地區的其他阿里雲產品訪問Log Service,請使用內網Endpoint
https://cn-hangzhou-intranet.log.aliyuncs.com。關於Log Service支援的地區與Endpoint的對應關係,請參見服務入口。
您可以在SDK代碼的響應對象中調用
IsCompleted()方法,用於判斷本次查詢是否精確。如果
IsCompleted()方法的返回結果為true,表示本次查詢已完成且查詢精確,返回的查詢結果是完整的。如果
IsCompleted()方法的返回結果為false,表示本次查詢已完成但查詢不精確,返回的查詢結果是不完整的。您需要重複請求,擷取完整的查詢結果。關於查詢不精確的更多資訊,請參見查詢不精確可能原因。
參數說明
請求參數
名稱 | 類型 | 是否必填 | 說明 |
project | String | 是 | Project名稱:專案(Project)是Log Service的資源嵌入式管理單元,是進行多使用者隔離與存取控制的主要邊界。更多資訊,請參見管理Project。 |
logstore | String | 是 | Logstore名稱:Logstore是Log Service中日誌資料的採集、儲存和查詢單元。更多資訊,請參見管理Logstore。 |
from | int | 是 | 查詢開始的時間點,使用Unix時間戳記格式。 說明
|
to | int | 是 | 查詢結束的時間點,使用Unix時間戳記格式。 說明
|
topic | String | 否 | 日誌主題。預設值為空白字串。更多資訊,請參見日誌主題(Topic)。 |
query | String | 否 | 查詢語句或分析語句。更多資訊,請參見查詢與分析概述。 在query參數的分析語句中加上 說明 當query參數中有分析語句(SQL 陳述式)時,該介面的line參數和offset參數無效。建議設定該介面的參數為0,需通過SQL語句的LIMIT文法實現翻頁。更多資訊,請參見分頁顯示查詢分析結果。 |
line | int | 否 | 僅當query參數為查詢語句時,該參數有效。表示請求返回的最大日誌條數,最小值為 0,最大值為 100,預設值為 100。 |
offset | int | 否 | 僅當query參數為查詢語句時,該參數有效。表示查詢開始行。預設值為 0。 |
reverse | boolean | 否 | 用於指定返回結果是否按日誌時間戳記降序返回日誌,精確到分鐘層級。
重要
|
powerSql | boolean | 否 | 是否使用SQL獨享版。更多資訊,請參見高效能完全精確查詢與分析(SQL獨享版)。
除通過powerSql參數配置SQL獨享版外,您還可以使用query參數。 |
scan | boolean | 否 | 如果scan設定為true,則查詢將使用掃描模式。 |
forward | boolean | 否 | 僅對於掃描查詢,如果forward設定為true,查詢將擷取下一頁,否則擷取上一頁。 |
返回參數
返回參數請參見:GetLogs - 查詢日誌庫日誌。
原始日誌範例
body_bytes_sent:1750
host:www.example.com
http_referer:www.example.com
http_user_agent:Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10_6_6; it-it) AppleWebKit/533.20.25 (KHTML, like Gecko) Version/5.0.4 Safari/533.20.27
http_x_forwarded_for:203.0.XX.XX
remote_addr:203.0.XX.XX
remote_user:p288
request_length:13741
request_method:GET
request_time:71
request_uri:/request/path-1/file-1
http_code:200
time_local:11/Aug/2021:06:52:27
upstream_response_time:0.66查詢和分析日誌樣本
您可以參考以下程式碼範例,對日誌進行查詢和分析。
在JAVA SDK中調用GetLogs介面的相關限制說明如下:
當設定
query僅為查詢語句(例如request_method:POST)時,可通過line參數控制返回日誌的條數,最大值為100。如果您需要返回更多的日誌,需使用SQL Limit文法。更多資訊,請參見LIMIT子句。當設定
query為查詢和分析語句(request_method:POST | SELECT host, COUNT(*) AS pv GROUP BY host LIMIT 5)時,line參數無效,需使用SQL Limit文法控制返回結果行數。更多資訊,請參見LIMIT子句。
關於查詢和分析語句的更多資訊,請參見基本文法。
樣本1:使用關鍵字查詢日誌
本樣本中將展示如何建立一個GetLogsTest.java檔案,並使用關鍵字path-0/file-5查詢日誌。為控制返回日誌條數,設定line參數為3。樣本如下:
import com.aliyun.openservices.log.Client;
import com.aliyun.openservices.log.common.LogItem;
import com.aliyun.openservices.log.common.QueriedLog;
import com.aliyun.openservices.log.exception.LogException;
import com.aliyun.openservices.log.request.GetLogsRequest;
import com.aliyun.openservices.log.response.GetLogsResponse;
public class GetLogsTest {
public static void main(String[] args) throws LogException {
// 本樣本從環境變數中擷取AccessKey ID和AccessKey Secret。
String accessId = System.getenv("ALIBABA_CLOUD_ACCESS_KEY_ID");
String accessKey = System.getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET");
// 輸入Project名稱。
String project = "your-project-name";
// 設定Log Service的服務存取點。此處以杭州為例,其它地區請根據實際情況填寫。
String host = "cn-hangzhou.log.aliyuncs.com";
// 輸入Logstore名稱。
String logStore = "your-logstore-name";
// 建立Log ServiceClient。
Client client = new Client(host, accessId, accessKey);
// 在指定的Logstore內執行查詢。
try {
// 使用關鍵字path-0/file-5查詢日誌。
String query = "path-0/file-5";
int from = 1754449503;
int to = 1754449510;
// 本樣本中,query參數用於設定查詢語句;line參數用於控制返回日誌條數,取值為3,最大值為100。
GetLogsRequest request = new GetLogsRequest(project, logStore, from, to, "", query);
GetLogsResponse logsResponse = client.GetLogs(request);
System.out.println("-------------Query is started.-------------");
System.out.println("Returned query result count :" + logsResponse.GetCount());
System.out.println("from time is :" + from);
System.out.println("to time is :" + to);
for (QueriedLog log : logsResponse.getLogs()) {
LogItem item = log.GetLogItem();
System.out.println("log time : " + item.mLogTime);
System.out.println("Jsonstring : " + item.ToJsonString());
}
System.out.println("-------------Query is finished.-------------");
} catch (LogException e) {
System.out.println("LogException e :" + e.toString());
System.out.println("error code :" + e.GetErrorCode());
System.out.println("error message :" + e.GetErrorMessage());
throw e;
}
}
}返回結果樣本如下:
-------------Query is started.-------------
Returned query result count :3
from time is :1644573549
to time is :1644573849
log time : 1644573808
Jsonstring : {"remote_addr":"203.0.XX.XX","__topic__":"nginx_access_log","request_uri":"/request/path-0/file-5"...}
log time : 1644573808
Jsonstring : {"remote_addr":"203.0.XX.XX","__topic__":"nginx_access_log","request_uri":"/request/path-0/file-5"...}
log time : 1644573788
Jsonstring : {"remote_addr":"203.0.XX.XX","__topic__":"nginx_access_log","request_uri":"/request/path-0/file-5"...}
-------------Query is finished.-------------
Process finished with exit code 0樣本2:指定特定欄位查詢日誌
本樣本中將展示如何建立一個GetLogsTest.java檔案,並查詢要求方法為POST的日誌。為控制返回日誌條數,設定line參數為3。樣本如下:
import com.aliyun.openservices.log.Client;
import com.aliyun.openservices.log.common.LogItem;
import com.aliyun.openservices.log.common.QueriedLog;
import com.aliyun.openservices.log.exception.LogException;
import com.aliyun.openservices.log.request.GetLogsRequest;
import com.aliyun.openservices.log.response.GetLogsResponse;
public class GetLogsTest {
public static void main(String[] args) throws LogException {
// 本樣本從環境變數中擷取AccessKey ID和AccessKey Secret。
String accessId = System.getenv("ALIBABA_CLOUD_ACCESS_KEY_ID");
String accessKey = System.getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET");
// 輸入Project名稱。
String project = "your-project-name";
// 設定Log Service的服務存取點。此處以杭州為例,其它地區請根據實際情況填寫。
String host = "cn-hangzhou.log.aliyuncs.com";
// 輸入Logstore名稱。
String logStore = "your-logstore-name";
// 建立Log ServiceClient。
Client client = new Client(host, accessId, accessKey);
// 在指定的Logstore內執行查詢。
try {
// 統計要求方法為POST的日誌。
String query = "request_method:POST";
// 設定查詢語句執行時間範圍。
int from = 1754449503;
int to = 1754449510;
// 本樣本中,query參數用於設定查詢語句;line參數用於控制返回日誌條數,取值為3,最大值為100。
GetLogsRequest request = new GetLogsRequest(project, logStore, from, to, "", query);
GetLogsResponse logsResponse = client.GetLogs(request);
System.out.println("-------------Query is started.-------------");
System.out.println("Returned query result count :" + logsResponse.GetCount());
System.out.println("from time is :" + from);
System.out.println("to time is :" + to);
for (QueriedLog log : logsResponse.getLogs()) {
LogItem item = log.GetLogItem();
System.out.println("log time : " + item.mLogTime);
System.out.println("Jsonstring : " + item.ToJsonString());
}
System.out.println("-------------Query is finished.-------------");
} catch (LogException e) {
System.out.println("LogException e :" + e.toString());
System.out.println("error code :" + e.GetErrorCode());
System.out.println("error message :" + e.GetErrorMessage());
throw e;
}
}
}返回結果樣本如下:
-------------Query is started.-------------
Returned query result count :3
from time is :1644574151
to time is :1644574451
log time : 1644574438
Jsonstring : {"remote_addr":"203.0.XX.XX","__topic__":"nginx_access_log","body_bytes_sent":"3604","request_method":"POST"...}
log time : 1644574438
Jsonstring : {"remote_addr":"203.0.XX.XX","__topic__":"nginx_access_log","body_bytes_sent":"3369","request_method":"POST"...}
log time : 1644574438
Jsonstring : {"remote_addr":"203.0.XX.XX","__topic__":"nginx_access_log","body_bytes_sent":"12714","request_method":"POST"...}
-------------Query is finished.-------------
Process finished with exit code 0樣本3:使用SQL語句分析日誌
本樣本中將展示如何建立一個GetLogsTest.java檔案,查詢要求方法為POST的日誌,並統計POST請求的PV數量。樣本如下:
import com.aliyun.openservices.log.Client;
import com.aliyun.openservices.log.common.LogItem;
import com.aliyun.openservices.log.common.QueriedLog;
import com.aliyun.openservices.log.exception.LogException;
import com.aliyun.openservices.log.request.GetLogsRequest;
import com.aliyun.openservices.log.response.GetLogsResponse;
public class GetLogsTest {
public static void main(String[] args) throws LogException {
// 本樣本從環境變數中擷取AccessKey ID和AccessKey Secret。
String accessId = System.getenv("ALIBABA_CLOUD_ACCESS_KEY_ID");
String accessKey = System.getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET");
// 輸入Project名稱。
String project = "your-project-name";
// 設定Log Service的服務存取點。此處以杭州為例,其它地區請根據實際情況填寫。
String host = "cn-hangzhou.log.aliyuncs.com";
// 輸入Logstore名稱。
String logStore = "your-logstore-name";
// 建立Log ServiceClient。
Client client = new Client(host, accessId, accessKey);
// 在指定的Logstore內執行查詢。
try {
// 查詢要求方法為POST的日誌,並統計POST請求的PV數量。
String query = "request_method:POST|select COUNT(*) as pv";
// 設定查詢語句執行時間範圍。
int from = 1754449503;
int to = 1754449510;
// 本樣本中,query參數用於設定查詢語句;line參數用於控制返回日誌條數,取值為3,最大值為100。
GetLogsRequest request = new GetLogsRequest(project, logStore, from, to, "", query);
GetLogsResponse logsResponse = client.GetLogs(request);
System.out.println("-------------Query is started.-------------");
System.out.println("Returned query result count :" + logsResponse.GetCount());
System.out.println("from time is :" + from);
System.out.println("to time is :" + to);
for (QueriedLog log : logsResponse.getLogs()) {
LogItem item = log.GetLogItem();
System.out.println("log time : " + item.mLogTime);
System.out.println("Jsonstring : " + item.ToJsonString());
}
System.out.println("-------------Query is finished.-------------");
} catch (LogException e) {
System.out.println("LogException e :" + e.toString());
System.out.println("error code :" + e.GetErrorCode());
System.out.println("error message :" + e.GetErrorMessage());
throw e;
}
}
}返回結果樣本如下:
-------------Query is started.-------------
Returned query result count :1
from time is :1644574354
to time is :1644574654
log time : 1644574354
Jsonstring : {"pv":"162","logtime":1644574354}
-------------Query is finished.-------------
Process finished with exit code 0樣本4:使用SQL分組分析日誌
本樣本中將展示如何建立一個GetLogsTest.java檔案,查詢要求方法為POST的日誌並且按照host進行分組。樣本如下:
import com.aliyun.openservices.log.Client;
import com.aliyun.openservices.log.common.LogItem;
import com.aliyun.openservices.log.common.QueriedLog;
import com.aliyun.openservices.log.exception.LogException;
import com.aliyun.openservices.log.request.GetLogsRequest;
import com.aliyun.openservices.log.response.GetLogsResponse;
public class GetLogsTest {
public static void main(String[] args) throws LogException {
// 本樣本從環境變數中擷取AccessKey ID和AccessKey Secret。
String accessId = System.getenv("ALIBABA_CLOUD_ACCESS_KEY_ID");
String accessKey = System.getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET");
// 輸入Project名稱。
String project = "your-project-name";
// 設定Log Service的服務存取點。此處以杭州為例,其它地區請根據實際情況填寫。
String host = "cn-hangzhou.log.aliyuncs.com";
// 輸入Logstore名稱。
String logStore = "your-logstore-name";
// 建立Log ServiceClient。
Client client = new Client(host, accessId, accessKey);
// 在指定的Logstore內執行查詢。
try {
// 統計要求方法為POST的日誌並且按照host進行分組。
// 使用SQL文法中的limit語句限制條數為5。
String query = "request_method:POST|select host, COUNT(*) as pv group by host limit 5";
// 設定查詢語句執行時間範圍。
int from = 1754449503;
int to = 1754449510;
// 本樣本中,query參數用於設定查詢語句;line參數用於控制返回日誌條數,取值為3,最大值為100。
GetLogsRequest request = new GetLogsRequest(project, logStore, from, to, "", query);
GetLogsResponse logsResponse = client.GetLogs(request);
System.out.println("-------------Query is started.-------------");
System.out.println("Returned query result count :" + logsResponse.GetCount());
System.out.println("from time is :" + from);
System.out.println("to time is :" + to);
for (QueriedLog log : logsResponse.getLogs()) {
LogItem item = log.GetLogItem();
System.out.println("log time : " + item.mLogTime);
System.out.println("Jsonstring : " + item.ToJsonString());
}
System.out.println("-------------Query is finished.-------------");
} catch (LogException e) {
System.out.println("LogException e :" + e.toString());
System.out.println("error code :" + e.GetErrorCode());
System.out.println("error message :" + e.GetErrorMessage());
throw e;
}
}
}返回結果樣本如下:
-------------Query is started.-------------
Returned query result count :5
from time is :1644574445
to time is :1644574745
log time : 1644574445
Jsonstring : {"pv":"1","host":"www.example1.com","logtime":1644574445}
log time : 1644574445
Jsonstring : {"pv":"1","host":"www.example.org","logtime":1644574445}
log time : 1644574445
Jsonstring : {"pv":"1","host":"www.example.net","logtime":1644574445}
log time : 1644574445
Jsonstring : {"pv":"1","host":"www.example.edu","logtime":1644574445}
log time : 1644574445
Jsonstring : {"pv":"1","host":"www.aliyundoc.com","logtime":1644574445}
-------------Query is finished.-------------
Process finished with exit code 0樣本5:使用SQL分組分析日誌(返回200條)
本樣本中將展示如何建立一個GetLogsTest.java檔案,查詢要求方法為POST的日誌並且按照host進行分組,返回200條日誌。樣本如下:
import com.aliyun.openservices.log.Client;
import com.aliyun.openservices.log.common.LogItem;
import com.aliyun.openservices.log.common.QueriedLog;
import com.aliyun.openservices.log.exception.LogException;
import com.aliyun.openservices.log.request.GetLogsRequest;
import com.aliyun.openservices.log.response.GetLogsResponse;
public class GetLogsTest {
public static void main(String[] args) throws LogException {
// 本樣本從環境變數中擷取AccessKey ID和AccessKey Secret。
String accessId = System.getenv("ALIBABA_CLOUD_ACCESS_KEY_ID");
String accessKey = System.getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET");
// 輸入Project名稱。
String project = "your-project-name";
// 設定Log Service的服務存取點。此處以杭州為例,其它地區請根據實際情況填寫。
String host = "cn-hangzhou.log.aliyuncs.com";
// 輸入Logstore名稱。
String logStore = "your-logstore-name";
// 建立Log ServiceClient。
Client client = new Client(host, accessId, accessKey);
// 在指定的Logstore內執行查詢。
try {
//統計要求方法為POST的日誌並且按照host進行分組。
//使用SQL文法中的limit語句控制返回條數。
String old_query = "request_method:POST|select host, COUNT(*) as pv group by host limit 0,200";
// 設定查詢語句執行時間範圍。
int from = 1754449503;
int to = 1754449510;
// 本樣本中,query參數用於設定查詢語句;line參數用於控制返回日誌條數,取值為3,最大值為100。
GetLogsRequest request = new GetLogsRequest(project, logStore, from, to, "", query);
GetLogsResponse logsResponse = client.GetLogs(request);
System.out.println("-------------Query is started.-------------");
System.out.println("Returned query result count :" + logsResponse.GetCount());
System.out.println("from time is :" + from);
System.out.println("to time is :" + to);
for (QueriedLog log : logsResponse.getLogs()) {
LogItem item = log.GetLogItem();
System.out.println("log time : " + item.mLogTime);
System.out.println("Jsonstring : " + item.ToJsonString());
}
System.out.println("-------------Query is finished.-------------");
} catch (LogException e) {
System.out.println("LogException e :" + e.toString());
System.out.println("error code :" + e.GetErrorCode());
System.out.println("error message :" + e.GetErrorMessage());
throw e;
}
}
}返回結果樣本如下:
-------------Query is started.-------------
Returned query result count :200
from time is :1644574445
to time is :1644574745
log time : 1644574445
Jsonstring : {"pv":"1","host":"www.example1.com","logtime":1644574445}
log time : 1644574445
Jsonstring : {"pv":"1","host":"www.example.org","logtime":1644574445}
log time : 1644574445
Jsonstring : {"pv":"1","host":"www.example.net","logtime":1644574445}
log time : 1644574445
Jsonstring : {"pv":"1","host":"www.example.edu","logtime":1644574445}
log time : 1644574445
Jsonstring : {"pv":"1","host":"www.aliyundoc.com","logtime":1644574445}
......
-------------Query is finished.-------------
Process finished with exit code 0樣本6:使用SQL統計過去一小時內的日誌總條數
本樣本中將展示如何建立一個GetLogsTest.java檔案,並使用SQL語句*|select count(*) as count查詢過去一小時內的日誌總條數。樣本如下:
import com.aliyun.openservices.log.Client;
import com.aliyun.openservices.log.common.LogItem;
import com.aliyun.openservices.log.common.QueriedLog;
import com.aliyun.openservices.log.exception.LogException;
import com.aliyun.openservices.log.request.GetLogsRequest;
import com.aliyun.openservices.log.response.GetLogsResponse;
import java.util.Date;
public class GetLogsTest {
public static void main(String[] args) throws LogException {
// 本樣本從環境變數中擷取AccessKey ID和AccessKey Secret。
String accessId = System.getenv("ALIBABA_CLOUD_ACCESS_KEY_ID");
String accessKey = System.getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET");
// 輸入Project名稱。
String project = "your-project-name";
// 設定Log Service的服務存取點。此處以杭州為例,其它地區請根據實際情況填寫。
String host = "cn-hangzhou.log.aliyuncs.com";
// 輸入Logstore名稱。
String logStore = "your-logstore-name";
// 建立Log ServiceClient。
Client client = new Client(host, accessId, accessKey);
// 在指定的Logstore內執行查詢。
try {
// 查詢日誌總條數。
String query = "*|select count(*) as count";
// 查詢時間區間為1小時(3600秒)。
int from = (int) (new Date().getTime() / 1000 - 3600);
int to = (int) (new Date().getTime() / 1000);
int offset = 0;
int line = 200;
// 本樣本中,query參數用於設定查詢語句;line參數用於控制返回日誌條數,取值為3,最大值為100。
GetLogsRequest request = new GetLogsRequest(project, logStore, from, to, "", query,line,offset,true);
GetLogsResponse logsResponse = client.GetLogs(request);
System.out.println("-------------Query is started.-------------");
System.out.println("Returned query result count :" + logsResponse.GetCount());
System.out.println("from time is :" + from);
System.out.println("to time is :" + to);
for (QueriedLog log : logsResponse.getLogs()) {
LogItem item = log.GetLogItem();
System.out.println("log time : " + item.mLogTime);
System.out.println("Jsonstring : " + item.ToJsonString());
}
System.out.println("-------------Query is finished.-------------");
} catch (LogException e) {
System.out.println("LogException e :" + e.toString());
System.out.println("error code :" + e.GetErrorCode());
System.out.println("error message :" + e.GetErrorMessage());
throw e;
}
}
}從返回結果可以看出,過去一小時內的日誌總條數為19051條。返回結果樣本如下:
from time is :1675041679
to time is :1675045279
Returned sql result count :1
Jsonstring : {"count":"19051","logtime":1675041679}
-------------Query is finished.-------------相關文檔
在調用API介面過程中,若服務端返回結果中包含錯誤資訊,則表示調用API介面失敗。您可以參考API錯誤碼對照表尋找對應的解決方案。更多資訊,請參見API錯誤處理對照表。
Log Service除自研的SDK外,還支援公用的阿里雲SDK,關於阿里雲SDK的使用方式,請參見Log Service_SDK中心-阿里雲OpenAPI開發人員門戶。
為滿足越來越多的自動化Log Service配置需求,Log Service提供命令列工具CLI(Command Line Interface)。更多資訊,請參見Log Service命令列工具CLI。
更多範例程式碼,請參見Aliyun Log Java SDK on GitHub。