How to Setup and Manage Logrotate in centos7.6

How to setup and Manage logrotate in Centos

Process

Change Directory to default logrotate directory

[root@linuxhelp ~]# cd /etc/logrotate.d/
 [root@linuxhelp logrotate.d]# ls -la
total 80
drwxr-xr-x.   2 root root  258 Jun 17 15:21 .
drwxr-xr-x. 146 root root 8192 Dec  3 10:42 ..
-rw-r--r--.   1 root root   91 Apr 11  2018 bootlog
-rw-r--r--.   1 root root  160 Sep 15  2017 chrony
-rw-r--r--.   1 root root   71 Apr 11  2018 cups
-rw-r--r--.   1 root root  626 Oct 13  2017 glusterfs
-rw-r--r--    1 root root  194 Apr 24  2019 httpd
-rw-r--r--.   1 root root  172 Sep 30  2016 iscsiuiolog
-rw-r--r--.   1 root root  165 Oct 31  2018 libvirtd
-rw-r--r--.   1 root root  142 Oct 31  2018 libvirtd.qemu
-rw-r--r--    1 root root  893 Aug 16  2018 mariadb
-rw-r--r--.   1 root root  106 Apr 11  2018 numad
-rw-r--r--.   1 root root  136 Jun 10  2014 ppp
-rw-r--r--.   1 root root  408 Aug  3  2017 psacct
-rw-r--r--.   1 root root  115 Oct 31  2018 samba
-rw-r--r--.   1 root root  237 Oct 31  2018 sssd
-rw-r--r--.   1 root root  224 Oct 30  2018 syslog
-rw-r--r--.   1 root root  100 Oct 31  2018 wpa_supplicant
-rw-r--r--.   1 root root  103 Nov  5  2018 yum

Create a your custom logrotate configuration file

[root@linuxhelp logrotate.d]# vim apache
/var/log/httpd/* {
weekly
create 0644 root root
rotate 5
size 100M
dateext
dateformat -%d%m%y
compress
olddir /mnt/test/
notifempty
}

To check the Existing apache’s access log

 [root@linuxhelp logrotate.d]# vim /var/log/httpd/access_log 

To check the Existing apache’s error log

[root@linuxhelp logrotate.d]# vim /var/log/httpd/error_log 

Change directory

[root@linuxhelp logrotate.d]# cd /mnt

create Directory

[root@linuxhelp mnt]# mkdir test

Run logrotate forcely by executing the following command

[root@linuxhelp mnt]# logrotate -f /etc/logrotate.d/apache 

Then change directory to test

[root@linuxhelp mnt]# cd test
[root@linuxhelp test]# ls -la
total 8
drwxr-xr-x  2 root root  51 Dec  3 10:59 .
drwxr-xr-x. 3 root root  18 Dec  3 10:58 ..
-rw-r--r--  1 root root 255 Jun 17 15:30 access_log.1.gz
-rw-r--r--  1 root root 958 Dec  3 10:42 error_log.1.gz

Unzip the rotated log file

[root@linuxhelp test]# gunzip access_log.1.gz 
 [root@linuxhelp test]# gunzip error_log.1.gz 

Check the rotated log file and original log file

[root@linuxhelp test]# vim access_log.1 

Log file become empty when the logrotate has done

 [root@linuxhelp test]# vim /var/log/httpd/access_log 

With this, the method to setup and Manage logrotate in Centos comes to end

FAQ
Q
What is the use of notifempty option in configuration?
A
The use of notifempty option in the configuration is skip logrotate if the file is empty
Q
How to store the rotated log in a specific location?
A
To store the rotated log in specific location use the below option in the configuration
olddir
Q
How to mail the rotated log to the Administrator?
A
To mail the rotated log to the Administrator use the below option in the configuration
mail abc@mydomain.com
Q
How to specify date in rotated log file?
A
To specify date in rotated log file use the below option in configuration file
dateext
Q
What is logrotate?
A
logrotate is designed to easy administration of systems.It allows automatic rotation, compression, removal and mailing of log files.
Log Rotation depends on weekly, monthly and year, or size