From cfc86dc91f46ab5675467b67ffa62ef24f189ed0 Mon Sep 17 00:00:00 2001 From: sshniro Date: Wed, 6 May 2020 07:36:44 +0200 Subject: [PATCH 1/4] Option to include request body in log util --- apisix/utils/log-util.lua | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/apisix/utils/log-util.lua b/apisix/utils/log-util.lua index 6ee03b288db6..c4341522a305 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,11 @@ 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 + log.request.body = ngx.req.get_body_data() + end + end _M.get_full_log = get_full_log From 03a9ac66f4dbf3aaeb7e2e97f236d61fec7f5aa5 Mon Sep 17 00:00:00 2001 From: sshniro Date: Wed, 6 May 2020 08:08:01 +0200 Subject: [PATCH 2/4] Adding return statement --- apisix/utils/log-util.lua | 1 + 1 file changed, 1 insertion(+) diff --git a/apisix/utils/log-util.lua b/apisix/utils/log-util.lua index c4341522a305..e08cb0b473dc 100644 --- a/apisix/utils/log-util.lua +++ b/apisix/utils/log-util.lua @@ -61,6 +61,7 @@ local function get_full_log(ngx, conf) log.request.body = ngx.req.get_body_data() end + return log end _M.get_full_log = get_full_log From 47f7b9bfaffbb037ff1dc905c816a7bdfdc87522 Mon Sep 17 00:00:00 2001 From: sshniro Date: Mon, 18 May 2020 10:04:24 +0200 Subject: [PATCH 3/4] Adding request body file --- apisix/plugins/http-logger.lua | 1 + apisix/plugins/kafka-logger.lua | 1 + apisix/plugins/syslog.lua | 1 + apisix/plugins/tcp-logger.lua | 1 + apisix/plugins/udp-logger.lua | 1 + apisix/utils/log-util.lua | 10 +++++++++- 6 files changed, 14 insertions(+), 1 deletion(-) diff --git a/apisix/plugins/http-logger.lua b/apisix/plugins/http-logger.lua index 523d02d09796..61a1899cdc81 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"} } diff --git a/apisix/plugins/kafka-logger.lua b/apisix/plugins/kafka-logger.lua index e88cf7cac9ef..ad9b8c2629a8 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"} } diff --git a/apisix/plugins/syslog.lua b/apisix/plugins/syslog.lua index f633f3ceb337..f60f12158e4d 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"} } diff --git a/apisix/plugins/tcp-logger.lua b/apisix/plugins/tcp-logger.lua index 197f424dd0d6..6a5560243807 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"} } diff --git a/apisix/plugins/udp-logger.lua b/apisix/plugins/udp-logger.lua index 119d3159d663..be7abc497ab5 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"} } diff --git a/apisix/utils/log-util.lua b/apisix/utils/log-util.lua index e08cb0b473dc..b11a435808f8 100644 --- a/apisix/utils/log-util.lua +++ b/apisix/utils/log-util.lua @@ -58,7 +58,15 @@ local function get_full_log(ngx, conf) } if conf.include_req_body then - log.request.body = ngx.req.get_body_data() + 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 From 1c80ff14e47abd2ee1e2b2ec017bd3505f7acc9b Mon Sep 17 00:00:00 2001 From: sshniro Date: Mon, 18 May 2020 10:29:09 +0200 Subject: [PATCH 4/4] Adding configuration to log util --- apisix/plugins/http-logger.lua | 2 +- apisix/plugins/kafka-logger.lua | 2 +- apisix/plugins/syslog.lua | 2 +- apisix/plugins/tcp-logger.lua | 2 +- apisix/plugins/udp-logger.lua | 2 +- 5 files changed, 5 insertions(+), 5 deletions(-) diff --git a/apisix/plugins/http-logger.lua b/apisix/plugins/http-logger.lua index 61a1899cdc81..44df6aeff99c 100644 --- a/apisix/plugins/http-logger.lua +++ b/apisix/plugins/http-logger.lua @@ -122,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 ad9b8c2629a8..fc7d90cde719 100644 --- a/apisix/plugins/kafka-logger.lua +++ b/apisix/plugins/kafka-logger.lua @@ -112,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 f60f12158e4d..7b96a2e010b6 100644 --- a/apisix/plugins/syslog.lua +++ b/apisix/plugins/syslog.lua @@ -128,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 6a5560243807..ced5f8f23dad 100644 --- a/apisix/plugins/tcp-logger.lua +++ b/apisix/plugins/tcp-logger.lua @@ -116,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 be7abc497ab5..cec782a34762 100644 --- a/apisix/plugins/udp-logger.lua +++ b/apisix/plugins/udp-logger.lua @@ -99,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")