java呼叫阿里雲日誌SDK 查詢日誌
阿新 • • 發佈:2022-03-29
參考: https://help.aliyun.com/document_detail/277466.html
maven依賴
<dependency>
<groupId>com.aliyun.openservices</groupId>
<artifactId>aliyun-log</artifactId>
<version>0.6.33</version>
</dependency>
service層
package com.server.Ali.aliyunlog; import com.aliyun.openservices.log.Client; import com.aliyun.openservices.log.common.LogContent; 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.response.GetLogsResponse; import org.springframework.stereotype.Service; @Service public class SLSQuickStart { //配置AccessKey、服務入口、Project名稱、Logstore名稱等相關資訊。 //阿里雲訪問金鑰AccessKey。更多資訊,請參見訪問金鑰。阿里雲賬號AccessKey擁有所有API的訪問許可權,風險很高。強烈建議您建立並使用RAM使用者進行API訪問或日常運維。 static String accessId = "---"; static String accessKey = "---"; //日誌服務的服務入口。更多資訊,請參見服務入口。 //此處以杭州為例,其它地域請根據實際情況填寫。 static String host = "cn-hangzhou.log.aliyuncs.com"; //建立日誌服務Client。 static Client client = new Client(host, accessId, accessKey); //Project名稱。 static String projectName = "kuebernetes-production"; //Logstore名稱。 static String logstoreName = "production-education"; // 查詢範圍 int from =1648398682; int to =1648481482; //通過SQL查詢日誌。 public int getLogs(int from, int to, String sql) throws LogException { //fromTime和toTime表示查詢日誌的時間範圍,Unix時間戳格式。 GetLogsResponse getLogsResponse = client.GetLogs(projectName, logstoreName, from, to, "", sql); System.out.println("輸出日誌"); for (QueriedLog log : getLogsResponse.GetLogs()) { for (LogContent mContent : log.mLogItem.mContents) { System.out.println(mContent.mKey + " : " + mContent.mValue); } System.out.println("********************"); } //輸出條數 return getLogsResponse.GetCount(); } public void test() throws LogException { System.out.println(this.getLogs(from,to,"220201")); } }