Skip to content

Commit 0d41d3a

Browse files
mscb402spacewander
andauthored
feat: file logger plugin support response body in variable (#8711)
Co-authored-by: 罗泽轩 <[email protected]> Fixes #8705
1 parent e021266 commit 0d41d3a

File tree

4 files changed

+99
-0
lines changed

4 files changed

+99
-0
lines changed

apisix/core/ctx.lua

+4
Original file line numberDiff line numberDiff line change
@@ -209,6 +209,10 @@ do
209209
balancer_port = true,
210210
consumer_group_id = true,
211211
consumer_name = true,
212+
resp_body = function(ctx)
213+
-- only for logger and requires the logger to have a special configuration
214+
return ctx.resp_body or ''
215+
end,
212216
route_id = true,
213217
route_name = true,
214218
service_id = true,

docs/en/latest/apisix-variable.md

+1
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ additional variables.
4848
| service_id | core | Id of Service. | |
4949
| service_name | core | Name of Service. | |
5050
| redis_cmd_line | Redis | The content of Redis command. | |
51+
| resp_body | core | In the logger plugin, if some of the plugins support logging of response body, for example by configuring `include_resp_body: true`, then this variable can be used in the log format. | |
5152
| rpc_time | xRPC | Time spent at the rpc request level. | |
5253

5354
You can also register your own [variable](./plugin-develop.md#register-custom-variable).

docs/zh/latest/apisix-variable.md

+1
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ APISIX 除了支持 [NGINX 变量](http://nginx.org/en/docs/varindex.html)外,
4747
| service_id | core | APISIX 服务的 ID。 | |
4848
| service_name | core | APISIX 服务的名称。 | |
4949
| redis_cmd_line | Redis | Redis 命令的内容。 | |
50+
| resp_body | core | 在 logger 插件中,如果部分插件支持记录响应的 body 信息,比如配置 `include_resp_body: true`,那可以在 log format 中使用该变量。| |
5051
| rpc_time | xRPC | 在 RPC 请求级别所花费的时间。 | |
5152

5253
当然,除上述变量外,你也可以创建自定义[变量](./plugin-develop.md#register-custom-variable)

t/plugin/file-logger2.t

+93
Original file line numberDiff line numberDiff line change
@@ -181,3 +181,96 @@ contain with target
181181
--- response_body
182182
contain target body hits with expr
183183
skip unconcern body
184+
185+
186+
187+
=== TEST 5: add plugin metadata
188+
--- config
189+
location /t {
190+
content_by_lua_block {
191+
local t = require("lib.test_admin").test
192+
local code, body = t('/apisix/admin/plugin_metadata/file-logger',
193+
ngx.HTTP_PUT,
194+
[[{
195+
"log_format": {
196+
"host": "$host",
197+
"client_ip": "$remote_addr",
198+
"resp_body": "$resp_body"
199+
}
200+
}]]
201+
)
202+
203+
if code >= 300 then
204+
ngx.status = code
205+
end
206+
ngx.say(body)
207+
}
208+
}
209+
--- response_body
210+
passed
211+
212+
213+
214+
=== TEST 6: add plugin
215+
--- config
216+
location /t {
217+
content_by_lua_block {
218+
local t = require("lib.test_admin").test
219+
local code, body = t('/apisix/admin/routes/1',
220+
ngx.HTTP_PUT,
221+
[[{
222+
"plugins": {
223+
"file-logger": {
224+
"path": "file-with-resp-body2.log",
225+
"include_resp_body": true
226+
}
227+
},
228+
"upstream": {
229+
"nodes": {
230+
"127.0.0.1:1982": 1
231+
},
232+
"type": "roundrobin"
233+
},
234+
"uri": "/hello"
235+
}]]
236+
)
237+
238+
if code >= 300 then
239+
ngx.status = code
240+
end
241+
ngx.say(body)
242+
}
243+
}
244+
--- response_body
245+
passed
246+
247+
248+
249+
=== TEST 7: verify plugin
250+
--- config
251+
location /t {
252+
content_by_lua_block {
253+
local core = require("apisix.core")
254+
local t = require("lib.test_admin").test
255+
local code = t("/hello", ngx.HTTP_GET)
256+
local fd, err = io.open("file-with-resp-body2.log", 'r')
257+
local msg
258+
259+
if not fd then
260+
core.log.error("failed to open file: file.log, error info: ", err)
261+
return
262+
end
263+
264+
msg = fd:read()
265+
266+
local new_msg = core.json.decode(msg)
267+
if new_msg.resp_body == 'hello world\n'
268+
then
269+
msg = "write file log success"
270+
ngx.status = code
271+
ngx.say(msg)
272+
end
273+
}
274+
}
275+
--- response_body
276+
write file log success

0 commit comments

Comments
 (0)