Skip to main content

UDP Logger

The udp-logger plugin sends request and response logs to a UDP collector after the gateway finishes processing a request. It supports custom formats, body capture, batching, and retries when a local send failure is detected, without delaying or failing the proxied response.

Default log entries can contain headers, query parameters, and other sensitive request data. UDP does not provide encryption, authentication, delivery confirmation, or retransmission. Send logs only across a trusted network, protect the collector, and use focused formats or masking when sensitive values should not leave the gateway.

Examples​

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

Start a UDP Collector​

Create and enter a working directory:

mkdir udp-logger-example
cd udp-logger-example

Create a collector that prints one received payload per line:

udp-collector.py
import socketserver


class Handler(socketserver.BaseRequestHandler):
def handle(self):
data, _socket = self.request
print(data.decode(), flush=True)


server = socketserver.ThreadingUDPServer(("0.0.0.0", 9000), Handler)
server.max_packet_size = 65535
server.serve_forever()

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

export GATEWAY_CONTAINER=replace-with-gateway-container-name
export UDP_LOG_HOST=udp-log-collector

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

Start the collector:

docker run -d --name udp-log-collector \
--network gateway-udp-logger-net \
-v "$PWD/udp-collector.py:/udp-collector.py:ro" \
python:3.13.15-alpine3.24 \
python -u /udp-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.

Each batch is encoded into one UDP packet. batch_max_size limits the number of entries, not the encoded byte size. Large packets can be fragmented or dropped by the network, so keep batches and log entries within the collector and network limits. This example sets the batch size to one for predictable verification.

curl "http://127.0.0.1:9180/apisix/admin/routes/udp-logger-route" -X PUT \
-H "X-API-KEY: ${ADMIN_API_KEY}" \
-d @- <<EOF
{
"uri": "/anything/udp-logger",
"plugins": {
"udp-logger": {
"host": "$UDP_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/udp-logger?source=docs" --silent --fail --output /dev/null

Read the latest collector payload and select stable fields:

docker logs --since 10s udp-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/udp-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/udp-logger-route/plugins/udp-logger" -X PATCH \
-H "X-API-KEY: ${ADMIN_API_KEY}" \
-H "Content-Type: application/json" \
-d @- <<EOF
{
"host": "$UDP_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/udp-logger?format=focused" --silent --fail --output /dev/null

Read the latest collector payload and select the fields configured in the custom format:

docker logs --since 10s udp-log-collector \
| tail -n 1 \
| jq '{method, uri, status}'

The selected fields confirm that the collector received the focused entry:

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