使用GetLogs接口查询日志

完成日志采集后,您可以调用GetLogs接口查询采集到的日志。本文介绍GetLogs接口示例。

前提条件

您已完成以下操作:

注意事项

  • 本示例以华东1(杭州)的公网Endpoint为例,其公网Endpointhttps://cn-hangzhou.log.aliyuncs.com

    如果您通过与Project同地域的其他阿里云产品访问日志服务,请使用内网Endpointhttps://cn-hangzhou-intranet.log.aliyuncs.com

    关于日志服务支持的地域与Endpoint的对应关系,请参见服务入口

  • 您可以在SDK代码的响应对象中调用IsCompleted()方法,用于判断本次查询是否精确。

    • 如果IsCompleted()方法的返回结果为true,表示本次查询已完成且查询精确,返回的查询结果是完整的。

    • 如果IsCompleted()方法的返回结果为 false,表示本次查询已完成但查询不精确,返回的查询结果是不完整的。您需要重复请求,获取完整的查询结果。关于查询不精确的更多信息,请参见查询不精确可能原因

参数说明

请求参数

名称

类型

是否必填

说明

project

String

Project名称:项目(Project)是日志服务的资源管理单元,是进行多用户隔离与访问控制的主要边界。更多信息,请参见管理Project

logstore

String

Logstore名称:Logstore是日志服务中日志数据的采集、存储和查询单元。更多信息,请参见管理Logstore

from

int

查询开始的时间点,使用Unix时间戳格式。

说明
  • 该时间为日志到达Logstore的时间。更多信息,请参见保留字段__tag__:__receive_time__

  • 开始时间点和结束时间点定义的时间区间。遵循左闭右开原则,即该时间区间包括区间开始时间点,但不包括区间结束时间点。如果开始时间点和结束时间点相同,则为无效区间,函数直接返回错误。

  • 如果您要确保不漏查数据,请将查询时间对齐到分钟级别。如果您在分析语句中设置了时间范围,则查询分析时以该时间范围为准。

  • 如果您需要在分析语句中指定时间范围并精确到秒时,可使用日期和时间函数或日期和时间函数转换下时间格式。例如:

    • * | SELECT * FROM log WHERE from_unixtime(__time__) > from_unixtime(1664186624) AND from_unixtime(__time__) < now()

    • * | SELECT * FROM log WHERE __time__ > to_unixtime(date_parse('2022-10-19 15:46:05', '%Y-%m-%d %H:%i:%s')) AND __time__ < to_unixtime(now())

to

int

查询结束的时间点,使用Unix时间戳格式。

说明
  • 该时间为日志到达Logstore的时间。更多信息,请参见保留字段__tag__:__receive_time__

  • 开始时间点和结束时间点定义的时间区间。遵循左闭右开原则,即该时间区间包括区间开始时间点,但不包括区间结束时间点。如果开始时间点和结束时间点相同,则为无效区间,函数直接返回错误。

  • 如果您要确保不漏查数据,请将查询时间对齐到分钟级别。如果您在分析语句中设置了时间范围,则查询分析时以该时间范围为准。

  • 如果您需要在分析语句中指定时间范围并精确到秒时,可使用日期和时间函数或日期和时间函数转换下时间格式。例如:

    • * | SELECT * FROM log WHERE from_unixtime(__time__) > from_unixtime(1664186624) AND from_unixtime(__time__) < now()

    • * | SELECT * FROM log WHERE __time__ > to_unixtime(date_parse('2022-10-19 15:46:05', '%Y-%m-%d %H:%i:%s')) AND __time__ < to_unixtime(now())

topic

String

日志主题。默认值为空字符串。更多信息,请参见日志主题(Topic)

query

String

查询语句或分析语句。更多信息,请参见查询与分析概述。 在query参数的分析语句中加上set session parallel_sql=true;,表示使用 SQL 独享版。例如* | set session parallel_sql=true; select count(*) as pv。常见查询与分析问题,请参见查询与分析日志的常见报错

说明

query参数中有分析语句(SQL 语句)时,该接口的line参数和offset参数无效。建议设置该接口的参数为0,需通过SQL语句的LIMIT语法实现翻页。更多信息,请参见分页显示查询分析结果

line

int

仅当query参数为查询语句时,该参数有效。表示请求返回的最大日志条数,最小值为 0,最大值为 100,默认值为 100。

offset

int

仅当query参数为查询语句时,该参数有效。表示查询开始行。默认值为 0。

reverse

boolean

用于指定返回结果是否按日志时间戳降序返回日志,精确到分钟级别。

  • true:按照日志时间戳降序返回日志。

  • false(默认值):按照日志时间戳升序返回日志。

重要
  • query参数为查询语句时,参数reverse有效,用于指定返回日志排序方式。

  • query参数为查询和分析语句时,参数reverse无效,由SQL分析语句中order by指定排序方式。

powerSql

boolean

是否使用SQL独享版。更多信息,请参见高性能完全精确查询与分析(SQL独享版)

  • true:使用SQL独享版。

  • false(默认值):使用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";
        // 设置日志服务的服务接入点。此处以杭州为例,其它地域请根据实际情况填写。
        String host = "cn-hangzhou.log.aliyuncs.com";
        // 输入Logstore名称。
        String logStore = "your-logstore-name";

        // 创建日志服务Client。
        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";
        // 设置日志服务的服务接入点。此处以杭州为例,其它地域请根据实际情况填写。
        String host = "cn-hangzhou.log.aliyuncs.com";
        // 输入Logstore名称。
        String logStore = "your-logstore-name";

        // 创建日志服务Client。
        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";
        // 设置日志服务的服务接入点。此处以杭州为例,其它地域请根据实际情况填写。
        String host = "cn-hangzhou.log.aliyuncs.com";
        // 输入Logstore名称。
        String logStore = "your-logstore-name";

        // 创建日志服务Client。
        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";
        // 设置日志服务的服务接入点。此处以杭州为例,其它地域请根据实际情况填写。
        String host = "cn-hangzhou.log.aliyuncs.com";
        // 输入Logstore名称。
        String logStore = "your-logstore-name";

        // 创建日志服务Client。
        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";
        // 设置日志服务的服务接入点。此处以杭州为例,其它地域请根据实际情况填写。
        String host = "cn-hangzhou.log.aliyuncs.com";
        // 输入Logstore名称。
        String logStore = "your-logstore-name";

        // 创建日志服务Client。
        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";
        // 设置日志服务的服务接入点。此处以杭州为例,其它地域请根据实际情况填写。
        String host = "cn-hangzhou.log.aliyuncs.com";
        // 输入Logstore名称。
        String logStore = "your-logstore-name";

        // 创建日志服务Client。
        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.-------------

相关文档