Linux log rolling cut with logratate โ
Introduction to Log Rollup โ
Logging is a very important part of online services. Various services constantly record their own operational logs as they run, such as nginx access logs, business system flow logs, various error logs, and so on. These logs are usually stored in different log files, and the size of the log files grows as the runtime increases.
But the disk space of online server is limited, and if the size of log files keeps growing, it will eventually lead to insufficient disk space. To solve this problem, we need to perform rolling cuts on the logs.
Specifically, rolling cut will do several things.
- set certain scrolling rules (e.g. by time or volume)
- change the current log to history log when the rule is satisfied, and generate a new log file as the current log file
- automatically clean up some old log files when there are too many history log files
This way the original large log file will become a bunch of small log files, and will be cut and rolled every once in a while, and the total log history is basically stable and unchanged, so you don't have to worry about the logs will keep growing and taking up disk space.
logrotate usage โ
Most Linux distributions have a built-in logrotate tool, which makes it easy to set logrotate rules and automatically clean up outdated log files.
The configuration file for logrotate is
/etc/logrotate.confmain configuration file- The
/etc/logrotate.ddirectory can hold many specific logrotate configuration files
When we need to set up a log-rolling cut rule, we can create a new configuration file under /etc/logrotate.d. For example /etc/logrotate.d/nginx, the contents of this file are as follows
/var/log/nginx/*.log {
daily
missingok
rotate 7
compress
delaycompress
notifempty
create 640 root root
sharedscripts
postrotate
[ -f /var/run/nginx.pid ] && kill -USR1 \`cat /var/run/nginx.pid`
endscript
}The meaning of this configuration file is.
dailyrolls over once a daymissingokdoes not roll if the file does not existrotate 7keep the last 7 log filescompressCompress log filesdelaycompressDelay compressionnotifemptyDo not roll if file is emptycreate 640 root rootThe owner and permissions of the new log file, especially if nginx is not run by therootusersharedscriptsshare scripts, i.e. run scripts after the logs have finished scrolling, otherwise you will have to run scripts once for each log file scrolledpostrotatescript that runs after the logs have finished rolling, some business logs may not need this script
Once the log rolling cut rule configuration is set, you can use logrotate -d to verify the rule, for example
logrotate -d /etc/logrotate.d/nginxreturns something like the following.
reading config file /etc/logrotate.d/nginx
Allocating hash table for state file, size: 15360 B
Handling 1 logs
rotating pattern: /var/log/nginx/*.log after 1 days (7 rotations)
empty log files are not rotated, old logs are removed
considering log /var/log/nginx/*.log /access.log
log does not need rotating (log has been already rotated)
considering log /var/log/nginx/*.log /error.log
log does not need rotating (log has been already rotated)
running postrotate script
......No errors means the configuration file is correct.
If you want to see results immediately, you can use logrotate -f to force a scroll cut, e.g.
logrotate -f /etc/logrotate.d/nginxOther parameters โ
compresscompress history logs after rollingnocompressdo not compress the history log after rollingcopytruncateis used to backup and truncate the current log file while it is still open; it is a way to copy and then empty, there is a time gap between copying and emptying, and some log data may be lost.nocopytruncatebacks up the log file but does not truncate itcreate mode owner groupSpecify the owner and permissions for creating new filesnocreatedo not create new log filesdelaycompressandcompresstogether compress the history log file until the next rollovernodelaycompressoverrides thedelaycompressoption and compresses on a rolling basismissingokIf a log is missing, continue to scroll to the next log without reporting an errorerrors addressSend error messages to the specified Email address when scrollingifemptyScroll even if the log file is emptynotifemptyDo not scroll when the log file is emptymail addressSend the scrolling log file to the specified Email addressnomailDo not send log files when scrollingolddir directoryPut the scrolled log file into the specified directory, it must be on the same file system as the current log filenoolddirThe scrolled log file is placed in the same directory as the current log filesharedscriptsshare scripts, i.e. run the scripts after the logs are scrolled, otherwise run the scripts once for each log file scrolledprerotatethe command to be executed before scrolling, such as modifying the file's properties; must be a separate linepostrotateA command to be executed after the rollover, such as restarting (kill -HUP) a service; must be a separate linedailyspecifies that the rolling period is dailyweeklyspecifies that the rolling period is weeklymonthlyspecifies a monthly rolling cyclerotate countspecifies the number of times the log file is rolled over before it is deleted,0means no backups are kept,5means 5 backups are keptdateextuses the current date as the naming formatdateformat . %sused with dateext, appears immediately after the next line, defines the name of the file after it is cut, must be used withdateext, only supports%Y/%m/%d/%sfour parameterssize log-size(orminsize log-size) Scrolls the log file when it reaches the specified size, the following is the legal format.size = 5orsize 5(scrolls when >= 5 bytes)size = 100korsize 100ksize = 100Morsize 100M
