Article directory
- 1. Introduction to Logrotate
-
- 1. Introduction to Logrotate configuration file
- The specific logrotate command format is as follows
- Logrotater log file cutting strategy
- Logrotate can trigger log rotation automatically or manually
- 2. Example: tomcat log
-
- 1. Sample file location
- 2. Create a file named tomcat-9.0.43 in the /etc/logrotate.d directory
- 3. Add the following content in the file tomcat-9.0.43
- 4. Trigger log rotation
- 5. After running the command, the compressed files are as follows
- 6. Verification result
This article briefly introduces the use and examples of logrotate.
This article is divided into two parts, namely the introduction of logrotate and the introduction of tomcat logs as an example.
1. Introduction to Logrotate
The Logrotate program is a log file management tool. Used to split log files, compress dumps, delete old log files, and create new log files
logrotate generally comes with the system and does not need to be installed by yourself
logrotate handles logs, relying on cron to execute automatically at execution time every day
1. Logrotate configuration file introduction
The Linux system installs logrotate by default, and the default configuration file:
#/etc/logrotate.conf #/etc/logrotate.d/ #logrotate.conf: main configuration file #logrotate.d: To configure related subsystems, used to isolate each application configuration (Nginx, PHP, Tomcat...)
Logrotate is based on CRON to run, its script is /etc/cron.daily/logrotate, log rotation is automatically completed by the system.
When actually running, Logrotate will call the configuration file /etc/logrotate.conf.
The specific logrotate command format is as follows
logrotate [OPTION...] <configfile> -d, --debug : debug mode, test the configuration file for errors. -f, --force : Force dump file. -m, --mail=command : After compressing the log, send the log to the specified mailbox. -s, --state=statefile : use the specified statefile. -v, --verbose : Show dump process.
Logrotater log file cutting strategy
cat /etc/logrotate.conf weekly //By default, the rotate job is executed once a week rotate 4 //How many log files to keep (how many times to rotate). Four are reserved by default. It is to specify the number of times to rotate before the log file is deleted, and 0 means no backup create //Automatically create a new log file, the new log file has the same permissions as the original file; because the log is renamed, a new one must be created to continue storing the previous log dateext //This parameter is very important! That is, the log file after cutting ends in the format of the current date, such as xxx.log-20131216. If it is commented out, the cutout will be incremented by number, that is, the format of xxx.log-1 mentioned above compress //Whether to compress the dumped log file through gzip, such as xxx.log-20131216.gz; if no compression is required, just comment it out include /etc/logrotate.d //Import each application configuration in the /etc/logrotate.d/ directory /var/log/wtmp {<!-- --> //Only for the parameters set for /var/log/wtmp monthly // cut once a month, instead of the default week minsize 1M //The file will be cut only after the file size exceeds 1M create 0664 root utmp //Specify the permission of the newly created log file and the user and group it belongs to rotate 1 //Only keep one log. } #This wtmp can record the time when the user logs in to the system and the system restarts #Because there is a minsize parameter, it may not necessarily be executed once a month. It depends on the file size. Other configurable parameters in Logrotate are as follows: compress //Compress the dumped log with gzip nocompress //do not do gzip compression processing copytruncate //Used for log files that are still open, back up and truncate the current log; it is a method of copying and then clearing. There is a time difference between copying and clearing, and some log data may be lost. nocopytruncate //Backup log files but do not truncate create mode owner group //Specify the attributes of creating a new file during rotation, such as create 0777 nobody nobody nocreate //Do not create a new log file delaycompress //When used together with compress, the dumped log file will not be compressed until the next dump nodelaycompress //Override the delaycompress option, dumping and compressing at the same time. missingok //If the log is missing, continue to scroll to the next log without reporting an error errors address //The error information during special storage is sent to the specified email address ifempty //Rotate even if the log file is empty, this is the default option of logrotate. notifempty //When the log file is empty, no rotation is performed mail address //Send the dumped log file to the specified E-mail address nomail //do not send log files when dumping olddir directory // put the dumped log file into the specified directory, it must be in the same file system as the current log file noolddir //The dumped log file and the current log file are placed in the same directory sharedscripts //Run the postrotate script, which is used to execute the script uniformly after all logs are rotated. If this is not configured, the script will be executed after each log rotation prerotate //Instructions that need to be executed before logrotate dumps, such as actions such as modifying file attributes; must be independent postrotate //Instructions that need to be executed after the logrotate dump, such as restarting (kill -HUP) a service! must travel independently daily //Specify the dump cycle as daily weekly //Specify the dump cycle as weekly monthly //Specify the dump cycle as monthly rotate count //Specify the number of dumps before the log file is deleted, 0 means no backup, 5 means keep 5 backups dateext //Use the current date as the naming format dateformat .%s //Use with dateext, followed by the next line, define the file name after cutting the file, must be used with dateext, only support the four parameters %Y %m %d %s size (or minsize) log-size //When the log file reaches the specified size, it will be dumped. The log-size can specify bytes (default) and KB (sizek) or MB (sizem). Dump when log file >= log-size. The following is a legal format: (I have not tried unit case in other formats) size = 5 or size 5 (dump if >= 5 bytes) size = 100k or size 100k size = 100M or size 100M
Logrotate can trigger log rotation automatically or manually
#Specify the triggered file cutting logrotate -f /etc/logrotate.d/nginx logrotate -f /etc/logrotate.d/php #All trigger file cutting logrotate /etc/logrotate.conf
2. Example: tomcat log
Cut specific files, this example is to cut catalina.out file
1. Sample file location
Log file location: /opt/apache-tomcat-9.0.43/logs/catalina.out
2. Create a file named tomcat-9.0.43 in the /etc/logrotate.d directory
vi /etc/logrotate.d/tomcat-9.0.43
3. Add the following content in the file tomcat-9.0.43
/opt/apache-tomcat-9.0.43/logs/catalina.out{<!-- --> copytruncate weekly rotate 10 missing ok compress size 100M noolddir dateext dateformat.%d-%s }
4. Trigger log rotation
logrotate --force /etc/logrotate.d/tomcat-9.0.43 #or logrotate --f /etc/logrotate.d/tomcat-9.0.43
5. After running the command, the compressed file is as follows
catalina.out.22-1658466965.gz
6. Verification result
After running for a long time, you can see the result as follows:
Above, a brief introduction to the use and examples of logrotate.