Writing An Apache Module To Add Custom "%" Directives

Published: December 5, 2016

mod_log_config provides many useful ”%” directives for defining CustomLog formats. In combination with its friend, mod_logio, 99% percent of logging use cases are covered. However, one day, you may find that there’s something you want to log that is not accessible with the tools Apache provides you. Luckily, you can utilize Apache’s module system to add your own logging directives. In this guide, we’ll write an Apache module that adds a %^IH % directive which records request header size, in bytes.

Scaffolding The Module

Per the official Apache module development guide, all modules use the same boilerplate code to register themselves. We will call our module log_header_size. Put the following code into a file called mod_log_header_size.c

You should now be able to add %^IH to a LogFormat string and see “Example” added to the logs.

Actually Getting The Request Header Size

In order to actually get the request header size we’ll take a look at the source code of another Apache module, mod_log_forensic. This module records the entire request headers to a log file for each request (it does a little more than that, feel free to read the documentation if you’re interested). The function is interest of log_before which uses apr_table_do to iterate through each of the request headers. Here’s an abbreviated version…