完成日志采集后,您可以调用GetLogs接口查询采集到的日志。本文介绍GetLogs接口示例。
前提条件
您已完成以下操作:
注意事项
本示例以华东1(杭州)的公网Endpoint为例,其公网Endpoint为
https://cn-hangzhou.log.aliyuncs.com
。如果您通过与Project同地域的其他阿里云产品访问日志服务,请使用内网Endpoint
https://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时间戳格式。 说明
|
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";
// 设置日志服务的服务接入点。此处以杭州为例,其它地域请根据实际情况填写。
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.-------------
相关文档
在调用API接口过程中,若服务端返回结果中包含错误信息,则表示调用API接口失败。您可以参考API错误码对照表查找对应的解决方法。更多信息,请参见API错误处理对照表。
日志服务除自研的SDK外,还支持公共的阿里云SDK,关于阿里云SDK的使用方式,请参见日志服务_SDK中心-阿里云OpenAPI开发者门户。
为满足越来越多的自动化日志服务配置需求,日志服务提供命令行工具CLI(Command Line Interface)。更多信息,请参见日志服务命令行工具CLI。
更多示例代码,请参见Aliyun Log Java SDK on GitHub。