/install aliyun-sls-query
阿里云日志查询技能 (aliyun-sls-get-log)\r
\r
技能描述\r
\r 调用阿里云日志服务(SLS)CLI命令查询指定Project和Logstore下的日志数据,支持基础日志查询和SQL分析查询,适用于获取指定时间范围内、指定数量的日志数据场景。\r \r
前置依赖与环境准备\r
\r
系统与软件要求\r
\r | 依赖项 | 要求 |\r | -------- | ------------------------------------------------------------------------------------------- |\r | 操作系统 | Windows、Linux、macOS |\r | Python版本 | Python 2.7+ 或 Python 3.7+(推荐Python 3.7及以上) |\r | 依赖组件 | docopt、aliyun-log-python-sdk、jmespath、aliyun-python-sdk-core、aliyun-python-sdk-sts、requests |\r \r
Python环境配置\r
\r
Linux/macOS\r
\r
- 创建并编辑环境变量配置文件\r
\r
touch ~/.bash_profile\r vim ~/.bash_profile\r ```\r - 添加Python安装路径(替换为实际路径)\r
\r
export PATH=$PATH:/usr/local/python3/bin\r ```\r - 使配置生效\r
\r
source ~/.bash_profile\r ```\r \r #### Windows\r - 右键单击「此电脑」→「属性」→「高级系统设置」→「环境变量」\r
- 在「用户变量」的「Path」中添加Python安装路径下的bin目录,例如:
D:\dev\python\python37\bin\r - 重启命令行或IDE使配置生效\r
\r
CLI安装\r
\r通用安装命令(推荐)\r
\rpip3 install aliyun-log-python-sdk aliyun-log-cli -U --no-cache\r ```\r \r #### 各系统验证安装\r \r ```bash\r aliyunlog --version\r # 输出示例:aliyun-log-cli 0.2.9\r ```\r \r ### CLI升级与卸载\r
- 升级到最新版本\r
\r
pip3 install aliyun-log-python-sdk aliyun-log-cli -U --no-cache\r ```\r - 卸载CLI\r
\r
pip3 uninstall aliyun-log-cli\r ```\r \r ## CLI配置指南\r \r ### 凭证优先级(从高到低)\r
- 命令行直接传入的全局参数(--access-id、--access-key等)\r
- --profile参数指定的阿里云CLI配置文件凭证\r
- ALIYUN_LOG_CLI系统环境变量\r
- 日志服务CLI配置文件(~/.aliyunlogcli)\r
- 阿里云CLI系统环境变量(ALIBABACLOUD、ALICLOUD)\r
- 阿里云CLI配置文件(~/.aliyun/config.json)\r
\r
配置默认账号\r
- 执行配置命令\r
\r
aliyunlog configure "你的AccessKey ID" "你的AccessKey Secret" "cn-hangzhou.log.aliyuncs.com"\r ```\r - 验证配置\r 配置文件会自动生成在以下路径:\r
- Linux/macOS:
~/.aliyunlogcli\r - Windows:
C:\Users\你的用户名\.aliyunlogcli\r 文件内容示例:\r \r[main]\r access-id = LTAI******pLMZ\r access-key = XjAsP******eRqax\r region-endpoint = cn-hangzhou.log.aliyuncs.com\r sts-token =\r ```\r \r ### 配置多账号\r
- 执行配置命令(指定配置名称)\r
\r
aliyunlog configure "你的AccessKey ID" "你的AccessKey Secret" "cn-shanghai.log.aliyuncs.com" "test"\r ```\r - 使用指定账号执行命令\r
\r
aliyunlog log get_log ... --client-name=test\r ```\r \r ### 全局输出格式配置\r
- 全局配置JSON格式化输出\r
\r
aliyunlog configure --format-output=json\r ```\r - 全局配置不转义非英文字符(解决中文乱码)\r
\r
aliyunlog configure --format-output=no_escape\r ```\r - 组合配置(推荐)\r
\r
aliyunlog configure --format-output=json,no_escape\r ```\r \r ### 特殊字符转义\r \r 当查询语句包含`$`、`` ` ``、`\`、`!`等特殊字符时,需使用以下两种方式之一转义:\r
- 反斜线转义\r
\r
--query="event_name:\$_enter"\r ```\r - 单引号强制引用(推荐)\r
\r
--query='event_name:$_enter'\r ```\r \r ## 触发词\r
- 阿里云日志查询\r
- SLS日志查询\r
- aliyun log get\r
- 查询阿里云日志\r
- SLS日志分析\r
\r
参数定义\r
\r业务参数\r
\r | 参数名 | 类型 | 是否必选 | 默认值 | 描述 |\r | --------- | ------- | ---- | ----- | ----------------------------------------------------------------------------------------------------- |\r | project | string | 是 | - | 阿里云SLS Project名称 |\r | logstore | string | 是 | - | 阿里云SLS Logstore名称 |\r | from_time | string | 是 | - | 查询开始时间,支持两种格式:\x3Cbr>1. Unix时间戳(秒级)\x3Cbr>2.%Y-%m-%d %H:%M:%S\x3Ctime_zone>格式,如2026-05-14 18:00:00+8:00|\r | to_time | string | 是 | - | 查询结束时间,格式同from_time|\r | topic | string | 否 | "" | 日志主题,默认空字符串 |\r | query | string | 否 | "*" | 查询语句或SQL分析语句,默认查询所有日志 |\r | reverse | boolean | 否 | false | 仅基础查询有效,是否按时间戳降序返回日志\x3Cbr>true:降序\x3Cbr>false:升序(默认) |\r | offset | integer | 否 | 0 | 仅基础查询有效,查询开始行偏移量 |\r | size | integer | 否 | 100 | 仅基础查询有效,返回最大日志条数,范围0-100 |\r | power_sql | boolean | 否 | false | 是否使用SQL独享版\x3Cbr>true:使用独享版\x3Cbr>false:使用普通版(默认) |\r \r全局参数(可选)\r
\r | 参数名 | 类型 | 是否必选 | 示例值 | 描述 |\r | --------------- | ------ | ---- | ---------------------------- | --------------------------- |\r | access-id | string | 否 | LTAI**************** | 阿里云AccessKey ID,优先级高于配置文件 |\r | access-key | string | 否 | yourAccessKeySecret | 阿里云AccessKey Secret |\r | sts-token | string | 否 | - | STS临时访问令牌 |\r | region-endpoint | string | 否 | cn-hangzhou.log.aliyuncs.com | SLS服务入口域名 |\r | client-name | string | 否 | test | 使用指定的多账号配置名称 |\r | profile | string | 否 | akProfile | 使用阿里云CLI配置文件中的凭证 |\r | format-output | string | 否 | json,no_escape | 输出格式,支持组合:json、no_escape |\r | jmes-filter | string | 否 | "data[*].status" | JMES语法表达式,过滤返回结果 |\r | decode-output | string | 否 | utf8 | 二进制日志解码方式,支持utf8、latin1、gbk |\r \r命令模板\r
\raliyunlog log get_log \\r --project="{{project}}" \\r --logstore="{{logstore}}" \\r --from_time="{{from_time}}" \\r --to_time="{{to_time}}" \\r {{#if topic}}--topic="{{topic}}"{{/if}} \\r {{#if query}}--query="{{query}}"{{/if}} \\r {{#if reverse}}--reverse={{reverse}}{{/if}} \\r {{#if offset}}--offset={{offset}}{{/if}} \\r {{#if size}}--size={{size}}{{/if}} \\r {{#if power_sql}}--power_sql={{power_sql}}{{/if}} \\r {{#if access-id}}--access-id="{{access-id}}"{{/if}} \\r {{#if access-key}}--access-key="{{access-key}}"{{/if}} \\r {{#if sts-token}}--sts-token="{{sts-token}}"{{/if}} \\r {{#if region-endpoint}}--region-endpoint="{{region-endpoint}}"{{/if}} \\r {{#if client-name}}--client-name="{{client-name}}"{{/if}} \\r {{#if profile}}--profile="{{profile}}"{{/if}} \\r {{#if jmes-filter}}--jmes-filter="{{jmes-filter}}"{{/if}} \\r {{#if decode-output}}--decode-output="{{decode-output}}"{{/if}} \\r --format-output={{format_output|default("json,no_escape")}}\r ```\r \r ## 使用示例\r \r ### 示例1:基础日志查询(获取最近100条INFO级别日志)\r \r ```\r 用户输入:查询阿里云日志 project=aliyun-test-project logstore=logstore-a from_time="2026-05-14 18:00:00+8:00" to_time="2026-05-14 18:30:00+8:00" query="level:INFO" reverse=true\r ```\r \r 生成命令:\r \r ```bash\r aliyunlog log get_log \\r --project="aliyun-test-project" \\r --logstore="logstore-a" \\r --from_time="2026-05-14 18:00:00+8:00" \\r --to_time="2026-05-14 18:30:00+8:00" \\r --query="level:INFO" \\r --reverse=true \\r --format-output=json,no_escape\r ```\r \r ### 示例2:SQL分析查询(统计不同状态码的请求数)\r \r ```\r 用户输入:阿里云日志分析 project=aliyun-test-project logstore=logstore-a from_time="2026-05-14 00:00:00+8:00" to_time="2026-05-14 18:30:00+8:00" query="* | select status,COUNT(*) as pv group by status order by pv desc limit 20"\r ```\r \r 生成命令:\r \r ```bash\r aliyunlog log get_log \\r --project="aliyun-test-project" \\r --logstore="logstore-a" \\r --from_time="2026-05-14 00:00:00+8:00" \\r --to_time="2026-05-14 18:30:00+8:00" \\r --query="* | select status,COUNT(*) as pv group by status order by pv desc limit 20" \\r --format-output=json,no_escape\r ```\r \r ### 示例3:使用全局参数指定临时凭证\r \r ```\r 用户输入:SLS临时查询 project=aliyun-test-project logstore=logstore-a from_time="2026-05-14 12:00:00+8:00" to_time="2026-05-14 18:30:00+8:00" access-id="LTAI******" access-key="XjAsP******" region-endpoint="cn-beijing.log.aliyuncs.com"\r ```\r \r 生成命令:\r \r ```bash\r aliyunlog log get_log \\r --project="aliyun-test-project" \\r --logstore="logstore-a" \\r --from_time="2026-05-14 12:00:00+8:00" \\r --to_time="2026-05-14 18:30:00+8:00" \\r --access-id="LTAI******" \\r --access-key="XjAsP******" \\r --region-endpoint="cn-beijing.log.aliyuncs.com" \\r --format-output=json,no_escape\r ```\r \r ### 示例4:使用JMES过滤结果(只返回状态码字段)\r \r ```\r 用户输入:查询阿里云日志 project=aliyun-test-project logstore=logstore-a from_time="2026-05-14 18:00:00+8:00" to_time="2026-05-14 18:30:00+8:00" jmes-filter="data[*].status"\r ```\r \r 生成命令:\r \r ```bash\r aliyunlog log get_log \\r --project="aliyun-test-project" \\r --logstore="logstore-a" \\r --from_time="2026-05-14 18:00:00+8:00" \\r --to_time="2026-05-14 18:30:00+8:00" \\r --jmes-filter="data[*].status" \\r --format-output=json,no_escape\r ```\r \r ## 重要注意事项\r
- SQL分析查询特殊规则:当
query参数包含SQL分析语句时,reverse、offset、size参数无效,必须通过SQL语句的ORDER BY指定排序,通过LIMIT语法实现分页\r - 时间格式要求:必须指定时区,中国区建议使用
+8:00时区,如2026-05-14 18:30:00+8:00\r - 返回条数限制:基础查询单次最多返回100条日志,如需更多数据请使用分页查询\r
- SQL独享版:除了通过
power_sql=true参数开启外,也可以在query语句中添加set session parallel_sql=true;\r - 凭证安全:避免在命令行明文输入AccessKey,优先使用配置文件或环境变量方式\r
- 特殊字符处理:查询语句包含特殊字符时,必须使用单引号引用或反斜线转义\r
- 多账号使用:跨账号操作时,使用
--client-name指定配置名称,或通过--profile使用阿里云CLI凭证\r \r返回结果说明\r
\r 返回结果为JSON格式,主要包含两部分:\r
data:查询结果数据数组,包含日志字段和分析结果\rmeta:查询元信息,包括执行状态、处理行数、CPU耗时、是否准确等\r 示例返回结果:\r \r{\r "data": [\r {\r "__source__": "192.168.1.100",\r "__time__": "1747297200",\r "pv": "1256",\r "status": "200"\r },\r {\r "__source__": "192.168.1.101",\r "__time__": "1747297200",\r "pv": "42",\r "status": "500"\r }\r ],\r "meta": {\r "count": 2,\r "isAccurate": true,\r "progress": "Complete",\r "processedRows": 1298,\r "cpuSec": 0.032,\r "elapsedMillisecond": 15\r }\r }\r ```\r \r ## 错误处理与常见问题\r \r | 错误现象 | 可能原因 | 解决方法 |\r | --------------------------------------------------- | ------------------------- | ------------------------------------ |\r | 命令未找到:aliyunlog | Python环境变量未配置或CLI未安装 | 检查Python环境变量,重新执行安装命令 |\r | 权限不足:Unauthorized | AccessKey错误或无SLS操作权限 | 验证AccessKey有效性,为RAM用户授予SLS相关权限 |\r | 服务不可达:Connection refused | Endpoint错误或网络不通 | 检查region-endpoint是否正确,确认网络可访问SLS服务入口 |\r | 时间格式错误 | 未指定时区或格式不正确 | 使用`%Y-%m-%d %H:%M:%S+8:00`格式,确保时区正确 |\r | SQL语法错误 | 分析语句语法不正确 | 检查SQL语句,参考SLS查询分析语法文档 |\r | 中文显示乱码 | 输出未配置no_escape | 添加`--format-output=json,no_escape`参数 |\r | 参数无效:offset/size | 使用了SQL分析语句但仍传入offset/size | 移除offset/size参数,使用SQL的LIMIT语法分页 |\r | 需要我帮你补充**自动分页查询**和**批量导出日志**的高级用法示例,让这个技能支持更多实际场景吗? | | |\r
- Make sure OpenClaw is installed (local or Docker)
- Run the install command in chat:
/install aliyun-sls-query - After installation, invoke the skill by name or use
/aliyun-sls-query - Provide required inputs per the skill's parameter spec and get structured output
What is 阿里云SLS日志查询?
调用阿里云日志服务CLI查询指定Project和Logstore的日志,支持时间范围筛选及SQL分析查询,返回结构化日志数据。 It is an AI Agent Skill for Claude Code / OpenClaw, with 46 downloads so far.
How do I install 阿里云SLS日志查询?
Run "/install aliyun-sls-query" in the OpenClaw or Claude Code chat to install it in one step — no extra setup required.
Is 阿里云SLS日志查询 free?
Yes, 阿里云SLS日志查询 is completely free, licensed under MIT-0. You can download, install and use it at no cost.
Which platforms does 阿里云SLS日志查询 support?
阿里云SLS日志查询 is cross-platform and runs anywhere OpenClaw / Claude Code is available (cross-platform).
Who created 阿里云SLS日志查询?
It is built and maintained by yunr-tg (@yunr-tg); the current version is v1.0.0.