mod_http_logging/mod_http_logging.lua
changeset 2969 557c976735e1
parent 2167 88fec2b2bd58
child 2970 678be8ea4d38
equal deleted inserted replaced
2968:1c336d0d0214 2969:557c976735e1
    12 
    12 
    13 module:set_global();
    13 module:set_global();
    14 
    14 
    15 local server = require "net.http.server";
    15 local server = require "net.http.server";
    16 
    16 
       
    17 local function log_response(response, body)
       
    18 	body = body or response.body;
       
    19 	local len = response.headers.content_length or (body and #body) or "-";
       
    20 	local request = response.request;
       
    21 	local ip = request.conn:ip();
       
    22 	local req = string.format("%s %s HTTP/%s", request.method, request.path, request.httpversion);
       
    23 	local date = os.date("%d/%m/%Y:%H:%M:%S %z");
       
    24 	module:log("info", "%s - - [%s] \"%s\" %d %s", ip, date, req, response.status_code, tostring(len));
       
    25 end
       
    26 
    17 local send_response = server.send_response;
    27 local send_response = server.send_response;
    18 local function log_and_send_response(response, body)
    28 local function log_and_send_response(response, body)
    19 	if not response.finished then
    29 	if not response.finished then
    20 		body = body or response.body;
    30 		log_response(response, body);
    21 		local len = body and #body or "-";
       
    22 		local request = response.request;
       
    23 		local ip = request.conn:ip();
       
    24 		local req = string.format("%s %s HTTP/%s", request.method, request.path, request.httpversion);
       
    25 		local date = os.date("%d/%m/%Y:%H:%M:%S %z");
       
    26 		module:log("info", "%s - - [%s] \"%s\" %d %s", ip, date, req, response.status_code, tostring(len));
       
    27 	end
    31 	end
    28 	return send_response(response, body);
    32 	return send_response(response, body);
    29 end
    33 end
    30 
    34 
    31 if module.wrap_object_event then
    35 if module.wrap_object_event then