@@ -82,23 +82,71 @@ local function gen_log_format(format)
8282 return log_format
8383end
8484
85+ local custom_vars = {
86+ latency = function () return (ngx_now () - ngx .req .start_time ()) * 1000 end ,
87+ upstream_latency = function (ctx )
88+ return ctx .var .upstream_response_time and ctx .var .upstream_response_time * 1000 or nil
89+ end ,
90+ apisix_latency = function (ctx )
91+ local latency = (ngx_now () - ngx .req .start_time ()) * 1000
92+ local upstream_latency =
93+ ctx .var .upstream_response_time and ctx .var .upstream_response_time * 1000 or 0
94+ local apisix_latency = latency - upstream_latency
95+ if apisix_latency < 0 then apisix_latency = 0 end
96+ return apisix_latency
97+ end ,
98+ client_ip = function (ctx ) return core .request .get_remote_client_ip (ctx ) end ,
99+ start_time = function () return ngx .req .start_time () * 1000 end ,
100+ hostname = function () return core .utils .gethostname () end ,
101+ version = function () return core .version .VERSION end ,
102+ request_method = function () return ngx .req .get_method () end ,
103+ request_headers = function () return ngx .req .get_headers () end ,
104+ request_querystring = function () return ngx .req .get_uri_args () end ,
105+ request_body = function (ctx , max_req_body_bytes )
106+ local max_bytes = max_req_body_bytes or MAX_REQ_BODY
107+ local req_body , err = get_request_body (max_bytes )
108+ if err then
109+ core .log .error (" fail to get request body: " , err )
110+ return nil
111+ end
112+ return req_body
113+ end ,
114+ response_status = function () return ngx .status end ,
115+ response_headers = function () return ngx .resp .get_headers () end ,
116+ response_size = function (ctx ) return ctx .var .bytes_sent end ,
117+ response_body = function (ctx ) return ctx .resp_body end ,
118+ upstream = function (ctx ) return ctx .var .upstream_addr end ,
119+ url = function (ctx )
120+ local var = ctx .var
121+ return string.format (" %s://%s:%s%s" ,
122+ var .scheme ,
123+ var .host ,
124+ var .server_port ,
125+ var .request_uri )
126+ end ,
127+ consumer_username = function (ctx )
128+ return ctx .consumer and ctx .consumer .username or nil
129+ end ,
130+ service_id = function (ctx )
131+ local matched_route = ctx .matched_route and ctx .matched_route .value
132+ return matched_route and matched_route .service_id
133+ end ,
134+ route_id = function (ctx )
135+ local matched_route = ctx .matched_route and ctx .matched_route .value
136+ return matched_route and matched_route .id
137+ end ,
138+ }
85139
86140local function get_custom_format_log (ctx , format , max_req_body_bytes )
87141 local log_format = lru_log_format (format or " " , nil , gen_log_format , format )
88142 local entry = core .table .new (0 , core .table .nkeys (log_format ))
89143 for k , var_attr in pairs (log_format ) do
90144 if var_attr [1 ] then
91145 local key = var_attr [2 ]
92- if key == " request_body" then
93- local max_req_body_bytes = max_req_body_bytes or MAX_REQ_BODY
94- local req_body , err = get_request_body (max_req_body_bytes )
95- if err then
96- core .log .error (" fail to get request body: " , err )
97- else
98- entry [k ] = req_body
99- end
146+ if custom_vars [key ] then
147+ entry [k ] = custom_vars [key ](ctx , max_req_body_bytes )
100148 else
101- entry [k ] = ctx .var [var_attr [ 2 ] ]
149+ entry [k ] = ctx .var [key ]
102150 end
103151 else
104152 entry [k ] = var_attr [2 ]
@@ -163,7 +211,8 @@ local function get_full_log(ngx, conf)
163211 local consumer
164212 if ctx .consumer then
165213 consumer = {
166- username = ctx .consumer .username
214+ username = ctx .consumer .username ,
215+ group_id = ctx .consumer .group_id
167216 }
168217 end
169218
@@ -277,10 +326,16 @@ function _M.get_log_entry(plugin_name, conf, ctx)
277326 local has_meta_log_format = metadata and metadata .value .log_format
278327 and core .table .nkeys (metadata .value .log_format ) > 0
279328
280- if conf .log_format or has_meta_log_format then
329+ local format
330+ if conf .log_format then
331+ format = conf .log_format
332+ elseif has_meta_log_format then
333+ format = metadata .value .log_format
334+ end
335+
336+ if format then
281337 customized = true
282- entry = get_custom_format_log (ctx , conf .log_format or metadata .value .log_format ,
283- conf .max_req_body_bytes )
338+ entry = get_custom_format_log (ctx , format , conf .max_req_body_bytes )
284339 else
285340 if is_http then
286341 entry = get_full_log (ngx , conf )
0 commit comments