Skip to main content

TCP Logger

The tcp-logger plugin sends request and response logs to a TCP collector after the gateway finishes processing a request. It supports custom formats, body capture, batching, and retries without delaying or failing the proxied response when delivery fails.

Default log entries can contain headers, query parameters, and other sensitive request data. Protect the collector and transport, and use focused formats or masking when sensitive values should not leave the gateway. TLS encrypts the connection but does not verify the collector certificate.

Examples​

The examples show how to send default gateway access logs to a TCP collector and how to replace the default format with selected fields.

Start a TCP Collector​

Create and enter a working directory:

mkdir tcp-logger-example
cd tcp-logger-example

Create a collector that prints one received payload per line:

tcp-collector.py
import socketserver


class Handler(socketserver.BaseRequestHandler):
def handle(self):
chunks = []
while True:
chunk = self.request.recv(65536)
if not chunk:
break
chunks.append(chunk)
print(b"".join(chunks).decode(), flush=True)


socketserver.ThreadingTCPServer(("0.0.0.0", 9000), Handler).serve_forever()

Set the running gateway container and connect it to a dedicated network:

export GATEWAY_CONTAINER=replace-with-gateway-container-name
export TCP_LOG_HOST=tcp-log-collector

docker network create gateway-tcp-logger-net
docker network connect gateway-tcp-logger-net "$GATEWAY_CONTAINER"

Start the collector:

docker run -d --name tcp-log-collector \
--network gateway-tcp-logger-net \
-v "$PWD/tcp-collector.py:/tcp-collector.py:ro" \
python:3.13.15-alpine3.24 \
python -u /tcp-collector.py

Send a Default Log Entry​

Configure a route to send each default log entry immediately so that you can verify it in the collector output.

curl "http://127.0.0.1:9180/apisix/admin/routes/tcp-logger-route" -X PUT \
-H "X-API-KEY: ${ADMIN_API_KEY}" \
-d @- <<EOF
{
"uri": "/anything/tcp-logger",
"plugins": {
"tcp-logger": {
"host": "$TCP_LOG_HOST",
"port": 9000,
"batch_max_size": 1
}
},
"upstream": {
"type": "roundrobin",
"nodes": {
"httpbin.org:80": 1
}
}
}
EOF

Send a request:

curl "http://127.0.0.1:9080/anything/tcp-logger?source=docs" --silent --fail --output /dev/null

Read the latest collector payload and select stable fields:

docker logs --since 10s tcp-log-collector \
| tail -n 1 \
| jq '{
method: .request.method,
uri: .request.uri,
status: .response.status
}'

The selected fields confirm that the collector received the default access record:

{
"method": "GET",
"uri": "/anything/tcp-logger?source=docs",
"status": 200
}

Send a Focused Log Entry​

Update the plugin to replace the default entry with selected fields:

curl "http://127.0.0.1:9180/apisix/admin/routes/tcp-logger-route/plugins/tcp-logger" -X PATCH \
-H "X-API-KEY: ${ADMIN_API_KEY}" \
-H "Content-Type: application/json" \
-d @- <<EOF
{
"host": "$TCP_LOG_HOST",
"port": 9000,
"batch_max_size": 1,
"log_format": {
"method": "\$request_method",
"uri": "\$request_uri",
"status": "\$status"
}
}
EOF

Send a request:

curl "http://127.0.0.1:9080/anything/tcp-logger?format=focused" --silent --fail --output /dev/null

The collector receives the focused object:

{
"method": "GET",
"uri": "/anything/tcp-logger?format=focused",
"status": 200,
"route_id": "tcp-logger-route"
}