CLS Skill
Query and analyze Tencent Cloud CLS logs.
Setup
- Install clscli (Homebrew):
brew tap dbwang0130/clscli
brew install dbwang0130/clscli/clscli
- Get credentials and region list: https://cloud.tencent.com/document/api/614/56474
- Set environment variables (same as Tencent Cloud API common parameters):
export TENCENTCLOUD_SECRET_ID="your-secret-id"
export TENCENTCLOUD_SECRET_KEY="your-secret-key"
- Specify region via
--region (e.g. ap-guangzhou).
Usage
!IMPORTANT: If you do not know the log topic, list topics first.
List log topics
List topics in a region to determine which --region and topic ID to use for query/context.
clscli topics --region <region> [--topic-name name] [--logset-name name] [--logset-id id] [--limit 20] [--offset 0]
Examples: --output=json, --output=csv, -o topics.csv
| Option |
Required |
Description |
| --region |
yes |
CLS region, e.g. ap-guangzhou |
| --topic-name |
no |
Filter by topic name (fuzzy match) |
| --logset-name |
no |
Filter by logset name (fuzzy match) |
| --logset-id |
no |
Filter by logset ID |
| --limit |
no |
Page size, default 20, max 100 |
| --offset |
no |
Pagination offset, default 0 |
| --output, -o |
no |
Output: json, csv, or file path |
Output columns: Region, TopicId, TopicName, LogsetId, CreateTime, StorageType.
Get log by query
clscli query -q "[query condition] | [SQL statement]" --region <region> -t <TopicId> --last 1h
Examples:
- Time:
--last 1h, --last 30m; or --from/--to (Unix ms)
- Multiple topics:
--topics <id1>,<id2> or multiple -t <id>
- Auto pagination and cap:
--max 5000 (paginate until 5000 logs or ListOver)
- Output:
--output=json, --output=csv, -o result.json (write to file)
| Option |
Required |
Description |
| --region |
yes |
CLS region, e.g. ap-guangzhou |
| -q, --query |
yes |
Query condition or SQL, e.g. level:ERROR or * | select count(*) as cnt |
| -t, --topic |
one of -t/--topics |
Single log topic ID |
| --topics |
one of -t/--topics |
Comma-separated topic IDs, max 50 |
| --last |
one of --last/--from/--to |
Time range, e.g. 1h, 30m, 24h |
| --from, --to |
one of --last/--from/--to |
Start/end time (Unix ms) |
| --limit |
no |
Logs per request, default 100, max 1000 |
| --max |
no |
Max total logs; when non-zero, auto-paginate until reached or ListOver |
| --output, -o |
no |
Output: json, csv, or file path |
| --sort |
no |
Sort: asc or desc, default desc |
Query condition syntax
Two syntaxes are supported:
- CQL (CLS Query Language): CLS-specific query syntax for logs, easy to use, recommended.
- Lucene: Open-source Lucene syntax; not designed for log search, has more restrictions on special chars, case, wildcards; not recommended.
CQL syntax
| Syntax |
Description |
key:value |
Key-value search; logs where field (key) contains value, e.g. level:ERROR |
value |
Full-text search; logs containing value, e.g. ERROR |
AND |
Logical AND, case-insensitive, e.g. level:ERROR AND pid:1234 |
OR |
Logical OR, case-insensitive, e.g. level:ERROR OR level:WARNING, level:(ERROR OR WARNING) |
NOT |
Logical NOT, case-insensitive, e.g. level:ERROR NOT pid:1234, level:ERROR AND NOT pid:1234 |
() |
Grouping for precedence, e.g. level:(ERROR OR WARNING) AND pid:1234. Note: AND has higher precedence than OR when no parentheses. |
" " |
Phrase search; double-quoted string, words and order must match, e.g. name:"john Smith". No logical operators inside phrase. |
' ' |
Phrase search; single quotes, same as ""; use when phrase contains double quotes, e.g. body:'user_name:"bob"' |
* |
Wildcard; zero or more chars, e.g. host:www.test*.com. No prefix wildcard. |
>, >=, <, <=, = |
Range operators for numeric values, e.g. status>400, status:>=400 |
\ |
Escape; escaped char is literal. Escape space, :, (), >, =, <, ", ', * in values. |
key:* |
text: field exists (any value). long/double: field exists and is numeric, e.g. response_time:* |
key:"" |
text: field exists and is empty. long/double: value is not numeric or field missing, e.g. response_time:"" |
SQL statement syntax
| Syntax |
Description |
| SELECT |
Select from table; data from current log topic matching query condition |
| AS |
Alias for column (KEY) |
| GROUP BY |
With aggregate functions, group by one or more columns (KEY) |
| ORDER BY |
Sort result set by KEY |
| LIMIT |
Limit rows, default 100, max 1M |
| WHERE |
Filter raw data |
| HAVING |
Filter after GROUP BY, before ORDER BY; WHERE filters raw data |
| Nested subquery |
One SELECT inside another for multi-step analysis |
| SQL functions |
Richer analysis: IP geo, time format, string split/join, JSON extract, math, distinct count, etc. |
Describe log context
Retrieve log context around a given log.
clscli context <PkgId> <PkgLogId> --region <region> -t <TopicId>
Examples: --output=json, --output=csv, -o context.json (write to file)
| Option |
Required |
Type |
Description |
Example |
| --region |
yes |
String |
CLS region |
ap-guangzhou |
| -t, --topic |
yes |
String |
Log topic ID |
- |
| PkgId |
yes |
String |
Log package ID, i.e. SearchLog Results[].PkgId |
528C1318606EFEB8-1A7 |
| PkgLogId |
yes |
Integer |
Index within package, i.e. SearchLog Results[].PkgLogId |
65536 |
| --output, -o |
no |
- |
Output: json, csv, or file path |
- |
1---
2name: clscli
3description: Query and analyze Tencent Cloud CLS logs
4homepage: https://github.com/
5metadata:
6 source: https://skillmd.com/skills/modbender/clscli
7 requires:
8 bin:
9 - clscli
10 env:
11 - TENCENTCLOUD_SECRET_ID
12 - TENCENTCLOUD_SECRET_KEY
13---
14
15# CLS Skill
16
17Query and analyze Tencent Cloud CLS logs.
18
19## Setup
201. Install clscli (Homebrew):
21 ```bash
22 brew tap dbwang0130/clscli
23 brew install dbwang0130/clscli/clscli
24 ```
252. Get credentials and region list: https://cloud.tencent.com/document/api/614/56474
263. Set environment variables (same as Tencent Cloud API common parameters):
27 ```bash
28 export TENCENTCLOUD_SECRET_ID="your-secret-id"
29 export TENCENTCLOUD_SECRET_KEY="your-secret-key"
30 ```
314. Specify region via `--region` (e.g. ap-guangzhou).
32
33## Usage
34
35!IMPORTANT: If you do not know the log topic, list topics first.
36
37### List log topics
38List topics in a region to determine which `--region` and topic ID to use for query/context.
39
40```bash
41clscli topics --region <region> [--topic-name name] [--logset-name name] [--logset-id id] [--limit 20] [--offset 0]
42```
43Examples: `--output=json`, `--output=csv`, `-o topics.csv`
44
45| Option | Required | Description |
46|--------|----------|-------------|
47| --region | yes | CLS region, e.g. ap-guangzhou |
48| --topic-name | no | Filter by topic name (fuzzy match) |
49| --logset-name | no | Filter by logset name (fuzzy match) |
50| --logset-id | no | Filter by logset ID |
51| --limit | no | Page size, default 20, max 100 |
52| --offset | no | Pagination offset, default 0 |
53| --output, -o | no | Output: json, csv, or file path |
54
55Output columns: Region, TopicId, TopicName, LogsetId, CreateTime, StorageType.
56
57### Get log by query
58```bash
59clscli query -q "[query condition] | [SQL statement]" --region <region> -t <TopicId> --last 1h
60```
61Examples:
62- Time: `--last 1h`, `--last 30m`; or `--from`/`--to` (Unix ms)
63- Multiple topics: `--topics <id1>,<id2>` or multiple `-t <id>`
64- Auto pagination and cap: `--max 5000` (paginate until 5000 logs or ListOver)
65- Output: `--output=json`, `--output=csv`, `-o result.json` (write to file)
66
67| Option | Required | Description |
68|--------|----------|-------------|
69| --region | yes | CLS region, e.g. ap-guangzhou |
70| -q, --query | yes | Query condition or SQL, e.g. `level:ERROR` or `* \| select count(*) as cnt` |
71| -t, --topic | one of -t/--topics | Single log topic ID |
72| --topics | one of -t/--topics | Comma-separated topic IDs, max 50 |
73| --last | one of --last/--from/--to | Time range, e.g. 1h, 30m, 24h |
74| --from, --to | one of --last/--from/--to | Start/end time (Unix ms) |
75| --limit | no | Logs per request, default 100, max 1000 |
76| --max | no | Max total logs; when non-zero, auto-paginate until reached or ListOver |
77| --output, -o | no | Output: json, csv, or file path |
78| --sort | no | Sort: asc or desc, default desc |
79
80#### Query condition syntax
81
82Two syntaxes are supported:
83- **CQL** (CLS Query Language): CLS-specific query syntax for logs, easy to use, recommended.
84- **Lucene**: Open-source Lucene syntax; not designed for log search, has more restrictions on special chars, case, wildcards; not recommended.
85
86##### CQL syntax
87| Syntax | Description |
88|--------|-------------|
89| `key:value` | Key-value search; logs where field (key) contains value, e.g. `level:ERROR` |
90| `value` | Full-text search; logs containing value, e.g. `ERROR` |
91| `AND` | Logical AND, case-insensitive, e.g. `level:ERROR AND pid:1234` |
92| `OR` | Logical OR, case-insensitive, e.g. `level:ERROR OR level:WARNING`, `level:(ERROR OR WARNING)` |
93| `NOT` | Logical NOT, case-insensitive, e.g. `level:ERROR NOT pid:1234`, `level:ERROR AND NOT pid:1234` |
94| `()` | Grouping for precedence, e.g. `level:(ERROR OR WARNING) AND pid:1234`. **Note: AND has higher precedence than OR when no parentheses.** |
95| `" "` | Phrase search; double-quoted string, words and order must match, e.g. `name:"john Smith"`. No logical operators inside phrase. |
96| `' '` | Phrase search; single quotes, same as `""`; use when phrase contains double quotes, e.g. `body:'user_name:"bob"'` |
97| `*` | Wildcard; zero or more chars, e.g. `host:www.test*.com`. No prefix wildcard. |
98| `>`, `>=`, `<`, `<=`, `=` | Range operators for numeric values, e.g. `status>400`, `status:>=400` |
99| `\` | Escape; escaped char is literal. Escape space, `:`, `()`, `>`, `=`, `<`, `"`, `'`, `*` in values. |
100| `key:*` | text: field exists (any value). long/double: field exists and is numeric, e.g. `response_time:*` |
101| `key:""` | text: field exists and is empty. long/double: value is not numeric or field missing, e.g. `response_time:""` |
102
103#### SQL statement syntax
104| Syntax | Description |
105|--------|-------------|
106| SELECT | Select from table; data from current log topic matching query condition |
107| AS | Alias for column (KEY) |
108| GROUP BY | With aggregate functions, group by one or more columns (KEY) |
109| ORDER BY | Sort result set by KEY |
110| LIMIT | Limit rows, default 100, max 1M |
111| WHERE | Filter raw data |
112| HAVING | Filter after GROUP BY, before ORDER BY; WHERE filters raw data |
113| Nested subquery | One SELECT inside another for multi-step analysis |
114| SQL functions | Richer analysis: IP geo, time format, string split/join, JSON extract, math, distinct count, etc. |
115
116
117### Describe log context
118
119Retrieve log context around a given log.
120
121```bash
122clscli context <PkgId> <PkgLogId> --region <region> -t <TopicId>
123```
124Examples: `--output=json`, `--output=csv`, `-o context.json` (write to file)
125
126| Option | Required | Type | Description | Example |
127|--------|----------|------|-------------|---------|
128| --region | yes | String | CLS region | ap-guangzhou |
129| -t, --topic | yes | String | Log topic ID | - |
130| PkgId | yes | String | Log package ID, i.e. SearchLog Results[].PkgId | 528C1318606EFEB8-1A7 |
131| PkgLogId | yes | Integer | Index within package, i.e. SearchLog Results[].PkgLogId | 65536 |
132| --output, -o | no | - | Output: json, csv, or file path | - |