From 3e19f1d300a7065550406fd9b03e6a42a716635b Mon Sep 17 00:00:00 2001 From: mscb402 Date: Thu, 19 Jan 2023 11:29:48 +0800 Subject: [PATCH 01/18] chore: code lint fix --- apisix/plugins/file-logger.lua | 3 + docs/en/latest/plugins/file-logger.md | 8 +-- docs/zh/latest/plugins/file-logger.md | 10 +-- t/plugin/file-logger2.t | 93 +++++++++++++++++++++++++++ 4 files changed, 105 insertions(+), 9 deletions(-) diff --git a/apisix/plugins/file-logger.lua b/apisix/plugins/file-logger.lua index 076a9e7e7592..c335da1d9aa1 100644 --- a/apisix/plugins/file-logger.lua +++ b/apisix/plugins/file-logger.lua @@ -59,6 +59,9 @@ local _M = { metadata_schema = metadata_schema } +core.ctx.register_var("resp_body", function(ctx) + return ctx.resp_body or '' +end) function _M.check_schema(conf, schema_type) if schema_type == core.schema.TYPE_METADATA then diff --git a/docs/en/latest/plugins/file-logger.md b/docs/en/latest/plugins/file-logger.md index 1f2c94b35efc..9cdb9f7cc9cd 100644 --- a/docs/en/latest/plugins/file-logger.md +++ b/docs/en/latest/plugins/file-logger.md @@ -43,10 +43,10 @@ The `file-logger` Plugin is used to push log streams to a specific location. ## Attributes -| Name | Type | Required | Description | -| ---- | ------ | -------- | ------------- | -| path | string | True | Log file path. | -| include_resp_body | boolean | False | When set to `true` includes the response body in the log file. | +| Name | Type | Required | Description | +| ---- | ------ | -------- |------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------| +| path | string | True | Log file path. | +| include_resp_body | boolean | False | When set to `true` includes the response body in the log file. If format is set, you can use the variable $resp_body to get the response body. | | include_resp_body_expr | array | False | When the `include_resp_body` attribute is set to `true`, use this to filter based on [lua-resty-expr](https://github.com/api7/lua-resty-expr). If present, only logs the response into file if the expression evaluates to `true`. | ## Metadata diff --git a/docs/zh/latest/plugins/file-logger.md b/docs/zh/latest/plugins/file-logger.md index 2677ee6a0b37..449a76eb81be 100644 --- a/docs/zh/latest/plugins/file-logger.md +++ b/docs/zh/latest/plugins/file-logger.md @@ -45,11 +45,11 @@ description: API 网关 Apache APISIX file-logger 插件可用于将日志数据 ## 属性 -| 名称 | 类型 | 必选项 | 描述 | -| ---------------- | ------- | ------ | ------------------------------------------------ | -| path | string | 是 | 自定义输出文件路径。例如:`logs/file.log`。 | -| include_resp_body | boolean | 否 | 当设置为 `true` 时,生成的文件包含响应体。 | -| include_resp_body_expr | array | 否 | 当 `include_resp_body` 属性设置为 `true` 时,使用该属性并基于 [lua-resty-expr](https://github.com/api7/lua-resty-expr) 进行过滤。 如果存在,则仅在表达式计算结果为 `true` 时记录响应。 | +| 名称 | 类型 | 必选项 | 描述 | +| ---------------- | ------- | ------ |--------------------------------------------------------------------------------------------------------------------------------------------| +| path | string | 是 | 自定义输出文件路径。例如:`logs/file.log`。 | +| include_resp_body | boolean | 否 | 当设置为 `true` 时,生成的文件包含响应体。如果设置了 format ,可以使用 $resp_body 的变量来获取响应体。 | +| include_resp_body_expr | array | 否 | 当 `include_resp_body` 属性设置为 `true` 时,使用该属性并基于 [lua-resty-expr](https://github.com/api7/lua-resty-expr) 进行过滤。 如果存在,则仅在表达式计算结果为 `true` 时记录响应。 | ## 插件元数据设置 diff --git a/t/plugin/file-logger2.t b/t/plugin/file-logger2.t index 11d5851634d6..4bef2cf09e37 100644 --- a/t/plugin/file-logger2.t +++ b/t/plugin/file-logger2.t @@ -181,3 +181,96 @@ contain with target --- response_body contain target body hits with expr skip unconcern body + + + +=== TEST 5: add plugin metadata +--- config + location /t { + content_by_lua_block { + local t = require("lib.test_admin").test + local code, body = t('/apisix/admin/plugin_metadata/file-logger', + ngx.HTTP_PUT, + [[{ + "log_format": { + "resp_body": "$resp_body" + } + }]] + ) + + if code >= 300 then + ngx.status = code + end + ngx.say(body) + } + } +--- response_body +passed + + + +=== TEST 6: add plugin with 'include_resp_body' setting +--- config + location /t { + content_by_lua_block { + local t = require("lib.test_admin").test + -- delete plugin metadata for response body format + t('/apisix/admin/plugin_metadata/file-logger', ngx.HTTP_DELETE) + + local code, body = t('/apisix/admin/routes/1', + ngx.HTTP_PUT, + [[{ + "plugins": { + "file-logger": { + "path": "file-with-resp-body-2.log", + "include_resp_body": true + } + }, + "upstream": { + "nodes": { + "127.0.0.1:1982": 1 + }, + "type": "roundrobin" + }, + "uri": "/hello" + }]] + ) + + if code >= 300 then + ngx.status = code + end + ngx.say(body) + } + } + + + +=== TEST 7: verify plugin for file-logger with response +--- config + location /t { + content_by_lua_block { + local core = require("apisix.core") + local t = require("lib.test_admin").test + local code = t("/hello", ngx.HTTP_GET) + local fd, err = io.open("file-with-resp-body-2.log", 'r') + local msg + + if not fd then + core.log.error("failed to open file: file-resp-check.log, error info: ", err) + return + end + + -- note only for first line + msg = fd:read() + + local new_msg = core.json.decode(msg) + ngx.status = code + + if new_msg.resp_body ~= nil and new_msg.resp_body == "hello world\n" then + ngx.status = code + ngx.say('contain with target') + end + } + } +--- response_body +contain with target From 04f69bc130ea504699fec455faf409d50f77a2c2 Mon Sep 17 00:00:00 2001 From: mscb402 Date: Thu, 19 Jan 2023 13:39:13 +0800 Subject: [PATCH 02/18] chore: modify test --- t/plugin/file-logger2.t | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/t/plugin/file-logger2.t b/t/plugin/file-logger2.t index 4bef2cf09e37..fc90446d0f1a 100644 --- a/t/plugin/file-logger2.t +++ b/t/plugin/file-logger2.t @@ -266,11 +266,8 @@ passed local new_msg = core.json.decode(msg) ngx.status = code - if new_msg.resp_body ~= nil and new_msg.resp_body == "hello world\n" then - ngx.status = code - ngx.say('contain with target') - end + ngx.say(new_msg.resp_body) } } --- response_body -contain with target +hello world From 667dd96c9464563f12aa875c5ab04d2c0fbe0f48 Mon Sep 17 00:00:00 2001 From: mscb402 Date: Thu, 19 Jan 2023 17:20:44 +0800 Subject: [PATCH 03/18] chore: modify test --- t/plugin/file-logger2.t | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/t/plugin/file-logger2.t b/t/plugin/file-logger2.t index fc90446d0f1a..7d101cc1ada1 100644 --- a/t/plugin/file-logger2.t +++ b/t/plugin/file-logger2.t @@ -263,10 +263,7 @@ passed -- note only for first line msg = fd:read() - local new_msg = core.json.decode(msg) - ngx.status = code - - ngx.say(new_msg.resp_body) + ngx.say(msg) } } --- response_body From e195c71505db0e1829d7d048c9fe4d4445eed8a1 Mon Sep 17 00:00:00 2001 From: mscb402 Date: Sat, 28 Jan 2023 12:01:56 +0800 Subject: [PATCH 04/18] chore: fix file logger e2e error --- docs/en/latest/plugins/file-logger.md | 2 +- docs/zh/latest/plugins/file-logger.md | 2 +- t/plugin/file-logger2.t | 87 ------------------ t/plugin/file-logger3.t | 124 ++++++++++++++++++++++++++ 4 files changed, 126 insertions(+), 89 deletions(-) create mode 100644 t/plugin/file-logger3.t diff --git a/docs/en/latest/plugins/file-logger.md b/docs/en/latest/plugins/file-logger.md index 9cdb9f7cc9cd..1002af0dee11 100644 --- a/docs/en/latest/plugins/file-logger.md +++ b/docs/en/latest/plugins/file-logger.md @@ -44,7 +44,7 @@ The `file-logger` Plugin is used to push log streams to a specific location. ## Attributes | Name | Type | Required | Description | -| ---- | ------ | -------- |------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------| +| ---- | ------ | -------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ | | path | string | True | Log file path. | | include_resp_body | boolean | False | When set to `true` includes the response body in the log file. If format is set, you can use the variable $resp_body to get the response body. | | include_resp_body_expr | array | False | When the `include_resp_body` attribute is set to `true`, use this to filter based on [lua-resty-expr](https://github.com/api7/lua-resty-expr). If present, only logs the response into file if the expression evaluates to `true`. | diff --git a/docs/zh/latest/plugins/file-logger.md b/docs/zh/latest/plugins/file-logger.md index 449a76eb81be..cb8dad29ade4 100644 --- a/docs/zh/latest/plugins/file-logger.md +++ b/docs/zh/latest/plugins/file-logger.md @@ -46,7 +46,7 @@ description: API 网关 Apache APISIX file-logger 插件可用于将日志数据 ## 属性 | 名称 | 类型 | 必选项 | 描述 | -| ---------------- | ------- | ------ |--------------------------------------------------------------------------------------------------------------------------------------------| +| ---------------- | ------- | ------ | -------------------------------------------------------------------------------------------------------------------------------------------- | | path | string | 是 | 自定义输出文件路径。例如:`logs/file.log`。 | | include_resp_body | boolean | 否 | 当设置为 `true` 时,生成的文件包含响应体。如果设置了 format ,可以使用 $resp_body 的变量来获取响应体。 | | include_resp_body_expr | array | 否 | 当 `include_resp_body` 属性设置为 `true` 时,使用该属性并基于 [lua-resty-expr](https://github.com/api7/lua-resty-expr) 进行过滤。 如果存在,则仅在表达式计算结果为 `true` 时记录响应。 | diff --git a/t/plugin/file-logger2.t b/t/plugin/file-logger2.t index 7d101cc1ada1..11d5851634d6 100644 --- a/t/plugin/file-logger2.t +++ b/t/plugin/file-logger2.t @@ -181,90 +181,3 @@ contain with target --- response_body contain target body hits with expr skip unconcern body - - - -=== TEST 5: add plugin metadata ---- config - location /t { - content_by_lua_block { - local t = require("lib.test_admin").test - local code, body = t('/apisix/admin/plugin_metadata/file-logger', - ngx.HTTP_PUT, - [[{ - "log_format": { - "resp_body": "$resp_body" - } - }]] - ) - - if code >= 300 then - ngx.status = code - end - ngx.say(body) - } - } ---- response_body -passed - - - -=== TEST 6: add plugin with 'include_resp_body' setting ---- config - location /t { - content_by_lua_block { - local t = require("lib.test_admin").test - -- delete plugin metadata for response body format - t('/apisix/admin/plugin_metadata/file-logger', ngx.HTTP_DELETE) - - local code, body = t('/apisix/admin/routes/1', - ngx.HTTP_PUT, - [[{ - "plugins": { - "file-logger": { - "path": "file-with-resp-body-2.log", - "include_resp_body": true - } - }, - "upstream": { - "nodes": { - "127.0.0.1:1982": 1 - }, - "type": "roundrobin" - }, - "uri": "/hello" - }]] - ) - - if code >= 300 then - ngx.status = code - end - ngx.say(body) - } - } - - - -=== TEST 7: verify plugin for file-logger with response ---- config - location /t { - content_by_lua_block { - local core = require("apisix.core") - local t = require("lib.test_admin").test - local code = t("/hello", ngx.HTTP_GET) - local fd, err = io.open("file-with-resp-body-2.log", 'r') - local msg - - if not fd then - core.log.error("failed to open file: file-resp-check.log, error info: ", err) - return - end - - -- note only for first line - msg = fd:read() - - ngx.say(msg) - } - } ---- response_body -hello world diff --git a/t/plugin/file-logger3.t b/t/plugin/file-logger3.t new file mode 100644 index 000000000000..a2f609fe5ec8 --- /dev/null +++ b/t/plugin/file-logger3.t @@ -0,0 +1,124 @@ +# +# Licensed to the Apache Software Foundation (ASF) under one or more +# contributor license agreements. See the NOTICE file distributed with +# this work for additional information regarding copyright ownership. +# The ASF licenses this file to You under the Apache License, Version 2.0 +# (the "License"); you may not use this file except in compliance with +# the License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# +use t::APISIX 'no_plan'; + +no_long_string(); +no_root_location(); + +add_block_preprocessor(sub { + my ($block) = @_; + + if (! $block->request) { + $block->set_value("request", "GET /t"); + } +}); + + +run_tests; + +__DATA__ + +=== TEST 1: add plugin metadata +--- config + location /t { + content_by_lua_block { + local t = require("lib.test_admin").test + local code, body = t('/apisix/admin/plugin_metadata/file-logger', + ngx.HTTP_PUT, + [[{ + "log_format": { + "host": "$host", + "client_ip": "$remote_addr", + "resp_body": "$resp_body" + } + }]] + ) + + if code >= 300 then + ngx.status = code + end + ngx.say(body) + } + } +--- response_body +passed + + + +=== TEST 2: add plugin +--- config + location /t { + content_by_lua_block { + local t = require("lib.test_admin").test + local code, body = t('/apisix/admin/routes/1', + ngx.HTTP_PUT, + [[{ + "plugins": { + "file-logger": { + "path": "file-with-resp-body.log", + "include_resp_body": true + } + }, + "upstream": { + "nodes": { + "127.0.0.1:1982": 1 + }, + "type": "roundrobin" + }, + "uri": "/hello" + }]] + ) + + if code >= 300 then + ngx.status = code + end + ngx.say(body) + } + } +--- response_body +passed + + + +=== TEST 3: verify plugin +--- config + location /t { + content_by_lua_block { + local core = require("apisix.core") + local t = require("lib.test_admin").test + local code = t("/hello", ngx.HTTP_GET) + local fd, err = io.open("file-with-resp-body.log", 'r') + local msg + + if not fd then + core.log.error("failed to open file: file.log, error info: ", err) + return + end + + msg = fd:read() + + local new_msg = core.json.decode(msg) + if new_msg.resp_body == 'hello world\n' + then + msg = "write file log success" + ngx.status = code + ngx.say(msg) + end + } + } +--- response_body +write file log success From 184797112b016b901d75a5439701a9bfb1c021e8 Mon Sep 17 00:00:00 2001 From: mscb402 Date: Sat, 28 Jan 2023 13:29:04 +0800 Subject: [PATCH 05/18] fix: log file conflicts --- docs/en/latest/plugins/file-logger.md | 4 ++-- docs/zh/latest/plugins/file-logger.md | 2 +- t/plugin/file-logger3.t | 4 ++-- 3 files changed, 5 insertions(+), 5 deletions(-) diff --git a/docs/en/latest/plugins/file-logger.md b/docs/en/latest/plugins/file-logger.md index 1002af0dee11..ae827578b972 100644 --- a/docs/en/latest/plugins/file-logger.md +++ b/docs/en/latest/plugins/file-logger.md @@ -44,8 +44,8 @@ The `file-logger` Plugin is used to push log streams to a specific location. ## Attributes | Name | Type | Required | Description | -| ---- | ------ | -------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ | -| path | string | True | Log file path. | +| ---- | ------ | -------- |------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------| +| path | string | True | Log file path. | | include_resp_body | boolean | False | When set to `true` includes the response body in the log file. If format is set, you can use the variable $resp_body to get the response body. | | include_resp_body_expr | array | False | When the `include_resp_body` attribute is set to `true`, use this to filter based on [lua-resty-expr](https://github.com/api7/lua-resty-expr). If present, only logs the response into file if the expression evaluates to `true`. | diff --git a/docs/zh/latest/plugins/file-logger.md b/docs/zh/latest/plugins/file-logger.md index cb8dad29ade4..c424b84b22d4 100644 --- a/docs/zh/latest/plugins/file-logger.md +++ b/docs/zh/latest/plugins/file-logger.md @@ -47,7 +47,7 @@ description: API 网关 Apache APISIX file-logger 插件可用于将日志数据 | 名称 | 类型 | 必选项 | 描述 | | ---------------- | ------- | ------ | -------------------------------------------------------------------------------------------------------------------------------------------- | -| path | string | 是 | 自定义输出文件路径。例如:`logs/file.log`。 | +| path | string | 是 | 自定义输出文件路径。例如:`logs/file.log`。 | | include_resp_body | boolean | 否 | 当设置为 `true` 时,生成的文件包含响应体。如果设置了 format ,可以使用 $resp_body 的变量来获取响应体。 | | include_resp_body_expr | array | 否 | 当 `include_resp_body` 属性设置为 `true` 时,使用该属性并基于 [lua-resty-expr](https://github.com/api7/lua-resty-expr) 进行过滤。 如果存在,则仅在表达式计算结果为 `true` 时记录响应。 | diff --git a/t/plugin/file-logger3.t b/t/plugin/file-logger3.t index a2f609fe5ec8..d1dc240247f9 100644 --- a/t/plugin/file-logger3.t +++ b/t/plugin/file-logger3.t @@ -69,7 +69,7 @@ passed [[{ "plugins": { "file-logger": { - "path": "file-with-resp-body.log", + "path": "file-with-resp-body2.log", "include_resp_body": true } }, @@ -101,7 +101,7 @@ passed local core = require("apisix.core") local t = require("lib.test_admin").test local code = t("/hello", ngx.HTTP_GET) - local fd, err = io.open("file-with-resp-body.log", 'r') + local fd, err = io.open("file-with-resp-body2.log", 'r') local msg if not fd then From 9a98eedebbb121b5266e5b593236eb7f8efaf31e Mon Sep 17 00:00:00 2001 From: mscb402 Date: Sun, 29 Jan 2023 16:43:30 +0800 Subject: [PATCH 06/18] fix: move file-logger3.t to file-logger2.t --- t/plugin/file-logger2.t | 93 ++++++++++++++++++++++++++++++ t/plugin/file-logger3.t | 124 ---------------------------------------- 2 files changed, 93 insertions(+), 124 deletions(-) delete mode 100644 t/plugin/file-logger3.t diff --git a/t/plugin/file-logger2.t b/t/plugin/file-logger2.t index 11d5851634d6..90ce001c64c0 100644 --- a/t/plugin/file-logger2.t +++ b/t/plugin/file-logger2.t @@ -181,3 +181,96 @@ contain with target --- response_body contain target body hits with expr skip unconcern body + + + +=== TEST 5: add plugin metadata +--- config + location /t { + content_by_lua_block { + local t = require("lib.test_admin").test + local code, body = t('/apisix/admin/plugin_metadata/file-logger', + ngx.HTTP_PUT, + [[{ + "log_format": { + "host": "$host", + "client_ip": "$remote_addr", + "resp_body": "$resp_body" + } + }]] + ) + + if code >= 300 then + ngx.status = code + end + ngx.say(body) + } + } +--- response_body +passed + + + +=== TEST 6: add plugin +--- config + location /t { + content_by_lua_block { + local t = require("lib.test_admin").test + local code, body = t('/apisix/admin/routes/1', + ngx.HTTP_PUT, + [[{ + "plugins": { + "file-logger": { + "path": "file-with-resp-body2.log", + "include_resp_body": true + } + }, + "upstream": { + "nodes": { + "127.0.0.1:1982": 1 + }, + "type": "roundrobin" + }, + "uri": "/hello" + }]] + ) + + if code >= 300 then + ngx.status = code + end + ngx.say(body) + } + } +--- response_body +passed + + + +=== TEST 7: verify plugin +--- config + location /t { + content_by_lua_block { + local core = require("apisix.core") + local t = require("lib.test_admin").test + local code = t("/hello", ngx.HTTP_GET) + local fd, err = io.open("file-with-resp-body2.log", 'r') + local msg + + if not fd then + core.log.error("failed to open file: file.log, error info: ", err) + return + end + + msg = fd:read() + + local new_msg = core.json.decode(msg) + if new_msg.resp_body == 'hello world\n' + then + msg = "write file log success" + ngx.status = code + ngx.say(msg) + end + } + } +--- response_body +write file log success diff --git a/t/plugin/file-logger3.t b/t/plugin/file-logger3.t deleted file mode 100644 index d1dc240247f9..000000000000 --- a/t/plugin/file-logger3.t +++ /dev/null @@ -1,124 +0,0 @@ -# -# Licensed to the Apache Software Foundation (ASF) under one or more -# contributor license agreements. See the NOTICE file distributed with -# this work for additional information regarding copyright ownership. -# The ASF licenses this file to You under the Apache License, Version 2.0 -# (the "License"); you may not use this file except in compliance with -# the License. You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# -use t::APISIX 'no_plan'; - -no_long_string(); -no_root_location(); - -add_block_preprocessor(sub { - my ($block) = @_; - - if (! $block->request) { - $block->set_value("request", "GET /t"); - } -}); - - -run_tests; - -__DATA__ - -=== TEST 1: add plugin metadata ---- config - location /t { - content_by_lua_block { - local t = require("lib.test_admin").test - local code, body = t('/apisix/admin/plugin_metadata/file-logger', - ngx.HTTP_PUT, - [[{ - "log_format": { - "host": "$host", - "client_ip": "$remote_addr", - "resp_body": "$resp_body" - } - }]] - ) - - if code >= 300 then - ngx.status = code - end - ngx.say(body) - } - } ---- response_body -passed - - - -=== TEST 2: add plugin ---- config - location /t { - content_by_lua_block { - local t = require("lib.test_admin").test - local code, body = t('/apisix/admin/routes/1', - ngx.HTTP_PUT, - [[{ - "plugins": { - "file-logger": { - "path": "file-with-resp-body2.log", - "include_resp_body": true - } - }, - "upstream": { - "nodes": { - "127.0.0.1:1982": 1 - }, - "type": "roundrobin" - }, - "uri": "/hello" - }]] - ) - - if code >= 300 then - ngx.status = code - end - ngx.say(body) - } - } ---- response_body -passed - - - -=== TEST 3: verify plugin ---- config - location /t { - content_by_lua_block { - local core = require("apisix.core") - local t = require("lib.test_admin").test - local code = t("/hello", ngx.HTTP_GET) - local fd, err = io.open("file-with-resp-body2.log", 'r') - local msg - - if not fd then - core.log.error("failed to open file: file.log, error info: ", err) - return - end - - msg = fd:read() - - local new_msg = core.json.decode(msg) - if new_msg.resp_body == 'hello world\n' - then - msg = "write file log success" - ngx.status = code - ngx.say(msg) - end - } - } ---- response_body -write file log success From 3aeb37daddafbdfb0aa0ae1da37a664ef3f5145f Mon Sep 17 00:00:00 2001 From: mscb402 Date: Mon, 30 Jan 2023 09:24:09 +0800 Subject: [PATCH 07/18] fix: move var to ctx.lua --- apisix/core/ctx.lua | 4 ++++ apisix/plugins/file-logger.lua | 3 --- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/apisix/core/ctx.lua b/apisix/core/ctx.lua index b0210259fad3..d353109b11f7 100644 --- a/apisix/core/ctx.lua +++ b/apisix/core/ctx.lua @@ -213,6 +213,10 @@ do route_name = true, service_id = true, service_name = true, + resp_body = function(ctx) + -- only work when file-logger set include_resp_body = true + return ctx.resp_body or '' + end } local mt = { diff --git a/apisix/plugins/file-logger.lua b/apisix/plugins/file-logger.lua index c335da1d9aa1..076a9e7e7592 100644 --- a/apisix/plugins/file-logger.lua +++ b/apisix/plugins/file-logger.lua @@ -59,9 +59,6 @@ local _M = { metadata_schema = metadata_schema } -core.ctx.register_var("resp_body", function(ctx) - return ctx.resp_body or '' -end) function _M.check_schema(conf, schema_type) if schema_type == core.schema.TYPE_METADATA then From 823b709d068f3bc0dfaf12db7186723f0cfd1a7e Mon Sep 17 00:00:00 2001 From: mscb402 Date: Tue, 31 Jan 2023 15:25:09 +0800 Subject: [PATCH 08/18] chore: update doc --- apisix/core/ctx.lua | 10 +++++----- docs/en/latest/plugins/file-logger.md | 16 ++++++++-------- docs/zh/latest/plugins/file-logger.md | 18 +++++++++--------- 3 files changed, 22 insertions(+), 22 deletions(-) diff --git a/apisix/core/ctx.lua b/apisix/core/ctx.lua index d353109b11f7..850c29c52c6f 100644 --- a/apisix/core/ctx.lua +++ b/apisix/core/ctx.lua @@ -209,14 +209,14 @@ do balancer_port = true, consumer_group_id = true, consumer_name = true, + resp_body = function(ctx) + -- only for logger and requires the logger to have a special configuration + return ctx.resp_body or '' + end, route_id = true, route_name = true, service_id = true, - service_name = true, - resp_body = function(ctx) - -- only work when file-logger set include_resp_body = true - return ctx.resp_body or '' - end + service_name = true } local mt = { diff --git a/docs/en/latest/plugins/file-logger.md b/docs/en/latest/plugins/file-logger.md index ae827578b972..926df62ad1cb 100644 --- a/docs/en/latest/plugins/file-logger.md +++ b/docs/en/latest/plugins/file-logger.md @@ -1,11 +1,11 @@ --- title: file-logger keywords: - - APISIX - - API Gateway - - Plugin - - File Logger -description: This document contains information about the Apache APISIX file-logger Plugin. +- APISIX +- API Gateway +- Plugin +- File Logger + description: This document contains information about the Apache APISIX file-logger Plugin. ---