]> granicus.if.org Git - apache/commitdiff
Adding a simple logging hook for mod_lua, which allows users to create their own...
authorDaniel Gruno <humbedooh@apache.org>
Sun, 1 Sep 2013 15:10:32 +0000 (15:10 +0000)
committerDaniel Gruno <humbedooh@apache.org>
Sun, 1 Sep 2013 15:10:32 +0000 (15:10 +0000)
git-svn-id: https://svn.apache.org/repos/asf/httpd/httpd/trunk@1519277 13f79535-47bb-0310-9956-ffa450edef68

docs/manual/mod/mod_lua.xml
modules/lua/mod_lua.c

index 82ed0fa9e8b3a6b11cc43a47158fef6f1511cfe0..a921198e2d231ca9bb1ff9c77f32f27593ece63f 100644 (file)
@@ -269,9 +269,10 @@ performing access control, or setting mime types:</p>
     </tr>
     <tr>
         <td>Logging</td>
-        <td>(none)</td>
+        <td><directive module="mod_lua">LuaHookLog</directive></td>
         <td>Once a request has been handled, it enters several logging phases, 
-            which logs the request in either the error or access log</td>
+            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.</td>
     </tr>
 
 </table>
@@ -1453,6 +1454,55 @@ processing</description>
 </usage>
 </directivesynopsis>
 
+<directivesynopsis>
+<name>LuaHookLog</name>
+<description>Provide a hook for the access log phase of a request
+processing</description>
+<syntax>LuaHookLog  /path/to/lua/script.lua log_function_name</syntax>
+<contextlist><context>server config</context><context>virtual host</context>
+<context>directory</context><context>.htaccess</context>
+</contextlist>
+<override>All</override>
+<usage>
+<p>
+    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
+    <code>apache2.DONE</code> in your logging handler, otherwise return 
+    <code>apache2.OK</code> to tell httpd to log as normal.
+</p>
+<p>Example:</p>
+<highlight language="config">
+LuaHookLog /path/to/script.lua logger
+</highlight>
+<highlight language="lua">
+-- /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
+</highlight>
+</usage>
+</directivesynopsis>
+
+
 <directivesynopsis>
 <name>LuaHookMapToStorage</name>
 <description>Provide a hook for the map_to_storage phase of request processing</description>
index 7c35011ec1e746ea02ab67a39c5e2836a6615e20..79a9456815534812414387cd22ad21757a8c60d2 100644 (file)
@@ -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) = {