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
```
##########
File path: docs/zh/latest/plugins/http-logger.md
##########
@@ -89,7 +89,7 @@ hello, world
| 名称 | 类型 | 必选项 | 默认值 | 有效值 | 描述
|
| ---------------- | ------- | ------ | ------------- | ------- |
------------------------------------------------ |
-| log_format | object | 可选 | {"host": "$host", "@timestamp":
"$time_iso8601", "client_ip": "$remote_addr"} | | 以 JSON 对象方式声明日志格式。对
value 部分,仅支持字符串。如果是以 `$` 开头,则表明是要获取 __APISIX__ 变量或 [Nginx
内置变量](http://nginx.org/en/docs/varindex.html)。特别的,该设置是全局生效的,意味着指定 log_format
后,将对所有绑定 http-logger 的 Route 或 Service 生效。 |
+| log_format | object | 可选 | {"host": "$host", "@timestamp":
"$time_iso8601", "client_ip": "$remote_addr"} | | 以 JSON 对象方式声明日志格式。对
value 部分,仅支持字符串。如果是以 `$` 开头,则表明是要获取 __APISIX__ 变量或 [Nginx
内置变量](http://nginx.org/en/docs/varindex.html)。特别的,**该设置是全局生效的**,意味着指定
log_format 后,将对所有绑定 http-logger 的 Route 或 Service 生效。 |
Review comment:
Could you also update the English version of doc? You can use an online
translator if you need help. Thanks!
--
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: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]