By default, Apache server logs all requests that it receives. But this can clutter your log files with unnecessary log entries about images, icons, fonts, css, js, etc. Sometimes it is better to exclude requests from Apache log files to disable logging for them. It removes the clutter from your log files and makes it easy to debug. In this article, we will learn how to exclude requests from Apache log.
How to Exclude Requests from Apache Log
Here are the steps to exclude requests from Apache log. We will basically use SetEnvIf directive to set flags for each type of request that we want to exclude from logging. For example, if you want to exclude image requests from being logged, open Apache configuration file in text editor.
$ vi /etc/httpd/conf
Add the following line to it.
SetEnvIf Request_URI "(\.gif|\.png|\.jpg)$" image-request=nolog
Next, we check the value of image-request flag for each request and set its do_not_log flag, if its image_request flag has value nolog.
SetEnvIf image-request nolog do_not_log
Finally, we log the request only if its do_not_log flag is not set for the request.
CustomLog /var/www/log/general-access.log vcommon env=!do_not_log
To summarize, here are the 3 entries required to disable logging for image requests.
SetEnvIf Request_URI "(\.gif|\.png|\.jpg)$" image-request=nolog SetEnvIf image-request nolog do_not_log CustomLog /var/www/log/general-access.log vcommon env=!do_not_log
Now you can use the same technique to set flags for each type of request type, check its value and set do not flag for only those requests. Here is an example to exclude various types of files from being logged.
## flag robots.txt requests SetEnvIf Request_URI "^/robots\.txt$" robots-request=nolog ## flag favicon requests SetEnvIf Request_URI "^/favicon\.ico$" favicon-request=nolog ## flag image requests SetEnvIf Request_URI "(\.gif|\.png|\.jpg)$" image-request=nolog ## flag Css and JS requests SetEnvIf Request_URI \.css css-request=nolog SetEnvIf Request_URI \.js js-request=nolog ## flag cron calls SetEnvIf Request_URI "(^/cron\.php|^/bgp-start/)" cron-request=nolog ## set do_not_log if any of the above flags are set SetEnvIf robots-request nolog do_not_log SetEnvIf favicon-request nolog do_not_log SetEnvIf image-request nolog do_not_log SetEnvIf css-request nolog do_not_log SetEnvIf js-request nolog do_not_log SetEnvIf cron-request nolog do_not_log ## only log if do_not_log is not set CustomLog /var/www/log/general-access.log vcommon env=!do_not_log
In the above code, Apache checks each request to see if it needs to set any of the flags image-request, robots-request, favicon-request, etc. for it. Once any of these flag is set for a request, its do_not_log flag is set. Finally, we use CustomLog directive to check if its env variable is set to do_not_log. If it is set, then the request is not logged.
After you add the above entries, save and close your log file. Restart Apache server to apply changes.
$ service apache2 restart
In this article, we have learnt how to exclude certain requests from Apache log. You can customize it as per your requirement.
Also read:
How to Exclude Requests from NGINX Log
How to Pass Shell Variable in cURL Command
Shell Script to Restart Service if Not Running
Shell Script to Read Data from Text File
Awk Split One Column into Multiple Columns