Query context logs, 查询上下文日志
Call GetContextLogs to retrieve log entries before and after a specified log entry.
Prerequisites
Simple Log Service is activated. For more information, see Activate Simple Log Service.
The Simple Log Service SDK for Python is initialized. For more information, see Initialize the SLS Python SDK.
Parameters
def get_context_logs(self, project, logstore, pack_id, pack_meta, back_lines, forward_lines):Request parameters
Parameter | Type | Required | Description |
project | String | Yes | Project name: The name of the project. A project is the basic resource management unit in Simple Log Service, used for resource isolation and access control. For more information, see Manage projects. |
logstore | String | Yes | Logstore name: The name of the logstore. A logstore is a unit in Simple Log Service for collecting, storing, and querying log data. For more information, see Manage a logstore. |
pack_id | String | Yes | The unique identifier of the log group that contains the anchor log entry. You can obtain this value from the response of the GetLogs operation. |
pack_meta | String | Yes | The unique context identifier of the anchor log entry within its log group. You can obtain this value from the response of the GetLogs operation. |
back_lines | int | Yes | The number of log entries to retrieve before the anchor log entry. The value must be in the range |
forward_lines | int | Yes | The number of log entries to retrieve after the anchor log entry. The value must be in the range |
Response parameters
For details about response parameters, see GetContextLogs.
Sample code
from aliyun.log import LogClient
import os
# Get the AccessKey ID and AccessKey Secret from environment variables.
access_key_id = os.environ.get('ALIBABA_CLOUD_ACCESS_KEY_ID', '')
access_key_secret = os.environ.get('ALIBABA_CLOUD_ACCESS_KEY_SECRET', '')
# The endpoint of Simple Log Service.
endpoint = "cn-hangzhou.log.aliyuncs.com"
# Create a LogClient instance.
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():
# Retrieve log entries before and after a specific log entry.
res = client.get_context_logs(project, logstore, pack_id, pack_meta, back_lines, forward_lines)
res.log_print()
if __name__ == '__main__':
main() Sample response
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 0References
The following API operations are also available for querying logs: