API7 Docs

kafka-logger plugin for Apache APISIX

Skill for configuring the Apache APISIX kafka-logger plugin via the a6 CLI. Covers pushing access logs to Apache Kafka topics, broker configuration, SAS…

Overview

The kafka-logger plugin pushes request/response logs to Apache Kafka topics. It supports multiple brokers, SASL authentication, async/sync producing, custom log formats, and batch processing for efficient delivery.

When to Use

  • Stream access logs to Kafka for downstream processing
  • Feed real-time API analytics pipelines
  • Integrate with Kafka-based logging infrastructure
  • Need SASL-authenticated Kafka clusters

Plugin Configuration Reference

Core Parameters

FieldTypeRequiredDefaultDescription
brokersarrayYesKafka broker list
brokers[].hoststringYesBroker hostname or IP
brokers[].portintegerYesBroker port (1-65535)
kafka_topicstringYesTarget Kafka topic
keystringNoPartition key for routing
timeoutintegerNo3Connection timeout in seconds

SASL Authentication

FieldTypeRequiredDefaultDescription
brokers[].sasl_configobjectNoSASL config per broker
brokers[].sasl_config.mechanismstringNo"PLAIN"PLAIN, SCRAM-SHA-256, or SCRAM-SHA-512
brokers[].sasl_config.userstringYes*SASL username (*if sasl_config set)
brokers[].sasl_config.passwordstringYes*SASL password (*if sasl_config set)

Producer Configuration

FieldTypeDefaultDescription
producer_typestring"async"async (batched) or sync (immediate)
required_acksinteger11 (leader ack) or -1 (all replicas)
producer_batch_numinteger200Messages per Kafka batch
producer_batch_sizeinteger1048576Batch size in bytes (1MB)
producer_max_bufferinginteger50000Max buffered messages
producer_time_lingerinteger1Flush interval in seconds
meta_refresh_intervalinteger30Kafka metadata refresh in seconds
cluster_nameinteger1Cluster identifier (for multi-cluster)

Log Format Options

FieldTypeDefaultDescription
meta_formatstring"default"default (JSON) or origin (raw HTTP)
log_formatobjectCustom log format with $variable syntax
include_req_bodybooleanfalseInclude request body
include_req_body_exprarrayConditional request body logging
include_resp_bodybooleanfalseInclude response body
include_resp_body_exprarrayConditional response body logging
max_req_body_bytesinteger524288Max request body size to log (512KB)
max_resp_body_bytesinteger524288Max response body size to log (512KB)

Batch Processing Parameters

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

Step-by-Step: Ship Logs to Kafka

1. Create a route with kafka-logger

a6 route create -f - <<'EOF'
{
  "id": "kafka-logged-api",
  "uri": "/api/*",
  "plugins": {
    "kafka-logger": {
      "brokers": [
        {"host": "kafka-1", "port": 9092},
        {"host": "kafka-2", "port": 9092}
      ],
      "kafka_topic": "apisix-logs",
      "batch_max_size": 100
    }
  },
  "upstream": {
    "type": "roundrobin",
    "nodes": {
      "backend:8080": 1
    }
  }
}
EOF

2. Verify messages in Kafka

kafka-console-consumer --bootstrap-server kafka-1:9092 --topic apisix-logs --from-beginning

Common Patterns

SASL-authenticated Kafka cluster

{
  "plugins": {
    "kafka-logger": {
      "brokers": [
        {
          "host": "kafka.example.com",
          "port": 9092,
          "sasl_config": {
            "mechanism": "SCRAM-SHA-256",
            "user": "apisix",
            "password": "secret"
          }
        }
      ],
      "kafka_topic": "api-logs",
      "required_acks": -1
    }
  }
}

Custom log format

{
  "plugins": {
    "kafka-logger": {
      "brokers": [{"host": "kafka", "port": 9092}],
      "kafka_topic": "api-logs",
      "log_format": {
        "@timestamp": "$time_iso8601",
        "client_ip": "$remote_addr",
        "method": "$request_method",
        "uri": "$request_uri",
        "status": "$status",
        "latency": "$request_time",
        "upstream": "$upstream_addr"
      }
    }
  }
}

Partition by route ID

{
  "plugins": {
    "kafka-logger": {
      "brokers": [{"host": "kafka", "port": 9092}],
      "kafka_topic": "api-logs",
      "key": "$route_id"
    }
  }
}

High-throughput tuning

{
  "plugins": {
    "kafka-logger": {
      "brokers": [
        {"host": "kafka-1", "port": 9092},
        {"host": "kafka-2", "port": 9092},
        {"host": "kafka-3", "port": 9092}
      ],
      "kafka_topic": "api-logs",
      "producer_type": "async",
      "producer_batch_num": 500,
      "producer_batch_size": 2097152,
      "producer_max_buffering": 100000,
      "producer_time_linger": 2,
      "batch_max_size": 5000,
      "inactive_timeout": 10,
      "required_acks": 1
    }
  }
}

Raw HTTP log format

{
  "plugins": {
    "kafka-logger": {
      "brokers": [{"host": "kafka", "port": 9092}],
      "kafka_topic": "raw-logs",
      "meta_format": "origin"
    }
  }
}

Produces raw HTTP request text instead of JSON.

Config Sync Example

version: "1"
routes:
  - id: kafka-logged-api
    uri: /api/*
    plugins:
      kafka-logger:
        brokers:
          - host: kafka-1
            port: 9092
          - host: kafka-2
            port: 9092
        kafka_topic: apisix-logs
        producer_type: async
        required_acks: 1
        batch_max_size: 200
        inactive_timeout: 5
    upstream_id: my-upstream

Troubleshooting

SymptomCauseFix
No messages in KafkaBroker unreachableVerify broker host/port; check firewall
SASL auth failureWrong credentials or mechanismVerify user/password; ensure mechanism matches Kafka config
Messages delayedLarge batch/timeout settingsReduce inactive_timeout and producer_time_linger
Messages droppedBuffer overflowIncrease producer_max_buffering; add more brokers
Topic not foundTopic doesn't exist and auto-create disabledCreate topic manually or enable auto.create.topics.enable
High latencyrequired_acks: -1 with slow replicasUse required_acks: 1 for lower latency (less durability)

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