You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@apisix.apache.org by we...@apache.org on 2020/11/18 09:51:12 UTC

[apisix] branch master updated: fix(http-logger): correct the Content-Type of newline format log report (#2780)

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

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


The following commit(s) were added to refs/heads/master by this push:
     new e7223e7  fix(http-logger): correct the Content-Type of newline format log report (#2780)
e7223e7 is described below

commit e7223e77a37ee26544926d8a01a1b194137142fb
Author: 罗泽轩 <sp...@gmail.com>
AuthorDate: Wed Nov 18 17:51:03 2020 +0800

    fix(http-logger): correct the Content-Type of newline format log report (#2780)
---
 apisix/plugins/http-logger.lua | 14 +++++++++++++-
 t/lib/server.lua               |  5 +++++
 2 files changed, 18 insertions(+), 1 deletion(-)

diff --git a/apisix/plugins/http-logger.lua b/apisix/plugins/http-logger.lua
index 853ce42..7a79102 100644
--- a/apisix/plugins/http-logger.lua
+++ b/apisix/plugins/http-logger.lua
@@ -115,6 +115,13 @@ local function send_http_data(conf, log_message)
         end
     end
 
+    local content_type
+    if conf.concat_method == "json" then
+        content_type = "application/json"
+    else
+        content_type = "text/plain"
+    end
+
     local httpc_res, httpc_err = httpc:request({
         method = "POST",
         path = url_decoded.path,
@@ -122,7 +129,7 @@ local function send_http_data(conf, log_message)
         body = log_message,
         headers = {
             ["Host"] = url_decoded.host,
-            ["Content-Type"] = "application/json",
+            ["Content-Type"] = content_type,
             ["Authorization"] = conf.auth_header
         }
     })
@@ -217,11 +224,16 @@ function _M.log(conf, ctx)
                 for i, entrie in ipairs(entries) do
                     t[i], err = core.json.encode(entrie)
                     if err then
+                        core.log.warn("failed to encode http log: ", err, ", log data: ", entrie)
                         break
                     end
                 end
                 data = core.table.concat(t, "\n") -- encode as multiple string
             end
+
+        else
+            -- defensive programming check
+            err = "unknown concat_method " .. (conf.concat_method or "nil")
         end
 
         if not data then
diff --git a/t/lib/server.lua b/t/lib/server.lua
index 622e9af..57ea46a 100644
--- a/t/lib/server.lua
+++ b/t/lib/server.lua
@@ -356,6 +356,11 @@ end
 function _M.log()
     ngx.req.read_body()
     local body = ngx.req.get_body_data()
+    local ct = ngx.var.content_type
+    if ct ~= "text/plain" then
+        body = json_decode(body)
+        body = json_encode(body)
+    end
     ngx.log(ngx.WARN, "request log: ", body or "nil")
 end