Kong provides a LOT of logging plugins for users to log request and response. Despite having all these logging plugins, they have one thing in common, you can’t log request and response body with them. Although there seems to be a lot of demand for this feature, this has never been added to Kong. Obviously this is by choice and confirmed by Kong engineer here.
The request/response body is left out of the logs on purpose as it is not bound (we don’t want to log the response body of a video streaming service) and will likely never be, within Kong itself.
Although I agree with this comment, there are always needs to log the request/response body and IMO users should always have the choice to do so. Previously it was only possible to log request and response with a custom logging. Luckily Kong introduced a new PDK since 2.4 version which allows users to set new log fields on the fly and output by one of official logging plugin.
By using this new PDK with pre-function plugin, we can inject request and response body on route/service/global level and then output by a logging plugin.
Let me show how easy this is.
Start Kong
I will deploy Kong in dbless mode and use admin API to push config via /config endpoint.
1 2 3 4 5 6
docker run --rm --detach --name kong \ -p "8000-8001:8000-8001" \ -e "KONG_ADMIN_LISTEN=0.0.0.0:8001" \ -e "KONG_PROXY_LISTEN=0.0.0.0:8000" \ -e "KONG_DATABASE=off" \ kong:3
This command should give us a kong container with admin API enabled.
Enable file log
Let’s save below file to /tmp/kong.yaml. This is a very simple setup with 1 service, 1 route and file log plugin enable globally.
Let’s create a new config file at /tmp/kong-new.yaml and use pre-function plugin. I’ve also added another route other-route here and enable pre-function plugin ONLY on demo-route.
If you send request to other-route at /demo, its log does not have request and response body.
One more thing
I agree the response log does not look clean and you might see an issue with other response body. The reason I chose kong.response.get_raw_body() PDK was kong response phase does not support http2, source.
If you know you will send your request in http1.1 only, you can log the response body with pre-function plugin as below.