Conditional logging in Apache 2

- 1 min

Sometimes Apache 2’s logs can get really polluted by entries which you don’t care about and might skew your statistics. For example you wouldn’t want your own IP address in the logs because you don’t want that counted in the statistic. Although most web analyzers have filters to exclude such things, you can do this directly in Apache by not logging these. To do this you first need to set and environment variable that matches what you don’t want to log - this can use regular expressions and the matching is done on HTTP header fields like Remote_Addr or Request_URI.

Let’s say you want to exclude all internal IPs in your company from your website’s logs. You need to add this to your httpd.conf or domain_name.conf, depending on how you have your web server set up:

SetEnvIf Remote_Addr "10\.*\.*\.*" nologging

This just created a new variable in Apache’s environment called “nologging”. Now we will add this to the log directive in the same config file:

CustomLog log/access.log  combined env =! nologging

Now Apache will log everything except what is matched with the rule above. If you want to add more things to the list of non-logging, we can just continue adding rules to that variable. Let’s assume we decided not to add the favicon.ico requests to the log:

SetEnvIf Request_URI "^/favicon\.ico$" nologging

That’s it, no more pesky favicon requests in the logs! As you can see, the rules are pretty powerful and there are many uses for this! Also, don’t forget that you need the “log_config” and the “setenvif” module loaded in Apache! As usual you can check this using httpd -M or apache2 -M on the command line.

For more details: http://httpd.apache.org/docs/2.2/mod/mod_log_config.html

rss facebook twitter github youtube mail spotify instagram linkedin google pinterest medium tumblr soundcloud blog