Instant Logs 允许 Cloudflare 客户从 Cloudflare 仪表板或命令行界面 (CLI) 访问其域名的实时流量流。实时查看数据可帮助你调查攻击、排查问题、调试或测试对网络所做的更改。Instant Logs 轻量、易于使用,且无需任何额外设置。
| 免费版 | 专业版 (Pro) | 商业版 (Business) | 企业版 | |
|---|---|---|---|---|
| 可用性 | 否 | 否 | 是 | 是 |
- 在 Cloudflare 控制面板中,进入您区域的 可观测性 > 日志。
- 选择日志表格顶部的 三个点 (...)(操作)菜单。
- 选择 实时流式传输日志。
- (可选)选择 添加筛选条件,缩小显示的事件范围。
日志视图中的筛选条件和列会沿用到实时流中。无法流式传输的筛选条件会显示提示消息,而不会被静默丢弃。
通过向 Instant Logs 作业端点发送 POST 请求并附带以下参数来创建会话:
-
字段 — 列出 HTTP 请求数据集中可用的任意字段。
-
Sample - sample 参数是客户端设置的记录采样率:
"sample": 1表示 100% 的记录,"sample": 10表示 10%,以此类推。
- Filters - 使用筛选器深入到特定事件。筛选器由三部分组成:key、operator 和 value。
所有受支持的运算符可在 Filters 页面中找到。
以下是三个筛选器示例:
# Filter when client IP country is not Canada:
"filter": "{\"where\":{\"and\":[{\"key\":\"ClientCountry\",\"operator\":\"neq\",\"value\":\"ca\"}]}}"# Filter when the status code returned from Cloudflare is either 200 or 201:
"filter": "{\"where\":{\"and\":[{\"key\":\"EdgeResponseStatus\",\"operator\":\"in\",\"value\":[200,201]}]}}"# Filter when the request path contains "/static" and the request hostname is "example.com":
"filter": "{\"where\":{\"and\":[{\"key\":\"ClientRequestPath\",\"operator\":\"contains\",\"value\":\"/static\"}, {\"where\":{\"and\":[{\"key\":\"ClientRequestHost\",\"operator\":\"eq\",\"value\":\"example.com\"}]}}"使用 cURL 的示例请求:
Required API token permissions
At least one of the following token permissions is required:Logs Read
curl "https://api.cloudflare.com/client/v4/zones/$ZONE_ID/logpush/edge/jobs" \
--request POST \
--header "Authorization: Bearer $CLOUDFLARE_API_TOKEN" \
--json '{
"fields": "ClientIP,ClientRequestHost,ClientRequestMethod,ClientRequestURI,EdgeEndTimestamp,EdgeResponseBytes,EdgeResponseStatus,EdgeStartTimestamp,RayID",
"sample": 100,
"filter": "",
"kind": "instant-logs"
}'响应:
响应将包含一个名为 destination_conf 的新字段。该字段的值是你的唯一 WebSocket 地址,将接收来自 Cloudflare 全球网络的消息。
{
"errors": [],
"messages": [],
"result": {
"id": <JOB_ID>,
"fields": "ClientIP,ClientRequestHost,ClientRequestMethod,ClientRequestURI,EdgeEndTimestamp,EdgeResponseBytes,EdgeResponseStatus,EdgeStartTimestamp,RayID",
"sample": 100,
"filter": "",
"destination_conf": "wss://logs.cloudflare.com/instant-logs/ws/sessions/<SESSION_ID>",
"kind": "instant-logs"
},
"success": true
}使用 Websocat ↗ 等 CLI 工具,你可以连接到 WebSocket 并立即开始接收日志。
websocat wss://logs.cloudflare.com/instant-logs/ws/sessions/<SESSION_ID>响应:
连接到 websocket 后,你将收到以换行分隔的 JSON 消息。
现在你已连接到 Cloudflare 的 websocket,并正在接收来自 Cloudflare 全球网络的日志,可以开始对日志进行切片和分析。对此很实用的一个工具是 Angle Grinder ↗。Angle Grinder 允许你在 stdin 上应用筛选、转换和聚合,并提供一流的 JSON 支持。例如,要获取每个国家/地区的访问者数量,可以按 ClientCountry 字段对事件数求和。
websocat wss://logs.cloudflare.com/instant-logs/ws/sessions/<SESSION_ID> | agrind '* | json | sum(sampleInterval) by ClientCountry'响应:
| ClientCountry | _sum |
|---|---|
| pt | 4 |
| fr | 3 |
| us | 3 |
| om | 2 |
| ar | 1 |
| au | 1 |
目前仅支持 HTTP Requests 数据集。未来我们会扩展到其他数据集。
您可以通过 导出 按钮,将控制面板中显示的日志表格下载为 JSON 格式。
Instant Logs 设有三项限制:
- 每个 zone 仅允许一个活动的 Instant Logs 会话。
- 最长会话时间为 60 分钟。
- 如果你停止监听套接字超过五分钟。
如果达到上述任一限制,日志流将自动停止。
如果你有任何功能请求或发现任何错误,请加入 Cloudflare Developers Discord 社区 ↗,直接与我们分享反馈。