Yesterday, the online project we participated in was launched. After reading the log file, we did not split it. Considering that the log file may cause a large number of log files to cause performance degradation, we quickly split the log file. In Linux, there is a logrotate tool that can assist log segmentation. You only need to configure the parameters, which is very convenient. Therefore, you can directly configure this method.
I used to split nginx log files. My configuration file is as follows:
Create a configuration file, such as nginx, in the/etc/logrotate. d/directory. The file content is as follows:
/Search/nginx/logs/access_log/search/nginx/logs/error. Log {
Daily
Dateext
Extension. Log
Create
Rotate 60
Notifempty
Sharedscripts
Postrotate
[! -F/usr/local/nginx/sbin/nginx. pid] | kill-usr1 'cat/usr/local/nginx/sbin/nginx. PID'
Endscript
}
The field meanings are as follows:
The daily is separated once a day.
Dateext indicates that the log file is represented in the log format, so that the log name is changed to access-20140626.
Extension. log indicates that the. log extension is added after the log name to the well-known programming access-20140626.log.
"Create" indicates that access. log is created after a new log file is created.
Rotate 60 indicates backing up so many copies
Receivempty indicates that log files are not separated if they are empty.
Sharedscripts indicates that logrotate is executed only once between multiple log files.
Postrotate
[! -F/usr/local/nginx/sbin/nginx. pid] | kill-usr1 'cat/usr/local/nginx/sbin/nginx. PID'
Endscript
Indicates the command that is executed after logrotate is executed
There are many other parameters, which can be viewed by man logrotate.
After configuring logrotate, I thought that I had to set the crontab file to regularly execute logrotate, so:
Crontab-e
59 23 ***/usr/sbin/logrotate-F/etc/logrotate. d/nginx
The result is that the logrotate is automatically executed at and, when the time is, you can also modify the configuration file to modify the time, so you do not need to set the crontab file.
There is a time difference between copying and clearing, so some log data may be lost.