跳到主要内容
版本:3.10.x

API7 网关 AI Agent Skill:plugin http logger

本页提供 plugin http logger 场景的 API7 企业版操作指南。请保留下方命令、字段、配置项和 API 路径的原样,并根据实际网关环境替换示例值。

Overview

The http-logger plugin pushes request/response logs as JSON to HTTP or HTTPS endpoints. Logs are batched for efficiency and support custom formats using NGINX variables. Use it to send structured logs to any HTTP-based logging backend (Elasticsearch, Loki, custom APIs, etc.).

When to Use

  • Ship access logs to an HTTP-based logging backend
  • Custom log formats with selected fields only
  • Conditional request/response body capture
  • Batch log delivery with retry on failure

Plugin Configuration Reference

FieldTypeRequiredDefaultDescription
uristringYesHTTP/HTTPS endpoint for log delivery
auth_headerstringNoAuthorization header value
timeoutintegerNo3Connection timeout in seconds
log_formatobjectNoCustom log format (supports $variable syntax)
include_req_bodybooleanNofalseInclude request body in logs
include_req_body_exprarrayNoConditional expression for request body logging
include_resp_bodybooleanNofalseInclude response body in logs
include_resp_body_exprarrayNoConditional expression for response body logging
concat_methodstringNo"json"Batch format: json (array) or new_line (newline-separated)
ssl_verifybooleanNofalseVerify SSL certificate for HTTPS endpoints

Batch Processing Parameters

FieldTypeDefaultDescription
batch_max_sizeinteger1000Max entries per batch
inactive_timeoutinteger5Seconds before flushing incomplete batch
buffer_durationinteger60Max age of oldest entry before forced flush
max_retry_countinteger0Retry attempts on failure
retry_delayinteger1Seconds between retries

Default Log Entry Format

When no custom log_format is set, each log entry contains:

{
"client_ip": "127.0.0.1",
"route_id": "1",
"start_time": 1703907485819,
"latency": 101.9,
"apisix_latency": 100.9,
"upstream_latency": 1,
"upstream": "127.0.0.1:8080",
"request": {
"method": "GET",
"uri": "/api/users",
"url": "http://127.0.0.1:9080/api/users",
"size": 194,
"headers": { "host": "...", "user-agent": "..." }
},
"response": {
"status": 200,
"size": 123,
"headers": { "content-type": "...", "content-length": "..." }
}
}

Step-by-Step: Ship Logs to an HTTP Endpoint

1. Create a route with http-logger

Enable logging for gateway group default:

a7 route create --gateway-group default -f - <<'EOF'
{
"id": "logged-api",
"uri": "/api/*",
"plugins": {
"http-logger": {
"uri": "http://log-collector:8080/logs",
"batch_max_size": 100,
"inactive_timeout": 10
}
},
"upstream": {
"type": "roundrobin",
"nodes": [{"host": "backend", "port": 8080, "weight": 1}]
}
}
EOF

2. Global logging for a gateway group

Apply a Global Rule to log all traffic in the prod group:

a7 global_rule create --gateway-group prod -f - <<'EOF'
{
"id": "http-logger-global",
"plugins": {
"http-logger": {
"uri": "http://log-collector:8080/global-logs",
"batch_max_size": 500,
"inactive_timeout": 30
}
}
}
EOF

Common Patterns

Custom log format with NGINX variables

{
"plugins": {
"http-logger": {
"uri": "http://log-collector:8080/logs",
"log_format": {
"@timestamp": "$time_iso8601",
"client_ip": "$remote_addr",
"host": "$host",
"method": "$request_method",
"uri": "$request_uri",
"status": "$status",
"latency": "$request_time"
}
}
}
}

Authenticated endpoint

{
"plugins": {
"http-logger": {
"uri": "https://log-service.example.com/api/v1/logs",
"auth_header": "Bearer eyJhbGciOiJIUzI1NiIs...",
"ssl_verify": true,
"timeout": 5
}
}
}

Conditional request body logging

Log request bodies only when a query parameter is present:

{
"plugins": {
"http-logger": {
"uri": "http://log-collector:8080/logs",
"include_req_body": true,
"include_req_body_expr": [
["arg_debug", "==", "true"]
]
}
}
}

Troubleshooting

SymptomCauseFix
No logs arrivingWrong uri or endpoint downVerify endpoint is reachable from gateway nodes
SSL handshake failureCertificate not trustedSet ssl_verify: false for self-signed certs
Logs delayedLarge inactive_timeoutLower inactive_timeout for faster delivery
Logs droppedBuffer overflowIncrease batch_max_size; reduce delivery latency
Auth rejectedWrong auth_header valueInclude full header value (e.g. Bearer <token>)
Config not appliedWrong gateway group specifiedEnsure --gateway-group matches the desired cluster

Config Sync Example

version: "1"
gateway_group: default
routes:
- id: logged-api
uri: /api/*
plugins:
http-logger:
uri: http://log-collector:8080/logs
batch_max_size: 200
inactive_timeout: 10
log_format:
timestamp: "$time_iso8601"
client_ip: "$remote_addr"
method: "$request_method"
status: "$status"
upstream:
type: roundrobin
nodes:
- host: backend
port: 8080
weight: 1

This page is generated from a7-plugin-http-logger/SKILL.md in the api7/a7 repository. Browse all skills on the AI Agent Skills page.