调用GetLogsV2接口查询指定Project下某个Logstore中的原始日志数据,返回结果显示某时间区间中的原始日志(返回结果压缩后传输)。
前提条件
您已完成以下操作:
参数说明
def get_log_all_v2(self, project, logstore, from_time, to_time, topic=None, query=None, reverse=False, offset=0, power_sql=False, scan=False, forward=True):
请求参数
名称 | 类型 | 是否必填 | 说明 |
project | String | 是 | Project名称:项目(Project)是日志服务的资源管理单元,是进行多用户隔离与访问控制的主要边界。更多信息,请参见管理Project。 |
logstore | String | 是 | Logstore名称:Logstore是日志服务中日志数据的采集、存储和查询单元。更多信息,请参见管理Logstore。 |
from_time | int | 是 | 查询开始的时间点,使用Unix时间戳格式。
|
to_time | 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 | bool | 否 | 用于指定返回结果是否按日志时间戳降序返回日志,精确到分钟级别。
|
power_sql | bool | 否 | 是否使用SQL独享版。更多信息,请参见开启SQL独享版。
除通过powerSql参数配置SQL独享版外,您还可以使用query参数。 |
scan | bool | 否 | 如果scan设置为True,则查询将使用扫描模式。 |
forward | bool | 否 | 仅对于扫描查询,如果forward设置为True,查询将获取下一页,否则获取上一页。 |
返回参数
返回参数请参见:GetLogsV2 - 查询Logstore中的日志数据。
示例代码
from aliyun.log import LogClient, GetLogsRequest
import os
from datetime import datetime, timedelta
# 本示例从环境变量中获取AccessKey ID和AccessKey Secret
access_key_id = os.environ.get('ALIBABA_CLOUD_ACCESS_KEY_ID', '')
access_key_secret = os.environ.get('ALIBABA_CLOUD_ACCESS_KEY_SECRET', '')
# 日志服务的服务接入点
endpoint = "cn-hangzhou.log.aliyuncs.com"
client = LogClient(endpoint, access_key_id, access_key_secret)
project = "proj-test-1"
logstore = "logstore-1"
from_time = (datetime.now() - timedelta(minutes=1)).timestamp()
to_time = datetime.now().timestamp()
def main():
# 查询Logstore中的日志数据,此接口返回一个generator
res = client.get_log_all_v2(project, logstore, from_time, to_time)
for i in res:
print(i.log_print())
if __name__ == '__main__':
main()
示例返回结果
GetLogsResponse:
headers: {'Server': 'AliyunSLS', 'Content-Type': 'application/json', 'Content-Length': '849', 'Connection': 'keep-alive', 'Access-Control-Allow-Origin': '*', 'Date': 'Tue, 15 Oct 2024 08:53:29 GMT', 'x-log-time': '1728982409', 'x-log-bodyrawsize': '1633', 'x-log-compresstype': 'deflate', 'x-log-requestid': '670E2D89****2DC5A1F'}
count: 1
progress: Complete
meta: {'count': 1, 'progress': 'Complete', 'processedRows': 1, 'elapsedMillisecond': 1, 'hasSQL': False, 'whereQuery': '', 'aggQuery': '', 'cpuSec': 0.0, 'cpuCores': 0, 'mode': 0, 'scanBytes': 0, 'phraseQueryInfo': None, 'limited': None, 'processedBytes': 0, 'telementryType': 'logging', 'powerSql': False, 'insertedSQL': None, 'keys': ['content', '__topic__', '__source__', '__tag__:__hostname__', '__tag__:__path__', '__tag__:__pack_id__', '__tag__:__receive_time__'], 'marker': None, 'isAccurate': True, 'columnTypes': None, 'highlights': None, 'terms': [{'key': '', 'term': '*'}]}
QueriedLog class:
QueriedLog:
time: 1728973611
source: 172.x.x.28
contents: {'content': '{"@timestamp":"2024-10-15T14:26:51+08:00","server_addr":"172.x.x.28","remote_addr":"192.x.x.1","scheme":"http","request_method":"POST","request_uri": "/admin-ajax.php","request_length": "1178","uri": "/admin-ajax.php", "request_time":0.030,"body_bytes_sent":112,"bytes_sent":669,"status":"200","upstream_time":"0.030","upstream_host":"unix:/dev/shm/php-cgi.sock","upstream_status":"200","host":"www.example.com","http_referer":"http://www.example.com/post.php?post=1&action=edit","http_user_agent":"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/129.0.X.X Safari/537.36","request_body":"","content_type":"application/json; charset=UTF-8"}', '__topic__': '', '__tag__:__hostname__': '****', '__tag__:__path__': '/data/wwwlogs/json/www_json.log', '__tag__:__pack_id__': 'E56DA***44D05-4', '__tag__:__receive_time__': '1728973614'}
None
GetLogsResponse:
headers: {'Server': 'AliyunSLS', 'Content-Type': 'application/json', 'Content-Length': '173', 'Connection': 'keep-alive', 'Access-Control-Allow-Origin': '*', 'Date': 'Tue, 15 Oct 2024 08:53:29 GMT', 'x-log-time': '1728982409', 'x-log-bodyrawsize': '260', 'x-log-compresstype': 'deflate', 'x-log-requestid': '670E2D89C74B0D57BB98F9A5'}
count: 0
progress: Complete
meta: {'count': 0, 'progress': 'Complete', 'processedRows': 0, 'elapsedMillisecond': 0, 'hasSQL': False, 'whereQuery': '', 'aggQuery': '', 'cpuSec': 0.0, 'cpuCores': 0, 'mode': 0, 'scanBytes': 0, 'phraseQueryInfo': None, 'limited': None, 'processedBytes': 0, 'telementryType': 'logging', 'powerSql': False, 'insertedSQL': None, 'keys': None, 'marker': None, 'isAccurate': True, 'columnTypes': None, 'highlights': None, 'terms': [{'key': '', 'term': '*'}]}
QueriedLog class:
None
Process finished with exit code 0
相关文档
- 本页导读 (1)
- 前提条件
- 参数说明
- 请求参数
- 返回参数
- 示例代码
- 示例返回结果
- 相关文档