Log files contain useful information about events occurring in the system and are often used during troubleshooting or system performance analysis. For busy servers, the log file size can grow extremely fast, and the server can quickly consume disk space, which becomes a problem. In addition, dealing with a single large log file is often tricky. There are many ways to cut Nginx logs. This chapter takes out two cases to share, which are Logrotate tool cutting and script cutting.

Logrotate Cut Nginx log

Cutting mode 1

[root@nginx_proxy02 /]# vim /etc/logrotate.d/nginx
Copy clear cut by day
/usr/local/nginx/logs/*.log{ daily rotate 30 missingok notifempty compress nodelaycompress copytruncate dateext dateformat -%Y-%m-%d dateyesterday } configuration parsing:# Cut by day | keep 30 log files | log errors encountered during cutting are ignored | Logs will not be cut and compressed if empty |
Do not compress the freshly cut log file in the next loop | copy the source log file and empty the source log file |
# file to add log extension after cutting the way | extended to % % Y - m - % d | log file named after the cutting date | yesterday

# Perform the cut manually (we use it for testing)1. Write 30M contents [root@nginx_proxy02 logs]# head -c 30M < /dev/urandom > /usr/local/nginx/logs/error.log
[root@nginx_proxy02 logs]# head -c 30M < /dev/urandom > /usr/local/nginx/logs/access.log
[root@nginx_proxy02 logs]# head -c 30M < /dev/urandom > /usr/local/nginx/logs/access.json2. Enforce cutting (whether or not it is automatic execution time for task plan) [root@nginx_proxy02 logs]# logrotate -vf /etc/logrotate.d/nginx-v Displays detailed information about the cutting process. -f Executes the cutting forcibly. 3.# ls -lrth
total 91M
-rw-r--r-- 1 nginx root 31M Mar 25 17:09 error.log-2020-03-24.gz
-rw-r--r-- 1 nginx root 31M Mar 25 17:09 access.log-2020-03-24.gz
-rw-r--r-- 1 nginx root 30M Mar 25 17:09 access.json
-rw-r--r-- 1 nginx root   0 Mar 25 17:11 access.log
-rw-r--r-- 1 nginx root   0 Mar 25 17:11 error.log
Copy the code

Cutting Mode 2

[root@nginx_proxy02 /]# vim /etc/logrotate.d/nginx
Send a signal to Nginx to restart log file cutting
/usr/local/nginx/logs/*.log {
        daily
        rotate 30
        missingok
        notifempty
        compress
        nodelaycompress
        dateext
        dateformat -%Y-%m-%d
        dateyesterday
        postrotate
            if [ -f /usr/local/nginx/run/nginx.pid ];then
                kill -USR1 `cat /usr/local/nginx/run/nginx.pid`
            fiEndscript}# Cut by day | keep 30 log files | log errors encountered during cutting are ignored | Logs will not be cut and compressed if empty |
Do not compress the freshly cut log file in the next loop
# file to add log extension after cutting the way | extended to % % Y - m - % d | log file named after the cutting date | yesterday
Run postrotate/endscript to send a signal to nginx to re-open the log
Copy the code

Cutting Mode 3

# File size cutting for different formats
"/usr/local/nginx/logs/access.log" "/usr/local/nginx/logs/error.log" /usr/local/nginx/logs/access.json { size 100M rotate 30 missingok notifempty compress nodelaycompress copytruncate dateext Dateformat -%Y-%m-%d-%H} dateformat -%Y-%m-%d-%H} dateformat -%Y-%m-%d-%HCopy the code

Cutting the time All above logrotate by time cut one day week | | | month years, but specific time is a few o ‘clock? Logrotate default timing task in the/etc/cron. Daily/logrotate file

[root@nginx_proxy02 logs]# cat /etc/cron.daily/logrotate
#! /bin/sh

/usr/sbin/logrotate -s /var/lib/logrotate/logrotate.status /etc/logrotate.conf
EXITVALUE=$?
if [ $EXITVALUE != 0 ]; then
    /usr/bin/logger -t logrotate "ALERT exited abnormally with [$EXITVALUE]. ""
fi
exit 0
Copy the code

But only in the above scripts executed/usr/sbin/logrotate -s/var/lib/logrotate/logrotate status/etc/logrotate. Conf logotate command, And then there is a check on whether the execution was successful, and there is no time, where can I find this? The behavior of logrotate is controlled by crontab, and the crontab task is controlled by anacron

[root@nginx_proxy02 logs]# 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
START_HOURS_RANGE=3-22          

#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
Copy the code

All system-level scheduled tasks are executed at the point in time specified by the script START_HOURS_RANGE=, with 3-22 being between 3am and 22am each day. We can change START_HOURS_RANGE to 23-24 (this is the log cutting time, i.e. only between 23:00 and 24:00, which should be 23:00), and of course we can write our own crontab task, but delete the logrotate task schedule. The nginx log is then cut at 00:00 am each day

[root@nginx_proxy02 logs]# rm -rf /etc/cron.daily/logrotate
[root@nginx_proxy02 logs]# crontab -e
#Nginx logs cut
00 00 * * * /sbin/logrotate -f /etc/logrotate.d/nginx
Copy the code

Does more articles and materials | click behind the text to the left left left 100 gpython self-study data package Ali cloud K8s practical manual guide] [ali cloud CDN row pit CDN ECS Hadoop large data of actual combat operations guide the conversation practice manual manual Knative cloud native application development guide OSS Operation and maintenance field manual Cloud native architecture white paper Zabbix enterprise-level distributed monitoring system source document Linux&Python self-study information package 10G interview questions