
# 查询和分析网站日志
通过本实践，您将学习到如何使用云日志服务（LTS）查询与分析网站日志。以网站日志为例，帮助您快速掌握LTS的使用方法。
#### 前提条件
已接入网站访问日志，请参见[云主机ECS文本日志接入LTS](https://support.huaweicloud.com/usermanual-lts/lts_04_1031.html)。
#### 步骤一：创建日志索引
索引是一种存储结构，用于对日志数据进行查询。通过配置索引后，可以对日志进行查询和分析操作。
1. 登录[云日志服务控制台](https://console.huaweicloud.com/lts/?#/cts/manager/groups)，进入"日志管理"页面。
2. 单击目标日志组或日志流名称，进入日志流详情页面。
3. 在"日志搜索"页面，单击快速分析旁边的![](https://support.huaweicloud.com/bestpractice-lts/zh-cn_image_0000002658981261.png)进入"索引配置"页面。
4. 在索引配置页面中，默认开启"全文索引"。
5. 配置字段索引，完成后单击"确定"。 
   可以手动逐条添加字段索引，也可以单击自动生成索引，云日志服务LTS会根据近15分钟的第一条日志内容或常见内置保留字段（例如hostIP、hostName、pathFile）自动生成字段索引。
   ![](https://support.huaweicloud.com/bestpractice-lts/public_sys-resources/note_3.0-zh-cn.png)
   - 全文索引属性和字段索引属性必须至少启用一种。
   
   - 云日志服务默认已为部分内置保留字段创建字段索引，请参见[内置保留字段](https://support.huaweicloud.com/usermanual-lts/lts_07_0078.html#section6)。
   
   - 索引配置修改后，对新写入的日志数据生效，历史日志数据不会生效。
    
   
   
 
#### 步骤二：查询和分析日志
支持管道符特性在一个语句中同时进行搜索和分析。其语法结构主要由三部分构成：针对非结构化数据和半结构化数据的搜索语句、管道符"\|"和针对结构化数据的查询的分析语句。语法示例结构：\* and msg:"hello world" \| SELECT avg(value)。
对日志查询和分析的详细步骤，请参见[搜索与分析日志](https://support.huaweicloud.com/usermanual-lts/lts_07_0076.html)。
**查询语句**
- 查询包含Chrome的日志。
  ```
  Chrome
  ```
  
- 查询请求时间大于60秒的日志。
  ```
  request_time > 60
  ```
  
- 查询请求时间在60秒\~120秒之间的日志。
  ```
  request_time in [60 120]
  ```
  
- 查询GET请求成功（状态码为200\~299）的日志。
  ```
  request_method : GET and status in [200 299]
  ```
  
- 查询request_uri字段值为/request/path-2/file-2的日志。
  ```
  request_uri:/request/path-2/file-2
  ```
  
**分析日志**
- 统计网站访问浏览量PV。 使用[count函数](https://support.huaweicloud.com/usermanual-lts/lts_07_0204.html#section6)统计网站访问浏览量PV。
  ```
  * | SELECT count(*) as PV
  ```
  查询结果显示PV值为8764。
  
- 根据每分钟的时间维度，统计网站访问浏览量PV。 使用[date_trunc函数](https://support.huaweicloud.com/usermanual-lts/lts_07_0206.html#section21)将时间对齐到每分钟，并依据时间进行分组，然后使用[count函数](https://support.huaweicloud.com/usermanual-lts/lts_07_0204.html#section6)计算每分钟的访问浏览量PV。并根据时间进行排序。
  ```
  * | SELECT count(*) as PV, date_trunc('minute', __time) as time GROUP BY time ORDER BY time
  ```
  
- 根据每5分钟的时间维度，统计每个请求方法的请求次数。 使用__time - __time%300000将时间对齐到5分钟并根据时间进行分组，然后使用[count函数](https://support.huaweicloud.com/usermanual-lts/lts_07_0204.html#section6)计算每5分钟的请求次数并根据时间进行排序。
  ```
  * | SELECT request_method, count(*) as count, __time - __time %300000 as time GROUP BY time, request_method ORDER BY time
  ```
  
- 环比上周的网站访问浏览量PV。 使用[count函数](https://support.huaweicloud.com/usermanual-lts/lts_07_0204.html#section6)计算总浏览量数，然后使用[ts_compare函数](https://support.huaweicloud.com/usermanual-lts/lts_07_0208.html#section2)计算本周与上周的环比。
  ```
  * | SELECT diff[1] as this_week, diff[2] as last_week, time FROM (SELECT ts_compare(pv, 604800) as diff, time FROM (SELECT COUNT(*) as pv, date_trunc('week', __time) as time FROM log GROUP BY time ORDER BY time) GROUP BY time)
  ```
  
- 统计客户端地址分布情况。 使用[ip_to_province函数](https://support.huaweicloud.com/usermanual-lts/lts_07_0213.html#section15)获取IP地址对应的省份，并依据省份进行分组，然后使用[count函数](https://support.huaweicloud.com/usermanual-lts/lts_07_0204.html#section6)计算每个地址出现的次数，并根据次数进行排序。
  ```
  * | SELECT count(*) as count, ip_to_province(client_ip) as address GROUP BY address ORDER BY count DESC
  ```
  
- 统计访问前5的请求路径。 根据请求路径进行分组，然后使用[count函数](https://support.huaweicloud.com/usermanual-lts/lts_07_0204.html#section6)计算每个路径的访问次数，并根据访问次数排序。
  ```
  * | SELECT count(*) as PV, url_extract_path(request_uri) as PATH GROUP BY PATH ORDER BY PV DESC LIMIT 5
  ```
  执行该查询语句后，返回的示例结果展示访问量排名前5的请求路径**PATH** 及其对应的访问次数**PV**。
  
- 查询request_uri字段的值以body.json结尾的日志。
  ```
  * | select * from log where request_uri like '%body.json'
  ```
  
 
#### 参考信息：网站日志样例
```
hostIP:203.0.113.15
collectTime:1719124688
hostName:website_access_log
body_bytes_sent:8736
client_ip:110.249.135.66
host:demo-business.cn
http_host:demo-business.cn
http_user_agent:Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/124.0.0.0 Safari/537.36
http_x_forwarded_for:110.249.135.66
instance_id:i-089d7f2c
instance_name:business-web-03
network_type:vpc
owner_id:op-789xyz-05
referer:https://search.demo-business.cn
remote_addr:10.0.120.45
remote_user:admin_ops
request_length:6892
request_method:GET
request_time:126
request_uri:/api/user/info/list?page=1&size=20
scheme:https
server_protocol:HTTP/1.1
status:200
time_local:14/Jun/2026:15:38:08
upstream_addr:10.0.56.18
upstream_response_time:87
upstream_status:200
user_agent:Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Geckocko) Chrome/124.0.0.0 Safari/537.36vip_addr:203.0.113.18
vpc_id:6cf942a3****15ce74629b41
```
