You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@apisix.apache.org by GitBox <gi...@apache.org> on 2021/06/29 03:12:53 UTC

[GitHub] [apisix] spacewander commented on a change in pull request #4483: feat(kafka-logger): support for specified the log formats via admin API.

spacewander commented on a change in pull request #4483:
URL: https://github.com/apache/apisix/pull/4483#discussion_r660248462



##########
File path: apisix/utils/log-util.lua
##########
@@ -17,10 +17,58 @@
 local core = require("apisix.core")
 local ngx  = ngx
 local pairs = pairs
+local str_byte = string.byte
 local req_get_body_data = ngx.req.get_body_data
 
+local lru_log_format = core.lrucache.new({
+    ttl = 300, count = 512
+})
+
 local _M = {}
+_M.metadata_schema_log_format = {
+    type = "object",
+    default = {
+        ["host"] = "$host",
+        ["@timestamp"] = "$time_iso8601",
+        ["client_ip"] = "$remote_addr",
+    },
+}
+
+
+local function gen_log_format(format)
+    local log_format = {}
+    for k, var_name in pairs(format) do
+        if var_name:byte(1, 1) == str_byte("$") then
+            log_format[k] = {true, var_name:sub(2)}
+        else
+            log_format[k] = {false, var_name}
+        end
+    end
+    core.log.info("log_format: ", core.json.delay_encode(log_format))
+    return log_format
+end
+
+local function get_custom_format_log(format)

Review comment:
       Better to pass ctx as an argument

##########
File path: apisix/plugins/http-logger.lua
##########
@@ -198,23 +168,10 @@ function _M.log(conf, ctx)
     core.log.info("metadata: ", core.json.delay_encode(metadata))
 
     local entry
-    local log_format = lru_log_format(metadata or "", nil, gen_log_format,
-                                      metadata)
-    if core.table.nkeys(log_format) > 0 then
-        entry = core.table.new(0, core.table.nkeys(log_format))
-        for k, var_attr in pairs(log_format) do
-            if var_attr[1] then
-                entry[k] = ctx.var[var_attr[2]]
-            else
-                entry[k] = var_attr[2]
-            end
-        end
 
-        local matched_route = ctx.matched_route and ctx.matched_route.value
-        if matched_route then
-            entry.service_id = matched_route.service_id
-            entry.route_id = matched_route.id
-        end
+    if metadata and metadata.value.log_format
+        and core.table.nkeys(metadata.value.log_format) > 0 then

Review comment:
       ```suggestion
         and core.table.nkeys(metadata.value.log_format) > 0
       then
   ```

##########
File path: apisix/plugins/kafka-logger.lua
##########
@@ -152,8 +166,16 @@ function _M.log(conf, ctx)
         -- core.log.info("origin entry: ", entry)
 
     else
-        entry = log_util.get_full_log(ngx, conf)
-        core.log.info("full log entry: ", core.json.delay_encode(entry))
+        local metadata = plugin.plugin_metadata(plugin_name)
+        core.log.info("metadata: ", core.json.delay_encode(metadata))
+        if metadata and metadata.value.log_format
+            and core.table.nkeys(metadata.value.log_format) > 0 then

Review comment:
       ```suggestion
             and core.table.nkeys(metadata.value.log_format) > 0
           then
   ```




-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscribe@apisix.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org