• 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"));
        }
    }
    
  • 相关阅读:
    bzoj4195 [Noi2015]程序自动分析
    bzoj4236 JOIOJI hash 模拟
    bzoj1012 [JSOI2008]最大数maxnumber
    day 4 名片管理系统 -函数版
    day 3 局部变量 全局变量
    day 2 函数的嵌套
    day1 函数 (独立功能代码块)
    day 14 元组
    day 13 字典dict 操作
    day 12 列表字典 补充
  • 原文地址:https://www.cnblogs.com/kaibindirver/p/16073399.html
Copyright © 2020-2023  润新知