Logrotate log partition of Linux Log Management artifact

preface:

Some services will automatically generate a large number of log files, which will occupy disk space if not limited.
If you simply use the scheduled task crontab to delete, it is not flexible. At this time, you need the log artifact logrotate.

logrotate tool is a tool built in the system to facilitate log management.
The system will run logrotate regularly, usually once a day. It is also run based on the scheduled task crontab.

Profile:

The main configuration file is located in / etc/logrotate.conf, and is generally configured in the subdirectory of / etc/logrotate.d /.
For example, the default log configuration:

$ vim /etc/logrotate.d/log_file 

/var/log/log_file {

    monthly
    rotate 5
    compress
    delaycompress
    missingok
    notifempty
    create 644 root root
    postrotate
        /usr/bin/killall -HUP rsyslogd
    endscript
}

Profile parameters:

  • Daily: Specifies that the dump cycle is daily
  • Weekly: Specifies that the dump cycle is weekly
  • Monthly: Specifies that the dump cycle is monthly
  • rotate count: Specifies the number of dumps before deleting the log file. 0 means no backup and 5 means 5 backups are reserved
  • tabooext [+] list: let logrotate not dump files with the specified extension. The default extensions are:. RPM Orig,. Rpmsave, V, and ~
  • missingok: during log rotation, any errors will be ignored, such as "file cannot be found".
  • Size: the log file is dumped when it reaches the specified size, bytes (default) and KB (sizek) or MB (sizem)
  • Compress: compress the dumped logs through gzip
  • nocompress: no compression
  • Copyruncat: used to back up and truncate the current log files that are still open
  • Nocopytruncat: backs up log files but does not truncate them
  • create mode owner group: dumps files and creates new log files using the specified file mode
  • nocreate: do not create a new log file
  • delaycompress: when used with compress, the dumped log file will not be compressed until the next dump
  • nodelaycompress: override the delaycompress option and compress the dump at the same time.
  • errors address: send the error information during private storage to the specified Email address
  • ifempty: dump even empty files. This is the default option of logrotate.
  • notifempty: if it is an empty file, it will not be dumped
  • Mail address: send the dumped log file to the specified E-mail address
  • nomail: do not send log files during dump
  • olddir directory: the stored log files are placed in the specified directory and must be in the same file system as the current log file
  • noolddir: the dumped log file is placed in the same directory as the current log file
  • prerotate/endscript: the commands to be executed before dumping can be put into this pair, and the two keywords must be on separate lines

For more information, refer to the man logrotate help documentation

The template is general, and the configuration parameters are adjusted according to your needs. Not all parameters are necessary.

/var/log/log_file {
    size=50M
    rotate 5
    dateext
    create 644 root root
    postrotate
        /usr/bin/killall -HUP rsyslogd
    endscript
}

In the above configuration file, we only want to poll one log file. size=50M specifies that the log file size can be increased to 50MB. Dateext indicates that the old log file is named after the creation date.

You can refer to the system default file in the / etc/logrotate.d / directory.

logrotate command:

The specific logrotate command format is as follows:

logrotate [OPTION...] <configfile>
-d, --debug : debug Mode to 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 status file.
-v, --verbose : Displays the dump process.

To call logrotate for a specific configuration:

 logrotate /etc/logrotate.d/log_file

The best choice during troubleshooting is to run logrotate in rehearsal mode with the - d option. To verify, you can simulate drill log rotation and display its output without actually rotating any log files.

[root@localhost ~]$ logrotate -d /etc/logrotate.d/log_file

WARNING: logrotate in debug mode does nothing except printing debug messages!  Consider using verbose mode (-v) instead if this is not what you want.

reading config file /etc/logrotate.d/dnmplog
Reading state from file: /var/lib/logrotate/status
Allocating hash table for state file, size 64 entries
Creating new state
Creating new state
Creating new state
Creating new state
Creating new state
Creating new state
Creating new state
Creating new state
Creating new state
Creating new state
Creating new state
Creating new state
Creating new state
Creating new state
Creating new state
Creating new state
Creating new state
Creating new state
Creating new state
Creating new state
Creating new state
Creating new state
Creating new state
Creating new state
Creating new state
Creating new state
Creating new state
Creating new state

Handling 1 logs

rotating pattern: /home/www/localhost/storage/logs  104857600 bytes (6 rotations)
empty log files are not rotated, old logs are removed
No logs found. Rotation not needed.

As we can see from the above output, logrotate judges that the cycle is unnecessary. This happens if the file is less than one day old.

Force round robin even if the round robin conditions are not met, we can force the logrotate round robin log file by using the - f option, and the - v parameter provides detailed output.

logrotate -vf /etc/logrotate.d/log_file 

Keywords: Linux Operation & Maintenance server

Added by ljzxtww on Fri, 05 Nov 2021 02:48:13 +0200