You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@apisix.apache.org by me...@apache.org on 2020/05/29 08:15:27 UTC

[incubator-apisix] branch master updated: feature: add option to include request body in log util (#1545)

This is an automated email from the ASF dual-hosted git repository.

membphis pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/incubator-apisix.git


The following commit(s) were added to refs/heads/master by this push:
     new 39ef6fb  feature: add option to include request body in log util (#1545)
39ef6fb is described below

commit 39ef6fb2eb944be5b4f6784573cd61a758ac3975
Author: Nirojan Selvanathan <ss...@gmail.com>
AuthorDate: Fri May 29 10:15:16 2020 +0200

    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 523d02d..44df6ae 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 e88cf7c..fc7d90c 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 f633f3c..7b96a2e 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 197f424..ced5f8f 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 119d315..cec782a 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 6ee03b2..b11a435 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