java调用阿里云日志SDK 查询日志
参考: 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"));
}
}
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· winform 绘制太阳,地球,月球 运作规律
· 超详细:普通电脑也行Windows部署deepseek R1训练数据并当服务器共享给他人
· 上周热点回顾(3.3-3.9)
· AI 智能体引爆开源社区「GitHub 热点速览」
· 写一个简单的SQL生成工具
2019-03-29 修改最后一次 已commit 的备注