http-logger
http-logger 插件将请求和响应日志作为 JSON 对象分批推送到 HTTP(S) 服务器,并支持自定义日志格式。
示例
以下示例展示了如何在不同场景下配置 http-logger 插件。
要跟随示例操作,请使用 mockbin 启动一个模拟 HTTP 日志端点,并记下 mockbin URL。
以默认日志格式记录请求
以下示例演示了如何在路由上配置 http-logger 插件,以记录访问该路由的请求信息。
创建一个带有 http-logger 插件的路由,并配置你的服务器 URI:
- Admin API
- ADC
- Ingress Controller
curl "http://127.0.0.1:9180/apisix/admin/routes" -X PUT \
-H "X-API-KEY: ${ADMIN_API_KEY}" \
-d '{
"id": "http-logger-route",
"uri": "/anything",
"plugins": {
"http-logger": {
"uri": "https://669f05eb10ca49f18763e023312c3d77.api.mockbin.io/"
}
},
"upstream": {
"nodes": {
"httpbin.org:80": 1
},
"type": "roundrobin"
}
}'
adc.yaml
services:
- name: httpbin
routes:
- uris:
- /anything
name: http-logger-route
plugins:
http-logger:
uri: "https://669f05eb10ca49f18763e023312c3d77.api.mockbin.io/"
upstream:
type: roundrobin
nodes:
- host: httpbin.org
port: 80
weight: 1
将配置同步到网关:
adc sync -f adc.yaml
- Gateway API
- APISIX CRD
http-logger-ic.yaml
apiVersion: v1
kind: Service
metadata:
namespace: aic
name: httpbin-external-domain
spec:
type: ExternalName
externalName: httpbin.org
---
apiVersion: apisix.apache.org/v1alpha1
kind: PluginConfig
metadata:
namespace: aic
name: http-logger-plugin-config
spec:
plugins:
- name: http-logger
config:
uri: "https://669f05eb10ca49f18763e023312c3d77.api.mockbin.io/"
---
apiVersion: gateway.networking.k8s.io/v1
kind: HTTPRoute
metadata:
namespace: aic
name: http-logger-route
spec:
parentRefs:
- name: apisix
rules:
- matches:
- path:
type: Exact
value: /anything
filters:
- type: ExtensionRef
extensionRef:
group: apisix.apache.org
kind: PluginConfig
name: http-logger-plugin-config
backendRefs:
- name: httpbin-external-domain
port: 80
http-logger-ic.yaml
apiVersion: apisix.apache.org/v2
kind: ApisixUpstream
metadata:
namespace: aic
name: httpbin-external-domain
spec:
ingressClassName: apisix
externalNodes:
- type: Domain
name: httpbin.org
---
apiVersion: apisix.apache.org/v2
kind: ApisixRoute
metadata:
namespace: aic
name: http-logger-route
spec:
ingressClassName: apisix
http:
- name: http-logger-route
match:
paths:
- /anything
methods:
- GET
upstreams:
- name: httpbin-external-domain
plugins:
- name: http-logger
config:
uri: "https://669f05eb10ca49f18763e023312c3d77.api.mockbin.io/"
应用配置:
kubectl apply -f http-logger-ic.yaml
向路由发送请求:
curl "http://127.0.0.1:9080/anything"
你应该收到 HTTP/1.1 200 OK 响应。在你的 mockbin 中,你应该看到类似于以下的日志条目:
[
{
"upstream": "3.213.1.197:80",
"server": {
"hostname": "7d8d831179d4",
"version": "3.9.0"
},
"start_time": 1718291190508,
"client_ip": "192.168.65.1",
"response": {
"status": 200,
"headers": {
"server": "APISIX/3.9.0",
"content-length": "390",
"access-control-allow-credentials": "true",
"connection": "close",
"date": "Thu, 13 Jun 2024 15:06:31 GMT",
"access-control-allow-origin": "*",
"content-type": "application/json"
},
"size": 617
},
"latency": 1200.0000476837,
"upstream_latency": 1133,
"apisix_latency": 67.000047683716,
"request": {
"url": "http://127.0.0.1:9080/anything",
"querystring": {},
"method": "GET",
"uri": "/anything",
"headers": {
"accept": "*/*",
"user-agent": "curl/8.6.0",
"host": "127.0.0.1:9080"
},
"size": 85
},
"service_id": "",
"route_id": "http-logger-route"
}
]
使用插件元数据记录请求和响应头
以下示例演示了如何使用插件元数据和内置变量自定义日志格式,以记录请求和响应中的特定头。
在 APISIX 中,插件元数据用于配置同一插件的所有插件实例的通用元数据字段。当一个插件在多个资源中启用并且需要对其元数据字段进行通用更新时,这非常有用。
首先,创建一个带有 http-logger 插件的路由,并配置你的服务器 URI:
- Admin API
- ADC
- Ingress Controller
curl "http://127.0.0.1:9180/apisix/admin/routes" -X PUT \
-H "X-API-KEY: ${ADMIN_API_KEY}" \
-d '{
"id": "http-logger-route",
"uri": "/anything",
"plugins": {
"http-logger": {
"uri": "https://669f05eb10ca49f18763e023312c3d77.api.mockbin.io/"
}
},
"upstream": {
"nodes": {
"httpbin.org:80": 1
},
"type": "roundrobin"
}
}'
adc.yaml
services:
- name: httpbin
routes:
- uris:
- /anything
name: http-logger-route
plugins:
http-logger:
uri: "https://669f05eb10ca49f18763e023312c3d77.api.mockbin.io/"
upstream:
type: roundrobin
nodes:
- host: httpbin.org
port: 80
weight: 1
将配置同步到网关:
adc sync -f adc.yaml
- Gateway API
- APISIX CRD
http-logger-ic.yaml
apiVersion: v1
kind: Service
metadata:
namespace: aic
name: httpbin-external-domain
spec:
type: ExternalName
externalName: httpbin.org
---
apiVersion: apisix.apache.org/v1alpha1
kind: PluginConfig
metadata:
namespace: aic
name: http-logger-plugin-config
spec:
plugins:
- name: http-logger
config:
uri: "https://669f05eb10ca49f18763e023312c3d77.api.mockbin.io/"
---
apiVersion: gateway.networking.k8s.io/v1
kind: HTTPRoute
metadata:
namespace: aic
name: http-logger-route
spec:
parentRefs:
- name: apisix
rules:
- matches:
- path:
type: Exact
value: /anything
filters:
- type: ExtensionRef
extensionRef:
group: apisix.apache.org
kind: PluginConfig
name: http-logger-plugin-config
backendRefs:
- name: httpbin-external-domain
port: 80
http-logger-ic.yaml
apiVersion: apisix.apache.org/v2
kind: ApisixUpstream
metadata:
namespace: aic
name: httpbin-external-domain
spec:
ingressClassName: apisix
externalNodes:
- type: Domain
name: httpbin.org
---
apiVersion: apisix.apache.org/v2
kind: ApisixRoute
metadata:
namespace: aic
name: http-logger-route
spec:
ingressClassName: apisix
http:
- name: http-logger-route
match:
paths:
- /anything
methods:
- GET
upstreams:
- name: httpbin-external-domain
plugins:
- name: http-logger
config:
uri: "https://669f05eb10ca49f18763e023312c3d77.api.mockbin.io/"
应用配置:
kubectl apply -f http-logger-ic.yaml
接下来,为 http-logger 配置插件元数据:
- Admin API
- ADC
- Ingress Controller
curl "http://127.0.0.1:9180/apisix/admin/plugin_metadata/http-logger" -X PUT \
-H "X-API-KEY: ${ADMIN_API_KEY}" \
-d '{
"log_format": {
"host": "$host",
"@timestamp": "$time_iso8601",
"client_ip": "$remote_addr",
"env": "$http_env",
"resp_content_type": "$sent_http_Content_Type"
}
}'
adc.yaml
plugin_metadata:
- name: http-logger
log_format:
host: "$host"
"@timestamp": "$time_iso8601"
client_ip: "$remote_addr"
env: "$http_env"
resp_content_type: "$sent_http_Content_Type"
将配置同步到网关:
adc sync -f adc.yaml
gatewayproxy.yaml
apiVersion: apisix.apache.org/v1alpha1
kind: GatewayProxy
metadata:
namespace: aic
name: apisix-config
spec:
provider:
type: ControlPlane
controlPlane:
# ...
# 控制面连接配置
pluginMetadata:
http-logger:
log_format:
host: "$host"
"@timestamp": "$time_iso8601"
client_ip: "$remote_addr"
env: "$http_env"
resp_content_type: "$sent_http_Content_Type"
应用配置:
kubectl apply -f gatewayproxy.yaml
❶ 记录自定义请求头 env。
❷ 记录响应头 Content-Type。
向路由发送带有 env 头的请求:
curl "http://127.0.0.1:9080/anything" -H "env: dev"
你应该收到 HTTP/1.1 200 OK 响应。在你的 mockbin 中,你应该看到类似于以下的日志条目:
[
{
"route_id": "http-logger-route",
"client_ip": "192.168.65.1",
"@timestamp": "2024-06-13T15:19:34+00:00",
"host": "127.0.0.1",
"env": "dev",
"resp_content_type": "application/json"
}
]