调用GetContextLogs接口查询指定日志前(上文)后(下文)的若干条日志。
前提条件
您已完成以下操作:
参数说明
def get_context_logs(self, project, logstore, pack_id, pack_meta, back_lines, forward_lines):
请求参数
名称 | 类型 | 是否必填 | 说明 |
project | String | 是 | Project名称:项目(Project)是日志服务的资源管理单元,是进行多用户隔离与访问控制的主要边界。更多信息,请参见管理Project。 |
logstore | String | 是 | Logstore名称:Logstore是日志服务中日志数据的采集、存储和查询单元。更多信息,请参见管理Logstore。 |
pack_id | String | 是 | 起始日志所属的LogGroup的唯一身份标识。从GetLogs接口获取。 |
pack_meta | String | 是 | 起始日志在对应LogGroup内的唯一上下文结构标识。从GetLogs接口获取。 |
back_lines | int | 是 | 指定起始日志往后(下文)的日志条数,取值范围为 |
forward_lines | int | 是 | 指定起始日志往前(上文)的日志条数,取值范围为 |
返回参数
返回参数请参见:GetContextLogs - 查询上下文日志。
示例代码
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 = "proj-test-1"
logstore = "logstore-1"
pack_id = "85C89****52DC6-808"
pack_meta = "2|MTY1NTcw****MTY0MDk1Mg==|3|0"
back_lines = 1
forward_lines = 1
def main():
# 查询指定日志前(上文)后(下文)的若干条日志
res = client.get_context_logs(project, logstore, pack_id, pack_meta, back_lines, forward_lines)
res.log_print()
if __name__ == '__main__':
main()
示例返回结果
GetContextLogsResponse:
headers: {'Server': 'AliyunSLS', 'Content-Type': 'application/json', 'Content-Length': '1855', 'Connection': 'keep-alive', 'Access-Control-Allow-Origin': '*', 'Date': 'Fri, 15 Nov 2024 05:25:34 GMT', 'x-log-time': '1731648334', 'x-log-requestid': '6736DB4E4DFECE685754****'}
total_lines: 2
back_lines: 1
forward_lines: 0
progress: Complete
QueriedLog class:
QueriedLog:
time: 1731647496
source:
contents: {'__index_number__': '-1', '@timestamp': '2024-11-15T13:11:36+08:00', 'server_addr': '172.x.x.243', 'remote_addr': '192.x.x.1', 'scheme': 'http', 'method': 'GET', 'path': '/druid/css/bootstrap.min.css', 'request_length': '415', 'uri': '/druid/css/bootstrap.min.css', 'request_time': '0.036000', 'body_bytes_sent': '15532', 'bytes_sent': '15768', 'status': '200', 'upstream_time': '0.036', 'upstream_host': '127.0.0.1:8080', 'upstream_status': '200', 'host': 'www.example.com', 'http_referer': 'http://www.example.com/login.html', 'http_user_agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/131.0.0.0 Safari/537.36', 'request_body': '', '__pack_meta__': '1|MTczMTQwMj****zNTAwMjAyMg==|7|6', '__tag__:__hostname__': 'my-host', '__tag__:__path__': '/data/logs/access.log', '__tag__:__pack_id__': '****06F906394977-3', '__tag__:__receive_time__': '1731647498'}
QueriedLog:
time: 1731647498
source:
contents: {'__index_number__': '0', '@timestamp': '2024-11-15T13:11:38+08:00', 'server_addr': '172.x.x.243', 'remote_addr': '192.x.x.1', 'scheme': 'http', 'method': 'GET', 'path': '/monitor/server', 'request_length': '538', 'uri': '/monitor/server', 'request_time': '1.584000', 'body_bytes_sent': '2455', 'bytes_sent': '2687', 'status': '200', 'upstream_time': '1.585', 'upstream_host': '127.0.0.1:8080', 'upstream_status': '200', 'host': 'www.example.com', 'http_referer': 'http://www.example.com/index', 'http_user_agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/131.0.0.0 Safari/537.36', 'request_body': '', '__pack_meta__': '1|MTczMTQwMj****zNTAwMjAyMw==|1|0', '__tag__:__hostname__': 'my-host', '__tag__:__path__': '/data/logs/access.log', '__tag__:__pack_id__': '****06F906394977-4', '__tag__:__receive_time__': '1731647501'}
Process finished with exit code 0
相关文档
该文章对您有帮助吗?
- 本页导读 (1)
- 前提条件
- 参数说明
- 请求参数
- 返回参数
- 示例代码
- 示例返回结果
- 相关文档