Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: file logger plugin support response body in variable #8711

Merged
Merged
4 changes: 4 additions & 0 deletions apisix/core/ctx.lua
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,10 @@ 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,
Expand Down
1 change: 1 addition & 0 deletions docs/en/latest/apisix-variable.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ additional variables.
| service_id | core | Id of Service. | |
| service_name | core | Name of Service. | |
| redis_cmd_line | Redis | The content of Redis command. | |
| 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. | |
| rpc_time | xRPC | Time spent at the rpc request level. | |

You can also register your own [variable](./plugin-develop.md#register-custom-variable).
1 change: 1 addition & 0 deletions docs/zh/latest/apisix-variable.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ APISIX 除了支持 [NGINX 变量](http://nginx.org/en/docs/varindex.html)外,
| service_id | core | APISIX 服务的 ID。 | |
| service_name | core | APISIX 服务的名称。 | |
| redis_cmd_line | Redis | Redis 命令的内容。 | |
| resp_body | core | 在 logger 插件中,如果部分插件支持记录响应的 body 信息,比如配置 `include_resp_body: true`,那可以在 log format 中使用该变量。| |
| rpc_time | xRPC | 在 RPC 请求级别所花费的时间。 | |

当然,除上述变量外,你也可以创建自定义[变量](./plugin-develop.md#register-custom-variable)。
93 changes: 93 additions & 0 deletions t/plugin/file-logger2.t
Original file line number Diff line number Diff line change
Expand Up @@ -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