使用pull_logs方法获取指定游标(Cursor)位置的日志数据。此接口获取的是原始日志,如要查询或分析日志,请使用GetLogsV2接口。
前提条件
您已完成以下操作:
开通日志服务。
初始化日志服务Python SDK。
参数说明
def pull_logs(self, project_name, logstore_name, shard_id, cursor, count=None, end_cursor=None, compress=None, query=None):
请求参数
名称 | 类型 | 是否必填 | 说明 |
project_name | String | 是 | Project名称:项目(Project)是日志服务的资源管理单元,是进行多用户隔离与访问控制的主要边界。更多信息,请参见管理Project。 |
logstore_name | String | 是 | Logstore名称:Logstore是日志服务中日志数据的采集、存储和查询单元。更多信息,请参见管理Logstore。 |
shard_id | int | 是 | 日志库的分区ID,更多信息,请参见分区(Shard)。 |
cursor | String | 是 | 起始游标值,表示读取数据的起始位置。获取方式请参见通过时间查询Cursor。 |
count | int | 否 | 返回的数目,默认值为 1000。 |
end_cursor | String | 否 | 结束游标。获取方式请参见通过时间查询Cursor。 |
compress | bool | 否 | 是否使用压缩,默认为True。 |
query | String | 否 | 过滤语句,SPL语法,请参见SPL指令 |
返回参数
返回参数请参见:PullLogs - 拉取日志。
示例代码
from aliyun.log import LogClient
import os
# 本示例从环境变量中获取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"
# 实例化LogClient类
client = LogClient(endpoint, access_key_id, access_key_secret)
project_name = "slsproject-1"
logstore_name = "logstore-1"
shard_id = 0
cursor = "MTczMjcwMDMzMDU0MTIyNTM2MA=="
def main():
# 查询原始日志
res = client.pull_logs(project_name, logstore_name, shard_id, cursor)
res.log_print()
if __name__ == '__main__':
main()
示例返回结果
PullLogResponse
next_cursor MTczMjczMjg3MTkzNTQwOTc5Mw==
log_count 1
headers: {'Server': 'AliyunSLS', 'Content-Type': 'application/x-protobuf', 'Content-Length': '528', 'Connection': 'keep-alive', 'Access-Control-Allow-Origin': '*', 'Date': 'Tue, 03 Dec 2024 03:44:36 GMT', 'x-log-cursor-time': '1732775514', 'x-log-end-of-cursor': '0', 'x-log-read-last-cursor': '1732732871935409792', 'x-log-time': '1733197476', 'x-log-bodyrawsize': '2019', 'x-log-compresstype': 'gzip', 'x-log-count': '1', 'x-log-cursor': 'MTczMjczMjg3MTkzNTQwOTc5Mw==', 'x-log-requestid': '674E7EA4A06865DB24F5F358'}
detail: [{'topic': '', 'source': '172.17.0.2', 'logs': [{'@lh_time': 1732775511, '@time_ns': 0, '@timestamp': '2024-11-28T06:30:39+00:00', 'body_bytes_sent': '615', 'bytes_sent': '853', 'host': 'www.example.com', 'http_referer': '', 'http_user_agent': 'curl/7.74.0', 'path': '/', 'remote_addr': '127.0.0.1', 'request_body': '', 'request_length': '78', 'request_method': 'GET', 'request_time': '0.000000', 'scheme': 'http', 'server_addr': '127.0.0.1', 'status': '200', 'upstream_host': '', 'upstream_status': '', 'upstream_time': '', 'uri': '/index.html'}, {'@lh_time': 1732775511, '@time_ns': 0, '@timestamp': '2024-11-28T06:30:42+00:00', 'body_bytes_sent': '615', 'bytes_sent': '853', 'host': 'www.example.com', 'http_referer': '', 'http_user_agent': 'curl/7.74.0', 'path': '/', 'remote_addr': '127.0.0.1', 'request_body': '', 'request_length': '78', 'request_method': 'GET', 'request_time': '0.000000', 'scheme': 'http', 'server_addr': '127.0.0.1', 'status': '200', 'upstream_host': '', 'upstream_status': '', 'upstream_time': '', 'uri': '/index.html'}, {'@lh_time': 1732775511, '@time_ns': 0, '@timestamp': '2024-11-28T06:30:42+00:00', 'body_bytes_sent': '615', 'bytes_sent': '853', 'host': 'www.example.com', 'http_referer': '', 'http_user_agent': 'curl/7.74.0', 'path': '/', 'remote_addr': '127.0.0.1', 'request_body': '', 'request_length': '78', 'request_method': 'GET', 'request_time': '0.000000', 'scheme': 'http', 'server_addr': '127.0.0.1', 'status': '200', 'upstream_host': '', 'upstream_status': '', 'upstream_time': '', 'uri': '/index.html'}, {'@lh_time': 1732775511, '@time_ns': 0, '@timestamp': '2024-11-28T06:30:43+00:00', 'body_bytes_sent': '615', 'bytes_sent': '853', 'host': 'www.example.com', 'http_referer': '', 'http_user_agent': 'curl/7.74.0', 'path': '/', 'remote_addr': '127.0.0.1', 'request_body': '', 'request_length': '78', 'request_method': 'GET', 'request_time': '0.000000', 'scheme': 'http', 'server_addr': '127.0.0.1', 'status': '200', 'upstream_host': '', 'upstream_status': '', 'upstream_time': '', 'uri': '/index.html'}], 'tags': {'__hostname__': '0af27e1dad23', '__path__': '/data/logs/www_json.log', '__user_defined_id__': 'pts-docker-group', '_image_name_': 'nginx:latest', '_container_name_': 'nginx', '_container_ip_': '172.17.0.3', '__pack_id__': '3BBBF582EA4F7BDB-0', '__receive_time__': '1732775514'}}]
Process finished with exit code 0
相关文档
查询日志数据的API请参见:
GetCursor - 通过时间查询Cursor
GetCursorTime - 通过Cursor查询服务器端时间
GetLogsV2 - 查询Logstore中的日志数据
GetContextLogs - 查询上下文日志
GetHistograms - 查询日志分布情况
更多示例代码,请参见Aliyun Log Python SDK on GitHub。