Query raw logs
Use the pull_logs method to retrieve raw log data from specified cursors. To query or analyze logs, use the GetLogsV2 API instead.
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 Simple Log Service SDK for Python.
Parameters
def pull_logs(self, project_name, logstore_name, shard_id, cursor, count=None, end_cursor=None, compress=None, query=None):
Request parameters
|
Parameter |
Type |
Required |
Description |
|
project_name |
string |
Yes |
The name of the project. The project in Simple Log Service is used to isolate the resources of different users and control access to specific resources. See Manage projects. |
|
logstore_name |
string |
Yes |
The name of the logstore. The logstore in Simple Log Service is used to collect, store, and query logs. See Manage a logstore. |
|
shard_id |
int |
Yes |
The ID of the shard in the logstore. |
|
cursor |
string |
Yes |
The start cursor for reading data. For more information, see Query a cursor by time. |
|
count |
int |
No |
The number of logs returned. Default value: 1000. |
|
end_cursor |
string |
No |
The end cursor. For more information, see Query a cursor by time. |
|
compress |
bool |
No |
Whether to use compression. Default value: True. |
|
query |
string |
No |
Filter statement in Simple Log Service Processing Language (SPL) syntax. For more information, see SPL instructions and functions. |
Response parameters
For information about the response parameters, see PullLogs.
Sample code
from aliyun.log import LogClient
import os
# In this example, the AccessKey ID and AccessKey secret are obtained 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', '')
# Specify a Simple Log Service endpoint.
endpoint = "ap-southeast-1.log.aliyuncs.com"
# Create an instance of the LogClient class.
client = LogClient(endpoint, access_key_id, access_key_secret)
project_name = "slsproject-1"
logstore_name = "logstore-1"
shard_id = 0
cursor = "MTczMjcwMDMzMDU0MTIyNTM2MA=="
def main():
# Query raw logs.
res = client.pull_logs(project_name, logstore_name, shard_id, cursor)
res.log_print()
if __name__ == '__main__':
main()
Sample response
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
References
-
APIs for querying log data:
-
More sample code is available in the Alibaba Cloud Simple Log Service SDK for Python GitHub repository.