Log files are files that are produced by services or programs to inform about their performance for the monitoring and control of network administrators or programmers. If the size of log files becomes very large, also var/log/ may become very large. In Linux, there are tools for rotating log files. The most common tool for rotating is called LogRotate. This article with teaches you How to Install LogRotate on CentOS 7. If you intend to buy a CentOS VPS server, you can check out the packages offered on the Eldernode website.
Table of Contents
What is LogRotate?
LogRotate is a service in Linux that is often run once a day and system log files for which the archive date is defined are archived or deleted. In terms of functionality, Logrotate has full control over all log files that are specified by the user or the software itself in Logrotate’s configuration files. It allows you to easily manage systems that generate large numbers of log files. This service also allows automatic rotation, compression, removal, and mailing of log files.
Installing LogRotate on CentOS 7
In this section, we will explain the installation process of LogRotate on CentOS 7. First of all, update the system packages with the following command:
yum update
install the LogRotate on CentOS 7 using the following command:
yum install logrotate
The /etc/logrotate.conf configuration file indicates that other more specific settings may be placed separate .conf files within /etc/logrotate.d.
If the below line is present and is not uncommented, this will happen:
include /etc/logrotate.d
Configuring LogRotate on CentOS 7
Now you will learn how to configure LogRotate on CentOS 7.
First, you need to insert the following contents in /etc/logrotate.d/apache2.conf and check each line to indicate its purpose:
/var/log/apache2/* { weekly rotate 3 size 10M compress delaycompress }
Note: You may need to create that file.
The directories inside the block available in the first line, apply to all logs inside /var/log/apache2:
–> Weekly: It means that the tool will attempt to rotate the logs on a weekly basis.
–> Rotate 3: It means that only 3 rotated logs will keep and the oldest file will remove on the fourth subsequent run.
–> Size=10M: It sets the minimum size for the rotation to take place to 10M.
–> Delaycompress/compress: It used to tell that all rotated logs, with the exception of the most recent one should be compressed.
You should execute a dry run using the -d option followed by the configuration file. Here’s what LogRotate would do if it actually ran now:
logrotate -d /etc/logrotate.d/apache2.conf
You can rename the logs after the date when they were rotate with the command below:
dateext
Also, use the following command to prevent the rotation from happening:
notifempty
You can use /etc/logrotate.d/squid.conf to rotate /var/log/squid/access.log:
squid.conf /var/log/squid/access.log { monthly create 0644 root root rotate 5 size=1M dateext dateformat -%d%m%Y notifempty mail [email protected] }
If you want to run a custom command when the rotation takes place, you should place the command between the post-rotate and end script directives.
That’s it!
Conclusion
LogRotate is the most common tool in Linux for rotating log files. In this article, we taught you how to Install and configure LogRotate on CentOS 7. I hope this tutorial helps you to install and configure LogRotate on CentOS 7. If you have any questions or suggestions, you can contact us in the Comments section.