How to specify the log format in kong-ingress while deploying using helm?

While using kong in EC2 I used to change the log format in nginx_kong.lua so that the log format is according to my wish.
While doing the same in kong-ingress, I wasnt able to find a way to change the nginx_kong.lua while deploying using kong on EKS.
Is there a way to add log format in the config file while deploying?

The only way to modify nginx_kong.lua itself if to build a custom image that replaces it.

Another option that may work is do direct the standard access log from kong.conf to /dev/null and then use directive injection to add a new format and log via environment variables:


http://nginx.org/en/docs/http/ngx_http_log_module.html#access_log
http://nginx.org/en/docs/http/ngx_http_log_module.html#log_format

NGINX doesn’t appear to mind having multiple access_log directives in a single server block, and that will allow you to control the format. I had some issues in my brief test where it created the log and then didn’t write to it, but that may have been a fluke in my environment.

We do normally recommend using a logging plugin, e.g. http-log instead of the standard NGINX logger, as there’s some information we have in Lua that doesn’t get exposed to NGINX and is useful to include in logs (finer-gained timing info, headers, etc.). Can you confirm your rationale for using the NGINX logger? For most K8S environments I assume because it’s easier to send NGINX logs to stdout (and by extension, the container daemon can scrape them easily from the Pod), and I’d like to encourage the product development team to investigate options for making that easier for logging plugins–more user reports along those lines will help.

Hi I am using nginx and kong in my present deployment in k8s.
Just wanted to keep both the log format same in the elastic search, so that it is easy to debug.

I even tried using HTTP log plugin but I was not getting any extra log in fluentd.
Do I need to so anything more to get the logs using http-log ?

Ah, okay, that’s a different use case we’d probably never be able to address in logging plugins (they include entire sections of data that will never show up in NGINX logs, so getting them to match format isn’t really possible). For that case, it would make sense to use the native NGINX logs, though it probably makes sense to use them in conjunction with a logging plugin for richer data if needed.

How do you have the http-log targets configured? Do you see any errors in logs?

I don’t have experience with fluentd or knowledge of your exact configuration, but I’d expect that setting up https://docs.fluentd.org/input/http and then pointing http-log to the fluentd address on the port you configure there should suffice.

@traines
I want to see the logs in some what this format which I use in nginx ingress controller.
Just the request and response wont be enough for me.

`log-format-upstream: >-
      {"proxy_protocol_addr": "$proxy_protocol_addr", "remote_addr": "$remote_addr", "remote_user": "$remote_user",
      "time_local": "$time_iso8601", "request": "$request", "status": "$status",
      "body_bytes_sent": "$body_bytes_sent", "http_referer": "$http_referer", "http_user_agent": "$http_user_agent",
      "request_length": "$request_length", "request_time": "$request_time", "proxy_upstream_name": "$proxy_upstream_name",
      "proxy_alternative_upstream_name": "$proxy_alternative_upstream_name", "upstream_addr": "$upstream_addr",
      "upstream_response_length": "$upstream_response_length", "upstream_response_time": "$upstream_response_time",
      "upstream_status": "$upstream_status", "req_id": "$req_id", "ingress_namespace": "$namespace",
      "ingress_name": "$ingress_name", "ingress_service_name": "$service_name", "ingress_service_port": "$service_port"}`

So how can I achieve this in kong-ingress-controller?

If you rewrite /usr/local/share/lua/5.1/kong/templates/nginx.lua and /usr/local/share/lua/5.1/kong/templates/nginx_kong.lua in a custom image, you’ll add those directives as you would in a standard NGINX configuration.

If you want to use directive injection you’ll add the format to http and the log directive to proxy, e.g.

env:
  - name: nginx_http_log_format
    valueFrom:
      configMapKeyRef:
         name: format-config
         key: upstream
  - name: nginx_proxy_access_log
    value: /dev/stdout upstream;
  - name: proxy_access_log
    value: /dev/null

The ConfigMap isn’t strictly necessary, but it may be useful to store the format there since it’s kinda large. Using multi-line YAML as shown in your example for the value key value should also work.