# HG changeset patch # User Kim Alvefur # Date 1522409655 -7200 # Node ID 557c976735e15ffb88f19ed5dcf6e508bed355bb # Parent 1c336d0d0214a57e12b49f4a802b82fa38e4f6c1 mod_http_logging: Factor out logging into a function for future reuse diff -r 1c336d0d0214 -r 557c976735e1 mod_http_logging/mod_http_logging.lua --- a/mod_http_logging/mod_http_logging.lua Thu Mar 29 22:07:18 2018 +0200 +++ b/mod_http_logging/mod_http_logging.lua Fri Mar 30 13:34:15 2018 +0200 @@ -14,16 +14,20 @@ local server = require "net.http.server"; +local function log_response(response, body) + body = body or response.body; + local len = response.headers.content_length or (body and #body) or "-"; + local request = response.request; + local ip = request.conn:ip(); + local req = string.format("%s %s HTTP/%s", request.method, request.path, request.httpversion); + local date = os.date("%d/%m/%Y:%H:%M:%S %z"); + module:log("info", "%s - - [%s] \"%s\" %d %s", ip, date, req, response.status_code, tostring(len)); +end + local send_response = server.send_response; local function log_and_send_response(response, body) if not response.finished then - body = body or response.body; - local len = body and #body or "-"; - local request = response.request; - local ip = request.conn:ip(); - local req = string.format("%s %s HTTP/%s", request.method, request.path, request.httpversion); - local date = os.date("%d/%m/%Y:%H:%M:%S %z"); - module:log("info", "%s - - [%s] \"%s\" %d %s", ip, date, req, response.status_code, tostring(len)); + log_response(response, body); end return send_response(response, body); end