October 18, 2020 | 12:20

Conditional Logging with Apache

I’ve started using Apache-Exporter1 for monitoring and checking this weekend how useful it is and how it can be integrated into my Prometheus2 monitoring enviroment. The server-status requests inevitably lead to more “background noise” in the Apache logfiles. The screenshot below clearly shows in the upper less section:


Of course the requests cannot be prevented, but you can manipulate what Apache writes in its logfiles. It’s called conditional logging and allows you to set variables with SetEnvIf3 to any regex on each request. With SetEnvIfExpr it is even possible to perform expressions4, for instance to check if a request comes from your inside network segments:

SetEnvIfExpr "-R '' || -R '' || -R ''" isInternal

In my case I set the variable “dontlog” on all Server-Status requests and exclude it explicitly in the logging:

SetEnvIf Request_URI "^/server-status(.*)$" dontlog
CustomLog ${APACHE_LOG_DIR}/other_vhosts_access.log vhost_combined env=!dontlog

Further regex statements e.g. for certain file extensions are possible aswell but should be done with caution. Ultimately, too many exceptions tend to make a usually sharp tool for debugging less reliable.

Never trust a log unless it comes from yourself!

  1. https://github.com/Lusitaniae/apache_exporter ↩︎

  2. https://prometheus.io/ ↩︎

  3. https://httpd.apache.org/docs/2.4/mod/mod_setenvif.html ↩︎

  4. https://httpd.apache.org/docs/2.4/expr.html ↩︎

© 2020 Tomas Jakobs - Imprint and Legal Notice