When developing web applications on Windows or Ubuntu, Apache’s log files can quickly become pretty large after a certain period of time. Knowing how to empty the Apache error log can sometimes save you several gigabytes of valuable space. Several solutions allow you to clear Apache error log cache or reduce its size.
How To Clear Apache Error Log Using Terminal
Server software such as Apache has very powerful logging tools that allow you to monitor application activity on the server. The Apache error log is an indispensable tool that allows you to debug errors that have occurred on your server. Unfortunately, it happens that this error log file increases drastically in size taking up a lot of space on your server. To clear Apache error log, type the command below in a Terminal
echo > /var/log/apache2/error.log
echo > /var/log/httpd/error.log
This will truncate the log file without halting any running file handles.
Please note that clearing Apache error log file is not a viable solution as the error log file can take again a huge amount of space as you are running your application. Other solutions exist in Apache to prevent the log file from taking a lot of space without the need to manually clear the log file. You can either use the Logrotate system utility to manage the Apache log files or change the verbosity of the Apache’s log mechanism.
How To Clear Apache Error Log Using Logrotate
Logrotate allows you to rotate log files automatically. It contains many options to choose how the rotation works. Logrotate allows you to limit the size of log files in /var/log by minimizing them or deleting old log files parodically. Logrotate is installed by default on all Linux distributions.
In this tutorial, we will see how to configure Logrotateto reduce the size of log files or to delete Apache log files automatically after a certain period of time.
When you install Apache in Ubuntu, it adds the file
/etc/logrotate.d/apache2, which is the default configuration file used by Logrotate to rotate access and error logs.
Open the /etc/logrotate.d/apache2 file and edit its content as follows:
/etc/init.d/apache2 reload > /dev/null
Let’s explain some of these options:
- rotate 5: tells Logrotate to only save the past five rotated log files (and clear Apache’s oldest log one at each newly created log file). This allows you to automatically clear Apache error log files.
- size 25M: tells Logrotate to rotate the log if it exceeds 30MB in size.
- compress: tells Logrotate to use gzip to compress the log file. This drastically reduces apache’s log file size.
How To Reduce Apache Error Log Size By Changing Apache’s Log Verbosity
Instead of clearing Apache error log files, you can reduce the Apache error log file size by changing Apache’s log verbosity.
Open your Apache configuration file (Located at
/etc/httpd/httpd.conf on Fedora and CentOs, Or at /etc/apache2/apache2.conf on Ubuntu and Debian)
sudo nano /etc/apache2/apache2.conf
LogLevel and change its value to “error”:
# LogLevel: Control the number of messages logged to the error_log.
# Possible values include: debug, info, notice, warn, error, crit,
# alert, emerg.
Setting LogLevel to “Error” reduces the amount of information the log file saves by logging only errors on your server and filtering any other nonsignificant data.
Here are the possible values for Apache’s verbosity level:
|System is unusable
|Action must be taken immediately
|Normal, but significant conditions
The Debug level is the most verbose log value that can be set and can quickly take a huge amount of disk space on your server. The debug option as its name suggest should only be used for debug purposes as it helps you debbuging your applications during developpement stage by recording a maximum of informations on the Apaches error log files.
The “emerg” level is the least verbose possible value for the Apache log system as it only records system crash errors. We suggest not using this option as it doesn’t provide enough information in case of errors on your application.