kong 网关日志格式修改
kong 網關日志格式修改
kong網關默認日志在/usr/local/kong/logs,主要有access.log,admin_access.log,error.log,access.log和nginx的access日志類似,這里會記錄所所有通過kong網關的日志。
有些時候這些日志并不能滿足我們的需要,因此需要修改日志格式。
當發現/usr/local/kong/下面有nginx-kong.conf,nginx.conf,nginx-kong-stream.conf這些配置文件時,以為這就是日志的配置文件,結果不管怎么修改這些日志格式,重啟后,配置文件都會還原。
最后查詢原因才得知,這些文件時kong網關啟動時通過模板渲染方式生成的,每次重啟都會重新生成,而真正的配置文件卻在另外一個地方。
這里要介紹下kong的安裝方式:
本文的服務器環境為ubuntu,安裝請參考:https://docs.konghq.com/install/ubuntu/
一、修改配置
修改路徑:/usr/local/share/lua/5.1/kong/templates/nginx_kong.lua
upstream kong_upstream {server 0.0.0.1;balancer_by_lua_block {Kong.balancer()}# injected nginx_http_upstream_* directives > for _, el in ipairs(nginx_http_upstream_directives) do$(el.name) $(el.value); > end } ###添加log_format日志格式 log_format main 'remote_addr=[$remote_addr] http_x_forward=[$http_x_forwarded_for] time=[$time_local] request=[$request] ''status=[$status] byte=[$bytes_sent] elapsed=[$request_time] upstream_connect_time=[$upstream_connect_time] upstream_header_time=[$upstream_header_time] upstream_response_time=[$upstream_response_time] ' 'refer=[$http_referer] ua=[$http_user_agent] gzip=[$gzip_ratio] ' 'msec=[$msec] http_host=[$http_host] http_accept=[$http_accept|$http_accept_encoding|$http_accept_language]';### server {server_name kong; > for i = 1, #proxy_listeners dolisten $(proxy_listeners[i].listener);修改完上面的配置后,還需要在同一個配置文件中添加兩處配置,見下面操作:
#修改兩處access.log文件配置·····第一處配置·····server {server_name kong; > for i = 1, #proxy_listeners dolisten $(proxy_listeners[i].listener); > enderror_page 400 404 408 411 412 413 414 417 494 /kong_error_handler;error_page 500 502 503 504 /kong_error_handler;access_log ${{PROXY_ACCESS_LOG}} main; ##此行結尾處添加mainerror_log ${{PROXY_ERROR_LOG}} ${{LOG_LEVEL}};client_body_buffer_size ${{CLIENT_BODY_BUFFER_SIZE}};·················· ······第二處配置·····if #admin_listeners > 0 then server {server_name kong_admin; > for i = 1, #admin_listeners dolisten $(admin_listeners[i].listener); > endaccess_log ${{ADMIN_ACCESS_LOG}} main; ##此行結尾處添加mainerror_log ${{ADMIN_ERROR_LOG}} ${{LOG_LEVEL}};······二、重啟kong,查看access.log日志格式
執行下面命令重啟kong
# kong reload /etc/kong/kong.conf
查看日志:
接下來我們就可以對日志進行進一步處理了。
總結
以上是生活随笔為你收集整理的kong 网关日志格式修改的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: jdk LocalDateTime my
- 下一篇: weedfs文件使用记录