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:
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()
- Docker
- Kubernetes
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
Set up the Ingress Controller and gateway in the aic namespace before continuing.
Create a ConfigMap from the collector script:
export TCP_LOG_HOST=tcp-log-collector.aic.svc
kubectl create configmap tcp-log-collector-script -n aic \
--from-file=tcp-collector.py \
--dry-run=client -o yaml | kubectl apply -f -
Deploy the TCP collector:
apiVersion: apps/v1
kind: Deployment
metadata:
namespace: aic
name: tcp-log-collector
spec:
replicas: 1
selector:
matchLabels:
app: tcp-log-collector
template:
metadata:
labels:
app: tcp-log-collector
spec:
containers:
- name: collector
image: python:3.13.15-alpine3.24
command: ["python", "-u", "/app/tcp-collector.py"]
ports:
- containerPort: 9000
volumeMounts:
- name: script
mountPath: /app/tcp-collector.py
subPath: tcp-collector.py
readOnly: true
volumes:
- name: script
configMap:
name: tcp-log-collector-script
---
apiVersion: v1
kind: Service
metadata:
namespace: aic
name: tcp-log-collector
spec:
selector:
app: tcp-log-collector
ports:
- port: 9000
targetPort: 9000
Apply the collector configuration:
kubectl apply -f tcp-log-collector.yaml
kubectl rollout status -n aic deployment/tcp-log-collector
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.
- Admin API
- ADC
- Ingress Controller
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
services:
- name: tcp-logger-service
labels:
docs-example: tcp-logger
routes:
- name: tcp-logger-route
uris:
- /anything/tcp-logger
plugins:
tcp-logger:
host: "${TCP_LOG_HOST}"
port: 9000
batch_max_size: 1
upstream:
type: roundrobin
nodes:
- host: httpbin.org
port: 80
weight: 1
Preview the scoped changes:
adc diff -f adc.yaml \
--include-resource-type service \
--label-selector docs-example=tcp-logger
Review diff.yaml, then synchronize the labeled service:
adc sync -f adc.yaml \
--include-resource-type service \
--label-selector docs-example=tcp-logger
- Gateway API
- APISIX CRD
apiVersion: v1
kind: Service
metadata:
namespace: aic
name: httpbin-external-domain
spec:
type: ExternalName
externalName: httpbin.org
ports:
- port: 80
targetPort: 80
---
apiVersion: apisix.apache.org/v1alpha1
kind: PluginConfig
metadata:
namespace: aic
name: tcp-logger-route
spec:
plugins:
- name: tcp-logger
config:
host: tcp-log-collector.aic.svc
port: 9000
batch_max_size: 1
---
apiVersion: gateway.networking.k8s.io/v1
kind: HTTPRoute
metadata:
namespace: aic
name: tcp-logger-route
spec:
parentRefs:
- name: apisix
rules:
- matches:
- path:
type: Exact
value: /anything/tcp-logger
filters:
- type: ExtensionRef
extensionRef:
group: apisix.apache.org
kind: PluginConfig
name: tcp-logger-route
backendRefs:
- name: httpbin-external-domain
port: 80
apiVersion: v1
kind: Service
metadata:
namespace: aic
name: httpbin-external-domain
spec:
type: ExternalName
externalName: httpbin.org
ports:
- port: 80
targetPort: 80
---
apiVersion: apisix.apache.org/v2
kind: ApisixPluginConfig
metadata:
namespace: aic
name: tcp-logger-route
spec:
ingressClassName: apisix
plugins:
- name: tcp-logger
enable: true
config:
host: tcp-log-collector.aic.svc
port: 9000
batch_max_size: 1
---
apiVersion: apisix.apache.org/v2
kind: ApisixRoute
metadata:
namespace: aic
name: tcp-logger-route
spec:
ingressClassName: apisix
http:
- name: tcp-logger-route
match:
paths:
- /anything/tcp-logger
backends:
- serviceName: httpbin-external-domain
servicePort: 80
plugin_config_name: tcp-logger-route
Apply the configuration:
kubectl apply -f tcp-logger-ic.yaml
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
- Kubernetes
docker logs --since 10s tcp-log-collector \
| tail -n 1 \
| jq '{
method: .request.method,
uri: .request.uri,
status: .response.status
}'
kubectl logs -n aic deployment/tcp-log-collector --since=10s \
| 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:
- Admin API
- ADC
- Ingress Controller
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
In adc.yaml, replace the plugin configuration on the existing route:
plugins:
tcp-logger:
host: "${TCP_LOG_HOST}"
port: 9000
batch_max_size: 1
log_format:
method: $request_method
uri: $request_uri
status: $status
Preview the updated service:
adc diff -f adc.yaml \
--include-resource-type service \
--label-selector docs-example=tcp-logger
Review diff.yaml, then synchronize the labeled service:
adc sync -f adc.yaml \
--include-resource-type service \
--label-selector docs-example=tcp-logger
Update the existing plugin configuration with the Kubernetes API used by the deployment:
- Gateway API
- APISIX CRD
apiVersion: apisix.apache.org/v1alpha1
kind: PluginConfig
metadata:
namespace: aic
name: tcp-logger-route
spec:
plugins:
- name: tcp-logger
config:
host: tcp-log-collector.aic.svc
port: 9000
batch_max_size: 1
log_format:
method: $request_method
uri: $request_uri
status: $status
apiVersion: apisix.apache.org/v2
kind: ApisixPluginConfig
metadata:
namespace: aic
name: tcp-logger-route
spec:
ingressClassName: apisix
plugins:
- name: tcp-logger
enable: true
config:
host: tcp-log-collector.aic.svc
port: 9000
batch_max_size: 1
log_format:
method: $request_method
uri: $request_uri
status: $status
Apply the updated plugin configuration:
kubectl apply -f tcp-logger-ic.yaml
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"
}