apisix icon indicating copy to clipboard operation
apisix copied to clipboard

help request: log all request headers by http_logger

Open tiepgm opened this issue 2 years ago • 12 comments

Description

I am using http_logger to log all requests. But cannot find any variable for request's headers. Can someone show me hot to send request header to http-logger? Thank you

Environment

  • APISIX version (run apisix version): 3.9.0
  • Operating system (run uname -a): Debian 11
  • OpenResty / Nginx version (run openresty -V or nginx -V): openresty/1.25.3.1
  • etcd version, if relevant (run curl http://127.0.0.1:9090/v1/server_info):3.5.0
  • APISIX Dashboard version, if relevant:
  • Plugin runner version, for issues related to plugin runners:
  • LuaRocks version, for installation issues (run luarocks --version):

tiepgm avatar Jun 06 '24 03:06 tiepgm

The default log format includes the request and response headers. What's missing in your use case?

kayx23 avatar Jun 06 '24 04:06 kayx23

In mycase, I am using log_format to get more information. And cannot find the variables for request's header.

tiepgm avatar Jun 06 '24 04:06 tiepgm

And cannot find the variables for request's header.

Can you paste what you get?

kayx23 avatar Jun 06 '24 05:06 kayx23

yes, I am using this configuration for http_logger

{ "_meta": { "disable": false }, "batch_max_size": 1000, "buffer_duration": 60, "concat_method": "json", "inactive_timeout": 5, "include_req_body": true, "include_resp_body": true, "log_format": { "balancer_ip": "$balancer_ip", "balancer_port": "$balancer_port", "client_ip": "$remote_addr", "content_type": "$content_type", "geoip_latitude": "$geoip_latitude", "geoip_longitude": "$geoip_longitude", "host": "$host", "http_user_agent": "$http_user_agent", "request_body": "$request_body", "request_method": "$request_method", "request_time": "$request_time", "request_uri": "$request_uri", "resp_body": "$resp_body", "status": "$status", "timestamp": "$time_iso8601", "upstream_response_time": "$upstream_response_time", "upstream_status": "$upstream_status", "uri": "$uri" }, "max_retry_count": 0, "name": "http logger", "retry_delay": 1, "ssl_verify": false, "timeout": 3, "uri": "..." }

And this is the log i got

{ "_id" : ObjectId("666127719c48e0d8946f7e71"), "host" : "109.362.25.36", "status" : NumberInt(200), "request_method" : "POST", "balancer_ip" : "109.362.25.37", "balancer_port" : NumberInt(7001), "request_time" : 0.146, "upstream_response_time" : 0.145, "client_ip" : "42.119.173.189", "request_uri" : "/api/v1/get_order", "upstream_status" : "200", "timestamp" : "2024-06-06T03:05:11+00:00", "request_body" : "{\r\n "shop_id": "shop_test",\r\n "page": 0,\r\n "page_size": 10,\r\n "start_date": "2024-05-01",\r\n "end_date": "2024-05-31",\r\n "email": "",\r\n "status": ""\r\n}", "route_id" : "503471665204691658", "uri" : "/productreviews/email_service/query_list_order", "http_user_agent" : "PostmanRuntime/7.37.3", "content_type" : "application/json", "resp_body" : "{"_pagination":{"total":0,"totalPage":1},"data":[],"message":"OK","result_code":200}\n" }

tiepgm avatar Jun 06 '24 07:06 tiepgm

Does none of the $req_header and $req_headers work?

kayx23 avatar Jun 06 '24 07:06 kayx23

Does none of the $req_header and $req_headers work?

No, it is not work. I also cannot find the $req_header or $req_headers in apisix/nginx variable (https://nginx.org/en/docs/varindex.html; https://apisix.apache.org/docs/apisix/apisix-variable/)

tiepnh avatar Jun 06 '24 08:06 tiepnh

Did you modify the logger configuration?

shreemaan-abhishek avatar Jun 13 '24 04:06 shreemaan-abhishek

I have the same issue. I want to add additional user information, but this requires modifying the log_format. However, after modification, the headers can no longer be included.

cverdela avatar Nov 04 '24 00:11 cverdela

Hi, I have the same issue. I have to use log_format because I want to add specific information, but then I'm loosing the headers and can't find any field that can returns them...

haddasbronfman avatar Apr 21 '25 12:04 haddasbronfman

The reason the $req_header variable does not work is that it is not a standard APISIX variable or an NGINX built-in variable. If you want to get request header information, you need to use the correct variable format. To get the request header information, you should use the $http_* format, where * is the name of the request header (lowercase, and the hyphen - is replaced with an underscore _). Example:

  • Get User-Agent header: use $http_user_agent
  • Get Content-Type header: use $http_content_type
  • Get X-Real-IP header: use $http_x_real_ip

If you need to get specific response headers in a custom log_format, you can use the $sent_http_* prefix.

The best way to do this is to use the default full log format, which will automatically include all request headers. If the default format contains too much information you don't need, you can process and filter the data on the server side that receives the log. If you really need to customize the format and include all request headers, you may need to develop a custom plugin to meet this specific need.

https://nginx.org/en/docs/http/ngx_http_core_module.html#var_http_ https://nginx.org/en/docs/http/ngx_http_core_module.html#var_sent_http_

Baoyuantop avatar Apr 22 '25 02:04 Baoyuantop

Marked,I have the same issue. I have to use log_fomat to defined the new format to add some specific info.

sola634 avatar Jun 04 '25 08:06 sola634

If there is still a problem, please open it again.

Baoyuantop avatar Mar 04 '26 05:03 Baoyuantop