Default logs in JSON

Hi,

What is the best way to change the format of logs to json.
By default Kong as nginx_acc_logs and nginx_err_logs with a string format A - B - C - D.

the documentation is not very clear, can you help on this.

Solved, i´ve created a new custom-nginx.template with this:

http {
log_format json_combined escape=json
‘{’
‘“time_local”:"$time_iso8601",’
‘“remote_addr”:"$remote_addr",’
‘“request”:"$request",’
‘“status”: “$status”,’
‘“request_time”:"$request_time",’
‘“connection_requests”:"$connection_requests",’
‘“http_user_agent”:"$http_user_agent"’
‘}’;

access_log ${{PROXY_ACCESS_LOG}} json_combined;

include ‘nginx-kong-template.conf’;
}

and on start do kong start -c kong.yml --nginx-conf custom-nginx.template

3 Likes

Interesting! It can become a plugin :slight_smile:

Neat trick for those who want json output web server logs as opposed to the usual, nicely done.

It will more easy if we can put the log format pattern on global config.
I will do a pull request with this as soon as possible.

This only changes the format of access logs to json.
Did you find a way to also change the format of error logs to json?

is not supported by nginx change error logs

I have a pod within kubernetes and have kong fired up and running already but want to change the access log to log with json format.

i created a nginx template like the following

# ---------------------
# custom_nginx.template
# ---------------------

worker_processes ${{NGINX_WORKER_PROCESSES}}; # can be set by kong.conf
daemon ${{NGINX_DAEMON}};                     # can be set by kong.conf

pid pids/nginx.pid;                      # this setting is mandatory
error_log ${{PROXY_ERROR_LOG}} ${{LOG_LEVEL}}; # can be set by kong.conf

events {
  use epoll; # custom setting
  multi_accept on;
}

http {

  # Custom JSON log format
  log_format logstash_json '{ "@timestamp": "$time_iso8601", '
                             '"@fields": { '
                               '"remote_addr": "$remote_addr", '
                               '"remote_user": "$remote_user", '
                               '"body_bytes_sent": "$body_bytes_sent", '
                               '"request_time": "$request_time", '
                               '"status": "$status", '
                               '"request": "$request", '
                               '"request_method": "$request_method", '
                               '"http_referrer": "$http_referer", '
                               '"http_user_agent": "$http_user_agent" } }';

  # Log errors to stdout and in JSON for ELK centralized logging
  access_log ${{PROXY_ACCESS_LOG}} logstash_json;

  # include default Kong Nginx config
  include 'nginx-kong.conf';
}

and i reloaded kong within the pod by doing this

kong reload --nginx-conf custom_nginx.template

i see the nginx file in /usr/local/kong/nginx.conf got modifed,

# ---------------------
# custom_nginx.template
# ---------------------

worker_processes auto; # can be set by kong.conf
daemon off;                     # can be set by kong.conf

pid pids/nginx.pid;                      # this setting is mandatory
error_log /dev/stderr notice; # can be set by kong.conf

events {
  use epoll; # custom setting
  multi_accept on;
}

http {

  # Custom JSON log format
  log_format logstash_json '{ "@timestamp": "$time_iso8601", '
                             '"@fields": { '
                               '"remote_addr": "$remote_addr", '
                               '"remote_user": "$remote_user", '
                               '"body_bytes_sent": "$body_bytes_sent", '
                               '"request_time": "$request_time", '
                               '"status": "$status", '
                               '"request": "$request", '
                               '"request_method": "$request_method", '
                               '"http_referrer": "$http_referer", '
                               '"http_user_agent": "$http_user_agent" } }';

  # Log errors to stdout and in JSON for ELK centralized logging
  access_log /dev/stdout logstash_json;

  # include default Kong Nginx config
  include 'nginx-kong.conf';
}

but i dot not see request coming in as json when doing a curl against kong.

let me know if i am doing something incorrect here.

hi,

the difference from my sample is "log_format logstash_json escape=json try with this