Customize the Default Log Format in Kong API Gateway

Role of Nginx

# Sample Log Line from Default log Pattern
172.10.24.50 - - [05/May/2021:08:19:40 +0000] "POST /api/service/getinfo HTTP/1.1" 200 2313 "-" "curl/7.61.1"

# Parameters in the above log line
$remote_addr - $remote_user [$time_local] "$request" $status $body_bytes_sent "$http_referer" "$http_user_agent"

Injecting Nginx directives

nginx_http_log_format=show_everything '\$time_iso8601 - \$bytes_sent - \$request - \$status - \$remote_addr'
proxy_access_log=/dev/stdout show_everything

Environment variables

-e "KONG_NGINX_HTTP_LOG_FORMAT=show_everything '\$time_iso8601 - \$bytes_sent - \$request - \$status - \$remote_addr'"
-e "KONG_PROXY_ACCESS_LOG=/dev/stdout show_everything"

My Log Format

$remote_addr - $remote_user [$time_local] $start_time "$request" $status $body_bytes_sent "$http_referer" "$http_user_agent"
# To get the request time, add the below env variable
KONG_NGINX_PROXY_SET_BY_LUA=$$start_time 'return ngx.now()'
# To get the request time, add the below env variable
KONG_NGINX_PROXY_SET_BY_LUA=$$start_time 'return ngx.now()'
# Below env variables are to define your log pattern:
KONG_PROXY_ACCESS_LOG=/dev/stdout show_everything
KONG_NGINX_HTTP_LOG_FORMAT=show_everything '$remote_addr - $remote_user [$time_local] $start_time - "$request" $status $body_bytes_sent "$http_referer" "$http_user_agent"'
172.18.240.50 - - [05/May/2021:08:59:49 +0000] 1620205189.259 - "POST /api/service/getinfo HTTP/1.1" 200 2313 "-" "curl/7.61.1"# Format of the log line:
# {Source IP} - {Remote User} [{Response time}] {Request time} .....
KONG_NGINX_PROXY_SET_BY_LUA=$$start_time 'return ngx.now()'
KONG_PROXY_ACCESS_LOG=/dev/stdout show_everything
KONG_NGINX_HTTP_LOG_FORMAT=show_everything '$remote_addr - $remote_user [$time_local - $msec] $start_time - $request_time "$request" $status $body_bytes_sent "$http_referer" "$http_user_agent"'

# Resulting Log Format:
# {Source IP} - {Remote User} [{Response time} - {Response time in milliseconds}] {Request time} - {Duration of the request} .....
172.18.240.50 - - [05/May/2021:08:59:49 +0000 - 1620205189.300] 1620205189.259 - 0.041 "POST /api/service/getinfo HTTP/1.1" 200 2313 "-" "curl/7.61.1"

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store