Loading...
Loading...
Query and troubleshoot logs in Alibaba Cloud Log Service (SLS) using query|analysis syntax and the Python SDK. Use for time-bounded log search, error investigation, and root-cause analysis workflows.
npx skill4agent add cinience/alicloud-skills alicloud-observability-sls-log-querypython3 -m venv .venv
. .venv/bin/activate
python -m pip install -U aliyun-log-python-sdkALIBABA_CLOUD_ACCESS_KEY_IDALIBABA_CLOUD_ACCESS_KEY_SECRETSLS_ENDPOINTcn-hangzhou.log.aliyuncs.comSLS_PROJECTSLS_LOGSTOREstatus:500查询语句|分析语句* | SELECT status, count(*) AS pv GROUP BY statusreferences/query-syntax.mdimport os
import time
from aliyun.log import LogClient, GetLogsRequest
client = LogClient(
os.environ["SLS_ENDPOINT"],
os.environ["ALIBABA_CLOUD_ACCESS_KEY_ID"],
os.environ["ALIBABA_CLOUD_ACCESS_KEY_SECRET"],
)
project = os.environ["SLS_PROJECT"]
logstore = os.environ["SLS_LOGSTORE"]
query = "status:500"
start_time = int(time.time()) - 15 * 60
end_time = int(time.time())
request = GetLogsRequest(project, logstore, start_time, end_time, query=query)
response = client.get_logs(request)
for log in response.get_logs():
print(log.contents)python skills/observability/sls/alicloud-observability-sls-log-query/scripts/query_logs.py \
--query "status:500" \
--last-minutes 15--project--logstore--endpoint--start--end--last-minutes--limitpython skills/observability/sls/alicloud-observability-sls-log-query/scripts/troubleshoot.py \
--group-field status \
--last-minutes 30 \
--limit 20--error-query--group-field--limitlimitreferences/query-syntax.mdreferences/python-sdk.mdreferences/templates.mdreferences/sources.md