Log rotate, a linux Log cutting tool

Keywords: Operation & Maintenance Linux Nginx PHP Tomcat

Log rotate is installed by default on Linux system, and the default configuration file is:

/etc/logrotate.conf
/etc/logrotate.d/
logrotate.conf: primary configuration file
logrotate.d: to configure the relevant subsystem to isolate each application configuration (Nginx, PHP, Tomcat...)
It is used to solve the problem that individual log files are too large to be cleaned and viewed easily.

cat /etc/logrotate.d/haproxy 
/var/log/haproxy/haproxy.log {  
#The absolute path of the file, and the same for nginx log
   daily
#Specify a dump period of every day
   rotate 20
#Keep a log for 20 days
   missingok
#If the log file is lost, do not display the error
   notifempty
#Do not dump when log file is empty
   dateext
#Plus log format
   compress
#Compress the dumped logs through gzip
   sharedscripts
#After the dump, run the script. The script content is between postrotate endscript and the script effect is to restart rsyslogd service.
   postrotate
       systemctl restart  rsyslog.service
   endscript
}

Test profile
logrotate -d /etc/logrotate.d/haproxy
The service is called by anacron
The default configuration of anacron is as follows
Modify the call time of the service

cat /etc/anacrontab 
# /etc/anacrontab: configuration file for anacron

# See anacron(8) and anacrontab(5) for details.

SHELL=/bin/sh
PATH=/sbin:/bin:/usr/sbin:/usr/bin
MAILTO=root
# the maximal random delay added to the base delay of the jobs
RANDOM_DELAY=45
# the jobs will be started during the following hours only
#The default value is 3:00 to 22:00, and the random delay is 45 minutes, which is changed to 3:00 to 5:00.
START_HOURS_RANGE=3-5

#period in days   delay in minutes   job-identifier   command
1	5	cron.daily		nice run-parts /etc/cron.daily
7	25	cron.weekly		nice run-parts /etc/cron.weekly
@monthly 45	cron.monthly		nice run-parts /etc/cron.monthly

The effect is as follows

-rw-------  1 root   root     3030916 Jul  1 03:48 haproxy.log-20190701.gz
-rw-------  1 root   root     3870655 Jul  2 03:24 haproxy.log-20190702.gz
-rw-------  1 root   root     4569410 Jul  3 03:39 haproxy.log-20190703.gz
-rw-------  1 root   root     3715970 Jul  4 03:33 haproxy.log-20190704.gz

Other configurable parameters in Logrotate are as follows:

compress //Compress and dump the logs later through gzip
nocompress //No gzip compression
copytruncate //It is used to back up and truncate the current log for log files that are still open. It is a method of copying first and then emptying. There is a time difference between copying and emptying, and some log data may be lost.
nocopytruncate //Backup log files without truncation
create mode owner group //When rotating, specify the property of creating a new file, such as create 0777 nobody nobody
nocreate //Do not create a new log file
delaycompress //When used with compress, the dumped log files will not be compressed until the next dump
nodelaycompress //Overwrite the delaycompress option, dump and compress at the same time.
missingok //If the log is lost, continue to scroll to the next log without error
errors address //Send the error message of special storage to the specified Email address
ifempty //Rotate even if the log file is empty. This is the default option of logrotate.
notifempty //Do not rotate when the log file is empty
mail address //Send the dump log file to the specified E-mail address
nomail //Do not send log files when dumping
olddir directory //The dumped log file is placed in the specified directory and must be in the same file system as the current log file
noolddir //The dump log file and the current log file are in the same directory
sharedscripts //Run the postrotate script to execute the script once after all logs are rotated. If this is not configured, a script will be executed after each log rotation.
prerotate //The instructions to be executed before the logrotate dump, such as modifying the attributes of the file, must be independent.
postrotate //Instructions to be executed after logrotate dump, such as restarting (kill -HUP) a service! Must be independent
daily //Specify a dump period of every day
weekly //Specify dump cycle as weekly
monthly //Specify a dump period of every month
rotate count //Specify the number of times to dump the log file before deleting. 0 means there is no backup, 5 means there are 5 backups reserved.
dateext //Use current date as naming format
dateformat .%s //It is used with dateext and immediately appears on the next line. It is necessary to define the file name after file cutting. It only supports the four parameters of% Y% m% d% s.
size(or minsize) log-size //Log size can specify bytes (default) and KB (sizek) or MB(sizem).
//Dump when log file > = log size. The following is the legal format: (the unit case of other formats has not been tried)
size = 5 or size 5 (>= 5 Dump in bytes)
size = 100k or size 100k
size = 100M or size 100M

Reference resources: https://www.linuxidc.com/Linux/2019-02/157099.htm

Posted by lordtrini on Fri, 18 Oct 2019 10:11:14 -0700