# 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[​](#examples "Direct link to 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[​](#start-a-udp-collector "Direct link to 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()
```

* Docker
* Kubernetes

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
```

[Set up the Ingress Controller and gateway](https://docs.api7.ai/ingress-controller/set-up-ingress-controller-and-gateway.md) in the `aic` namespace before continuing.

Create a ConfigMap from the collector script:

```
export UDP_LOG_HOST=udp-log-collector.aic.svc



kubectl create configmap udp-log-collector-script -n aic \

  --from-file=udp-collector.py \

  --dry-run=client -o yaml | kubectl apply -f -
```

Deploy the UDP collector:

udp-log-collector.yaml

```
apiVersion: apps/v1

kind: Deployment

metadata:

  namespace: aic

  name: udp-log-collector

spec:

  replicas: 1

  selector:

    matchLabels:

      app: udp-log-collector

  template:

    metadata:

      labels:

        app: udp-log-collector

    spec:

      containers:

        - name: collector

          image: python:3.13.15-alpine3.24

          command:

            - python

            - -u

            - /app/udp-collector.py

          ports:

            - containerPort: 9000

              protocol: UDP

          volumeMounts:

            - name: script

              mountPath: /app/udp-collector.py

              subPath: udp-collector.py

              readOnly: true

      volumes:

        - name: script

          configMap:

            name: udp-log-collector-script

---

apiVersion: v1

kind: Service

metadata:

  namespace: aic

  name: udp-log-collector

spec:

  selector:

    app: udp-log-collector

  ports:

    - port: 9000

      targetPort: 9000

      protocol: UDP
```

Apply the collector configuration:

```
kubectl apply -f udp-log-collector.yaml

kubectl rollout status -n aic deployment/udp-log-collector
```

### Send a Default Log Entry[​](#send-a-default-log-entry "Direct link to 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.

* Admin API
* ADC
* Ingress Controller

```
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
```

adc.yaml

```
services:

  - name: udp-logger-service

    labels:

      docs-example: udp-logger

    routes:

      - name: udp-logger-route

        uris:

          - /anything/udp-logger

        plugins:

          udp-logger:

            host: "${UDP_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=udp-logger
```

Review `diff.yaml`, then synchronize the labeled service:

```
adc sync -f adc.yaml \

  --include-resource-type service \

  --label-selector docs-example=udp-logger
```

* Gateway API
* APISIX CRD

udp-logger-ic.yaml

```
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: udp-logger-route

spec:

  plugins:

    - name: udp-logger

      config:

        host: udp-log-collector.aic.svc

        port: 9000

        batch_max_size: 1

---

apiVersion: gateway.networking.k8s.io/v1

kind: HTTPRoute

metadata:

  namespace: aic

  name: udp-logger-route

spec:

  parentRefs:

    - name: apisix

  rules:

    - matches:

        - path:

            type: Exact

            value: /anything/udp-logger

      filters:

        - type: ExtensionRef

          extensionRef:

            group: apisix.apache.org

            kind: PluginConfig

            name: udp-logger-route

      backendRefs:

        - name: httpbin-external-domain

          port: 80
```

udp-logger-ic.yaml

```
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: udp-logger-route

spec:

  ingressClassName: apisix

  plugins:

    - name: udp-logger

      enable: true

      config:

        host: udp-log-collector.aic.svc

        port: 9000

        batch_max_size: 1

---

apiVersion: apisix.apache.org/v2

kind: ApisixRoute

metadata:

  namespace: aic

  name: udp-logger-route

spec:

  ingressClassName: apisix

  http:

    - name: udp-logger-route

      match:

        paths:

          - /anything/udp-logger

      backends:

        - serviceName: httpbin-external-domain

          servicePort: 80

      plugin_config_name: udp-logger-route
```

Apply the configuration:

```
kubectl apply -f udp-logger-ic.yaml
```

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
* Kubernetes

```
docker logs --since 10s udp-log-collector \

  | tail -n 1 \

  | jq '{

      method: .request.method,

      uri: .request.uri,

      status: .response.status

    }'
```

```
kubectl logs -n aic deployment/udp-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/udp-logger?source=docs",

  "status": 200

}
```

### Send a Focused Log Entry[​](#send-a-focused-log-entry "Direct link to 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/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
```

In `adc.yaml`, replace the plugin configuration on the existing route:

adc.yaml (route plugin)

```
plugins:

  udp-logger:

    host: "${UDP_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=udp-logger
```

Review `diff.yaml`, then synchronize the labeled service:

```
adc sync -f adc.yaml \

  --include-resource-type service \

  --label-selector docs-example=udp-logger
```

Update the existing plugin configuration with the Kubernetes API used by the deployment:

* Gateway API
* APISIX CRD

udp-logger-ic.yaml

```
apiVersion: apisix.apache.org/v1alpha1

kind: PluginConfig

metadata:

  namespace: aic

  name: udp-logger-route

spec:

  plugins:

    - name: udp-logger

      config:

        host: udp-log-collector.aic.svc

        port: 9000

        batch_max_size: 1

        log_format:

          method: $request_method

          uri: $request_uri

          status: $status
```

udp-logger-ic.yaml

```
apiVersion: apisix.apache.org/v2

kind: ApisixPluginConfig

metadata:

  namespace: aic

  name: udp-logger-route

spec:

  ingressClassName: apisix

  plugins:

    - name: udp-logger

      enable: true

      config:

        host: udp-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 udp-logger-ic.yaml
```

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
* Kubernetes

```
docker logs --since 10s udp-log-collector \

  | tail -n 1 \

  | jq '{method, uri, status}'
```

```
kubectl logs -n aic deployment/udp-log-collector --since=10s \

  | 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

}
```
