跳到主要内容
版本:3.10.x

配置集中式日志记录

API7 网关会生成多种日志,用于调试和运维。生产环境建议将日志转发到 Elasticsearch、Splunk 或 AWS CloudWatch 等集中式日志管理系统。

日志类型和位置

数据面日志

数据面会写入错误日志和访问日志。使用官方容器镜像时,两个文件都位于 /usr/local/apisix/logs/

日志类型说明默认路径
错误日志网关启动事件、插件错误、上游故障和核心错误。/usr/local/apisix/logs/error.log
访问日志网关处理的每个请求一行。/usr/local/apisix/logs/access.log

如果不使用官方镜像,实际路径取决于安装前缀。两者都可以在 config.yaml 中配置。

控制面日志

控制面(控制台和 DP Manager)会将结构化日志输出到对应容器的标准输出:

docker logs <api7-ee-dashboard-container>
docker logs <api7-ee-dp-manager-container>

配置创建、更新、删除、登录和角色变更等管理操作会单独记录在审计日志中,并可在控制台查看。

配置日志文件

数据面的 config.yaml 使用以下配置项控制日志位置和错误日志详细程度:

conf/config.yaml
nginx_config:
error_log: logs/error.log
error_log_level: warn # debug, info, notice, warn, error, crit, alert, emerg
http:
enable_access_log: true
access_log: logs/access.log
access_log_format: '$remote_addr - $remote_user [$time_local] $http_host "$request_line" $status $body_bytes_sent $request_time "$http_referer" "$http_user_agent" $upstream_addr $upstream_status $upstream_response_time "$apisix_request_id"'
access_log_format_escape: default # default or json

相对路径以网关安装前缀为基准解析;官方镜像中的安装前缀为 /usr/local/apisix。修改后请重新加载或重启网关。

使用 JSON 访问日志格式

为了便于日志采集器解析,可以使用 JSON 格式并将转义方式设为 json

conf/config.yaml
nginx_config:
http:
access_log_format: '{"time_local":"$time_local","remote_addr":"$remote_addr","status":"$status","request_time":"$request_time","upstream_status":"$upstream_status","request":"$request"}'
access_log_format_escape: json

如需在每行访问日志中包含消费者标签,请参阅在访问日志中包含消费者标签

将日志转发到集中式系统

有两种互补方式:

  • 发送日志文件:在每台网关主机上运行 Fluentd、Vector、Filebeat 或 Logstash,读取 /usr/local/apisix/logs/access.logerror.log。此方式适用于 Elasticsearch、Splunk、AWS CloudWatch、Loki 等后端。
  • 从路由直接发送日志:使用 http-loggerkafka-loggersyslogsplunk-hec-logginggoogle-cloud-loggingelasticsearch-loggerrocketmq-loggerclickhouse-logger 等日志插件,通过网络发送结构化请求日志。完整的 Splunk HEC 流程请参阅将访问日志发送到 Splunk

如果 API7 网关运行在 Kubernetes 上,并且需要收集容器输出中的网关错误日志,请参阅将 Kubernetes 错误日志发送到 Splunk

后续步骤