From 39ef6fb2eb944be5b4f6784573cd61a758ac3975 Mon Sep 17 00:00:00 2001 From: Nirojan Selvanathan Date: Fri, 29 May 2020 10:15:16 +0200 Subject: [PATCH] feature: add option to include request body in log util (#1545) --- apisix/plugins/http-logger.lua | 3 ++- apisix/plugins/kafka-logger.lua | 3 ++- apisix/plugins/syslog.lua | 3 ++- apisix/plugins/tcp-logger.lua | 3 ++- apisix/plugins/udp-logger.lua | 3 ++- apisix/utils/log-util.lua | 18 ++++++++++++++++-- 6 files changed, 26 insertions(+), 7 deletions(-) diff --git a/apisix/plugins/http-logger.lua b/apisix/plugins/http-logger.lua index 523d02d0..44df6aef 100644 --- a/apisix/plugins/http-logger.lua +++ b/apisix/plugins/http-logger.lua @@ -36,6 +36,7 @@ local schema = { buffer_duration = {type = "integer", minimum = 1, default = 60}, inactive_timeout = {type = "integer", minimum = 1, default = 5}, batch_max_size = {type = "integer", minimum = 1, default = 1000}, + include_req_body = {type = "boolean", default = false} }, required = {"uri"} } @@ -121,7 +122,7 @@ end function _M.log(conf) - local entry = log_util.get_full_log(ngx) + local entry = log_util.get_full_log(ngx, conf) if not entry.route_id then core.log.error("failed to obtain the route id for http logger") diff --git a/apisix/plugins/kafka-logger.lua b/apisix/plugins/kafka-logger.lua index e88cf7ca..fc7d90cd 100644 --- a/apisix/plugins/kafka-logger.lua +++ b/apisix/plugins/kafka-logger.lua @@ -44,6 +44,7 @@ local schema = { buffer_duration = {type = "integer", minimum = 1, default = 60}, inactive_timeout = {type = "integer", minimum = 1, default = 5}, batch_max_size = {type = "integer", minimum = 1, default = 1000}, + include_req_body = {type = "boolean", default = false} }, required = {"broker_list", "kafka_topic", "key"} } @@ -111,7 +112,7 @@ end function _M.log(conf) - local entry = log_util.get_full_log(ngx) + local entry = log_util.get_full_log(ngx, conf) if not entry.route_id then core.log.error("failed to obtain the route id for kafka logger") diff --git a/apisix/plugins/syslog.lua b/apisix/plugins/syslog.lua index f633f3ce..7b96a2e0 100644 --- a/apisix/plugins/syslog.lua +++ b/apisix/plugins/syslog.lua @@ -42,6 +42,7 @@ local schema = { tls = {type = "boolean", default = false}, batch_max_size = {type = "integer", minimum = 1, default = 1000}, buffer_duration = {type = "integer", minimum = 1, default = 60}, + include_req_body = {type = "boolean", default = false} }, required = {"host", "port"} } @@ -127,7 +128,7 @@ end -- log phase in APISIX function _M.log(conf) - local entry = log_util.get_full_log(ngx) + local entry = log_util.get_full_log(ngx, conf) if not entry.route_id then core.log.error("failed to obtain the route id for sys logger") diff --git a/apisix/plugins/tcp-logger.lua b/apisix/plugins/tcp-logger.lua index 197f424d..ced5f8f2 100644 --- a/apisix/plugins/tcp-logger.lua +++ b/apisix/plugins/tcp-logger.lua @@ -40,6 +40,7 @@ local schema = { buffer_duration = {type = "integer", minimum = 1, default = 60}, inactive_timeout = {type = "integer", minimum = 1, default = 5}, batch_max_size = {type = "integer", minimum = 1, default = 1000}, + include_req_body = {type = "boolean", default = false} }, required = {"host", "port"} } @@ -115,7 +116,7 @@ end function _M.log(conf) - local entry = log_util.get_full_log(ngx) + local entry = log_util.get_full_log(ngx, conf) if not entry.route_id then core.log.error("failed to obtain the route id for tcp logger") diff --git a/apisix/plugins/udp-logger.lua b/apisix/plugins/udp-logger.lua index 119d3159..cec782a3 100644 --- a/apisix/plugins/udp-logger.lua +++ b/apisix/plugins/udp-logger.lua @@ -36,6 +36,7 @@ local schema = { buffer_duration = {type = "integer", minimum = 1, default = 60}, inactive_timeout = {type = "integer", minimum = 1, default = 5}, batch_max_size = {type = "integer", minimum = 1, default = 1000}, + include_req_body = {type = "boolean", default = false} }, required = {"host", "port"} } @@ -98,7 +99,7 @@ end function _M.log(conf) - local entry = log_util.get_full_log(ngx) + local entry = log_util.get_full_log(ngx, conf) if not entry.route_id then core.log.error("failed to obtain the route id for udp logger") diff --git a/apisix/utils/log-util.lua b/apisix/utils/log-util.lua index 6ee03b28..b11a4358 100644 --- a/apisix/utils/log-util.lua +++ b/apisix/utils/log-util.lua @@ -18,7 +18,7 @@ local core = require("apisix.core") local _M = {} -local function get_full_log(ngx) +local function get_full_log(ngx, conf) local ctx = ngx.ctx.api_ctx local var = ctx.var local service_id @@ -34,7 +34,7 @@ local function get_full_log(ngx) service_id = var.host end - return { + local log = { request = { url = url, uri = var.request_uri, @@ -56,6 +56,20 @@ local function get_full_log(ngx) start_time = ngx.req.start_time() * 1000, latency = (ngx.now() - ngx.req.start_time()) * 1000 } + + if conf.include_req_body then + local body = ngx.req.get_body_data() + if body then + log.request.body = body + else + local body_file = ngx.req.get_body_file() + if body_file then + log.request.body_file = body_file + end + end + end + + return log end _M.get_full_log = get_full_log