Configure logrotate on Linux:

LogRotate program is used to provide the administrator with an up-to-date record of events taking place on the system. The logrotate utility may also be used to back up log files, so copies may be used to establish patterns for system use. Servers which run large applications such as LAMP stacks, often log every activity and request in the face of bulky logs which may use high disk space on the server. Log rotation provides a way to limit the total size of the logs retained while still allowing analysis of recent events.
Logs are also the first source of information where administrators and engineers look while troubleshooting.Logrotate utility is designed to used in system administration in which dated log files are archived.

The main configuration file for logrotate is /etc/logrotate.conf and it has default settings. So, If someone want additional application-specific configuration then you have to create a seperate file for that application in /etc/logrotate.d directory.

In this article you will learn that how to configure logrotate in Linux

Install logrotate on CentOS

dnf install -y logrotate

Configure logrotate

We gonna configure logrotate for Apache web server in CentOS server. Let’s assume we want to rotate the logs of a service “apache” that is creating logfiles under /var/log/httpd directory. So, we gonna create a file “apache” under /etc/logrotate.d directory and make appropriate configuration in this file to rorate all the logs of apache service.

Run following command and add below lines in that file.

vi /etc/logrotate.d/apache
/var/log/httpd/* {
rotate 80
size 3M

/var/log/httpd It means rotating all logs from httpd directory.

daily means rotate the logs on a daily basis. you can also use weekly or monthly.

rotate 80 It means last 80 rotated logs should be kept.
size=3M means log will not be rotated until it reaches 3MB. sets the minimum size for the logs rotation.
compress It compress the old log files to save disk space.
delaycompress so it means rotated logs with the exception of the most recent one should be compressed.

Now save changes to file and exit

You can also set many other options, for details run follwoing command.

man logrotate

By default, logrotate automatically configures a cron job scheduled to run daily. but we want to run our desired log rotation file, that tells logrotate to force the rotation.

logrotate -f /etc/logrotate.d/httpd

Now run below command to see that what will logrotate do reading /etc/logrotate.d/httpd file.

logrotate -d /etc/logrotate.d/httpd

That’s about it.