You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何配置APISIX File Logger以记录请求响应体?

APISIX File Logger 响应体日志记录配置问题解决

问题描述

已为APISIX路由配置File Logger插件,每次请求可生成日志条目,但日志中始终缺失response_body字段,需调整配置实现响应体的日志记录。

当前配置信息

路由插件配置(JSON)

...
"plugins": {
  "key-auth": {
    ...
  },
  "file-logger": {
    "path": "/usr/local/apisix/apisix_log/test.log", 
    "log_format": {
      "ip_client": "$remote_addr",
      "http_verb": "$request_method",
      "request_body": "$request_body", 
      "consumer": "$consumer_name",
      "url": "$uri",
      "parametres": "$args",
      "http_code": "$status",
      "response_body": "$response_body",
      "temps_reponse": "$upstream_response_time"
    },
    "include_resp_body": true, 
    "buffer": {
      "size": 1048576,  
      "flush_interval": 100 
    },
    "rotate": {
      "max_size": 10, 
      "max_days": 7   
    }
  }
}

Docker Compose配置

apisix:
    container_name: apisix
    #image: apache/apisix:${APISIX_IMAGE_TAG:-3.11.0-debian}
    image: apache/apisix:3.7.0-debian
    volumes:
      - ./apisix_conf/config.yaml:/usr/local/apisix/conf/config.yaml:ro
      - ./cert.pem:/usr/local/apisix/keycloak-cert.pem:ro
      #- ./apisix_log/test.log:/usr/local/apisix/apisix_log/test.log
      - type: bind
        source: ./apisix_log/test.log
        target: /usr/local/apisix/apisix_log/test.log
    restart: always
    depends_on:
      etcd:
        condition: service_healthy
    ports:
      - "9180:9180/tcp"
      - "9080:9080/tcp"
      - "9091:9091/tcp"
      - "9443:9443/tcp"
      - "9092:9092/tcp"
    networks:
      - apisix

当前日志示例

{"http_code":200,"temps_reponse":0.048,"request_body":"{ \"sql\" : \"select emp.first_name,jobs.job_title,dept.department_name\nfrom employees emp,departments dept,jobs \nwhere emp.department_id = dept.department_id\nand emp.job_id = jobs.job_id and rownum = 1\"}","ip_client":"192.167.1.1","http_verb":"POST","url":"/ords/hr/jpa/getAll","route_id":"549761667391554410","consumer":"user_mas"}

解决方案

针对APISIX 3.7.0版本,按以下步骤调整配置:

1. 完善File Logger插件配置

虽然已开启include_resp_body: true,但需补充响应体收集的过滤规则和大小限制,确保响应体能被正确捕获:

"file-logger": {
  "path": "/usr/local/apisix/apisix_log/test.log", 
  "log_format": {
    "ip_client": "$remote_addr",
    "http_verb": "$request_method",
    "request_body": "$request_body", 
    "consumer": "$consumer_name",
    "url": "$uri",
    "parametres": "$args",
    "http_code": "$status",
    "response_body": "$response_body",
    "temps_reponse": "$upstream_response_time"
  },
  "include_resp_body": true,
  "include_resp_body_expr": [["true"]], // 不对响应做过滤,所有请求都记录响应体;如需过滤可设置如[["==", "$status", 200]]
  "max_resp_body_size": 4194304, // 设置最大记录响应体大小为4MB,可根据业务调整
  "buffer": {
    "size": 1048576,  
    "flush_interval": 100 
  },
  "rotate": {
    "max_size": 10, 
    "max_days": 7   
  }
}

2. 处理压缩响应(若存在)

如果上游服务返回gzip/brotli压缩格式的响应,需在路由插件中添加decompressor插件解压响应体,否则$response_body会是二进制压缩数据:

"plugins": {
  "key-auth": {
    ...
  },
  "decompressor": {
    "enable": true
  },
  "file-logger": {
    ... // 上述完善后的配置
  }
}

3. 调整APISIX全局配置

在config.yaml中确保允许足够大的响应体通过,避免被截断:

apisix:
  proxy_body_size: 10m # 设置为业务所需的最大响应体大小

4. 重启APISIX生效

修改配置后重启容器加载新配置:

docker restart apisix

完成以上步骤后,重新发起请求即可在日志中看到response_body字段。

内容的提问来源于stack exchange,提问作者user3090303

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.14 15:50:12