From 1ad63fbb7379ecd240c58979ef982dfb2c80a7ba Mon Sep 17 00:00:00 2001 From: Daniel Gruno Date: Sun, 1 Sep 2013 15:10:32 +0000 Subject: [PATCH] Adding a simple logging hook for mod_lua, which allows users to create their own logs or bypass the generic logging on a per-request basis. git-svn-id: https://svn.apache.org/repos/asf/httpd/httpd/trunk@1519277 13f79535-47bb-0310-9956-ffa450edef68 --- docs/manual/mod/mod_lua.xml | 54 +++++++++++++++++++++++++++++++++++-- modules/lua/mod_lua.c | 23 ++++++++++++++++ 2 files changed, 75 insertions(+), 2 deletions(-) diff --git a/docs/manual/mod/mod_lua.xml b/docs/manual/mod/mod_lua.xml index 82ed0fa9e8..a921198e2d 100644 --- a/docs/manual/mod/mod_lua.xml +++ b/docs/manual/mod/mod_lua.xml @@ -269,9 +269,10 @@ performing access control, or setting mime types:

Logging - (none) + LuaHookLog Once a request has been handled, it enters several logging phases, - which logs the request in either the error or access log + which logs the request in either the error or access log. Mod_lua + is able to hook into the start of this and control logging output. @@ -1453,6 +1454,55 @@ processing + +LuaHookLog +Provide a hook for the access log phase of a request +processing +LuaHookLog /path/to/lua/script.lua log_function_name +server configvirtual host +directory.htaccess + +All + +

+ This simple logging hook allows you to run a function when httpd enters the + logging phase of a request. With it, you can append data to your own logs, + manipulate data before the regular log is written, or prevent a log entry + from being created. To prevent the usual logging from happening, simply return + apache2.DONE in your logging handler, otherwise return + apache2.OK to tell httpd to log as normal. +

+

Example:

+ +LuaHookLog /path/to/script.lua logger + + +-- /path/to/script.lua -- +function logger(r) + -- flip a coin: + -- If 1, then we write to our own Lua log and tell httpd not to log + -- in the main log. + -- If 2, then we just sanitize the output a bit and tell httpd to + -- log the sanitized bits. + + if math.random(1,2) == 1 then + -- Log stuff ourselves and don't log in the regular log + local f = io.open("/foo/secret.log", "a") + if f then + f:write("Something secret happened at " .. r.uri .. "\n") + f:close() + end + return apache2.DONE -- Tell httpd not to use the regular logging functions + else + r.uri = r.uri:gsub("somesecretstuff", "") -- sanitize the URI + return apache2.OK -- tell httpd to log it. + end +end + +
+
+ + LuaHookMapToStorage Provide a hook for the map_to_storage phase of request processing diff --git a/modules/lua/mod_lua.c b/modules/lua/mod_lua.c index 7c35011ec1..79a9456815 100644 --- a/modules/lua/mod_lua.c +++ b/modules/lua/mod_lua.c @@ -1155,6 +1155,11 @@ static void lua_insert_filter_harness(request_rec *r) /* ap_log_rerror(APLOG_MARK, APLOG_WARNING, 0, r, "LuaHookInsertFilter not yet implemented"); */ } +static int lua_log_transaction_harness(request_rec *r) +{ + return lua_request_rec_hook_harness(r, "log_transaction", APR_HOOK_FIRST); +} + static int lua_quick_harness(request_rec *r, int lookup) { if (lookup) { @@ -1219,6 +1224,15 @@ static const char *register_map_to_storage_hook(cmd_parms *cmd, void *_cfg, return register_named_file_function_hook("map_to_storage", cmd, _cfg, file, function, APR_HOOK_MIDDLE); } + +static const char *register_log_transaction_hook(cmd_parms *cmd, void *_cfg, + const char *file, + const char *function) +{ + return register_named_file_function_hook("log_transaction", cmd, _cfg, + file, function, APR_HOOK_FIRST); +} + static const char *register_map_to_storage_block(cmd_parms *cmd, void *_cfg, const char *line) { @@ -1226,6 +1240,7 @@ static const char *register_map_to_storage_block(cmd_parms *cmd, void *_cfg, line); } + static const char *register_check_user_id_hook(cmd_parms *cmd, void *_cfg, const char *file, const char *function, @@ -1783,6 +1798,10 @@ command_rec lua_commands[] = { AP_INIT_TAKE2("LuaHookInsertFilter", register_insert_filter_hook, NULL, OR_ALL, "Provide a hook for the insert_filter phase of request processing"), + + AP_INIT_TAKE2("LuaHookLog", register_log_transaction_hook, NULL, + OR_ALL, + "Provide a hook for the logging phase of request processing"), AP_INIT_TAKE123("LuaScope", register_lua_scope, NULL, OR_ALL, "One of once, request, conn, server -- default is once"), @@ -1983,6 +2002,10 @@ static void lua_register_hooks(apr_pool_t *p) /* ivm mutex */ apr_thread_mutex_create(&lua_ivm_mutex, APR_THREAD_MUTEX_DEFAULT, p); + + /* Logging catcher */ + ap_hook_log_transaction(lua_log_transaction_harness,NULL,NULL, + APR_HOOK_FIRST); } AP_DECLARE_MODULE(lua) = { -- 2.49.0