Comparison

mod_http_logging/mod_http_logging.lua @ 1882:99863a6a7b8c

mod_http_logging: Produce HTTP logs in the style of Apache
author Kim Alvefur <zash@zash.se>
date Wed, 30 Sep 2015 13:36:13 +0200
child 1883:c625ed20ebda
comparison
equal deleted inserted replaced
1881:3683eb95bc1a 1882:99863a6a7b8c
1 -- mod_http_logging
2 --
3 -- Copyright (C) 2015 Kim Alvefur
4 --
5 -- Produces HTTP logs in the style of Apache
6 --
7 -- TODO
8 -- * Configurable format?
9
10 module:set_global();
11
12 local server = require "net.http.server";
13
14 local send_response = server.send_response;
15 local function log_and_send_response(response, body)
16 if not response.finished then
17 body = body or response.body;
18 local len = body and #body or "-";
19 local request = response.request;
20 local ip = request.conn:ip();
21 local req = string.format("%s %s HTTP/%s", request.method, request.path, request.httpversion);
22 local date = os.date("%d/%m/%Y:%H:%M:%S %z");
23 module:log("info", "%s - - [%s] \"%s\" %d %s", ip, date, req, response.status_code, tostring(len));
24 end
25 return server.send_response(response, body);
26 end
27
28 if module.wrap_object_event then
29 -- Use object event wrapping, allows clean unloading of the module
30 module:wrap_object_event(server._events, false, function (handlers, event_name, event_data)
31 if event_data.response then
32 event_data.response.send = log_and_send_response;
33 end
34 return handlers(event_name, event_data);
35 end);
36 else
37 -- Fall back to monkeypatching, unlikely to behave nicely in the
38 -- presence of other modules also doing this
39 server.send_response = log_and_send_response;
40 function module.unload()
41 server.send_response = send_response;
42 end
43 end