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
6 changes: 5 additions & 1 deletion apisix/core/ctx.lua
Original file line number Diff line number Diff line change
Expand Up @@ -209,10 +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,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why remove the comma?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I readded it

service_name = true
}

local mt = {
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 @@ -49,5 +49,6 @@ additional variables.
| service_name | core | Name of Service. | |
| redis_cmd_line | Redis | The content of Redis command. | |
| rpc_time | xRPC | Time spent at the rpc request level. | |
| resp_body | core | This variable is only for logger and requires the logger plugin to have a special configuration like `include_resp_body`. | |
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should explain what this variable is, and what decides its value

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done


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 @@ -48,5 +48,6 @@ APISIX 除了支持 [NGINX 变量](http://nginx.org/en/docs/varindex.html)外,
| service_name | core | APISIX 服务的名称。 | |
| redis_cmd_line | Redis | Redis 命令的内容。 | |
| rpc_time | xRPC | 在 RPC 请求级别所花费的时间。 | |
| resp_body | core | 此变量仅用于 Logger,并要求对应的 Logger 需要具备某些配置。 | |
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please put it in alphabetical order

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done


当然,除上述变量外,你也可以创建自定义[变量](./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