Skip to content

Commit 4c77327

Browse files
sshniroSaberMaster
authored andcommitted
feature: add option to include request body in log util (apache#1545)
1 parent 8a72574 commit 4c77327

File tree

6 files changed

+26
-7
lines changed

6 files changed

+26
-7
lines changed

apisix/plugins/http-logger.lua

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ local schema = {
3636
buffer_duration = {type = "integer", minimum = 1, default = 60},
3737
inactive_timeout = {type = "integer", minimum = 1, default = 5},
3838
batch_max_size = {type = "integer", minimum = 1, default = 1000},
39+
include_req_body = {type = "boolean", default = false}
3940
},
4041
required = {"uri"}
4142
}
@@ -121,7 +122,7 @@ end
121122

122123

123124
function _M.log(conf)
124-
local entry = log_util.get_full_log(ngx)
125+
local entry = log_util.get_full_log(ngx, conf)
125126

126127
if not entry.route_id then
127128
core.log.error("failed to obtain the route id for http logger")

apisix/plugins/kafka-logger.lua

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ local schema = {
4444
buffer_duration = {type = "integer", minimum = 1, default = 60},
4545
inactive_timeout = {type = "integer", minimum = 1, default = 5},
4646
batch_max_size = {type = "integer", minimum = 1, default = 1000},
47+
include_req_body = {type = "boolean", default = false}
4748
},
4849
required = {"broker_list", "kafka_topic", "key"}
4950
}
@@ -111,7 +112,7 @@ end
111112

112113

113114
function _M.log(conf)
114-
local entry = log_util.get_full_log(ngx)
115+
local entry = log_util.get_full_log(ngx, conf)
115116

116117
if not entry.route_id then
117118
core.log.error("failed to obtain the route id for kafka logger")

apisix/plugins/syslog.lua

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ local schema = {
4242
tls = {type = "boolean", default = false},
4343
batch_max_size = {type = "integer", minimum = 1, default = 1000},
4444
buffer_duration = {type = "integer", minimum = 1, default = 60},
45+
include_req_body = {type = "boolean", default = false}
4546
},
4647
required = {"host", "port"}
4748
}
@@ -127,7 +128,7 @@ end
127128

128129
-- log phase in APISIX
129130
function _M.log(conf)
130-
local entry = log_util.get_full_log(ngx)
131+
local entry = log_util.get_full_log(ngx, conf)
131132

132133
if not entry.route_id then
133134
core.log.error("failed to obtain the route id for sys logger")

apisix/plugins/tcp-logger.lua

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ local schema = {
4040
buffer_duration = {type = "integer", minimum = 1, default = 60},
4141
inactive_timeout = {type = "integer", minimum = 1, default = 5},
4242
batch_max_size = {type = "integer", minimum = 1, default = 1000},
43+
include_req_body = {type = "boolean", default = false}
4344
},
4445
required = {"host", "port"}
4546
}
@@ -115,7 +116,7 @@ end
115116

116117

117118
function _M.log(conf)
118-
local entry = log_util.get_full_log(ngx)
119+
local entry = log_util.get_full_log(ngx, conf)
119120

120121
if not entry.route_id then
121122
core.log.error("failed to obtain the route id for tcp logger")

apisix/plugins/udp-logger.lua

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ local schema = {
3636
buffer_duration = {type = "integer", minimum = 1, default = 60},
3737
inactive_timeout = {type = "integer", minimum = 1, default = 5},
3838
batch_max_size = {type = "integer", minimum = 1, default = 1000},
39+
include_req_body = {type = "boolean", default = false}
3940
},
4041
required = {"host", "port"}
4142
}
@@ -98,7 +99,7 @@ end
9899

99100

100101
function _M.log(conf)
101-
local entry = log_util.get_full_log(ngx)
102+
local entry = log_util.get_full_log(ngx, conf)
102103

103104
if not entry.route_id then
104105
core.log.error("failed to obtain the route id for udp logger")

apisix/utils/log-util.lua

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ local core = require("apisix.core")
1818

1919
local _M = {}
2020

21-
local function get_full_log(ngx)
21+
local function get_full_log(ngx, conf)
2222
local ctx = ngx.ctx.api_ctx
2323
local var = ctx.var
2424
local service_id
@@ -34,7 +34,7 @@ local function get_full_log(ngx)
3434
service_id = var.host
3535
end
3636

37-
return {
37+
local log = {
3838
request = {
3939
url = url,
4040
uri = var.request_uri,
@@ -56,6 +56,20 @@ local function get_full_log(ngx)
5656
start_time = ngx.req.start_time() * 1000,
5757
latency = (ngx.now() - ngx.req.start_time()) * 1000
5858
}
59+
60+
if conf.include_req_body then
61+
local body = ngx.req.get_body_data()
62+
if body then
63+
log.request.body = body
64+
else
65+
local body_file = ngx.req.get_body_file()
66+
if body_file then
67+
log.request.body_file = body_file
68+
end
69+
end
70+
end
71+
72+
return log
5973
end
6074

6175
_M.get_full_log = get_full_log

0 commit comments

Comments
 (0)