log-rotate

Description

The plug-in can automatically rotate access and error log files in the log directory at regular intervals.

Specify how often logs are rotated every interval and how many logs have been kept recently. When the number of log files exceeds the remaining number, the old files are automatically deleted.

Attributes

NameTypeRequirementDefaultValidDescription
intervalintegerrequired60 60How often to rotate the log in seconds
max_keptintegerrequired24 7How many historical logs can be kept at most. When this number is exceeded, old files will be deleted automatically.
enable_compressionbooleanoptionalfalseWhether to enable log file compression(gzip). This feature requires tar installed.

After this plug-in is enabled, the log file will be automatically rotated according to the configuration. For example, the following example is a sample based on interval: 10 and max_kept: 10.

  1. $ ll logs
  2. total 44K
  3. -rw-r--r--. 1 resty resty 0 Mar 20 20:32 2020-03-20_20-32-40_access.log
  4. -rw-r--r--. 1 resty resty 2.4K Mar 20 20:32 2020-03-20_20-32-40_error.log
  5. -rw-r--r--. 1 resty resty 0 Mar 20 20:32 2020-03-20_20-32-50_access.log
  6. -rw-r--r--. 1 resty resty 2.8K Mar 20 20:32 2020-03-20_20-32-50_error.log
  7. -rw-r--r--. 1 resty resty 0 Mar 20 20:32 2020-03-20_20-33-00_access.log
  8. -rw-r--r--. 1 resty resty 2.4K Mar 20 20:33 2020-03-20_20-33-00_error.log
  9. -rw-r--r--. 1 resty resty 0 Mar 20 20:33 2020-03-20_20-33-10_access.log
  10. -rw-r--r--. 1 resty resty 2.4K Mar 20 20:33 2020-03-20_20-33-10_error.log
  11. -rw-r--r--. 1 resty resty 0 Mar 20 20:33 2020-03-20_20-33-20_access.log
  12. -rw-r--r--. 1 resty resty 2.4K Mar 20 20:33 2020-03-20_20-33-20_error.log
  13. -rw-r--r--. 1 resty resty 0 Mar 20 20:33 2020-03-20_20-33-30_access.log
  14. -rw-r--r--. 1 resty resty 2.4K Mar 20 20:33 2020-03-20_20-33-30_error.log
  15. -rw-r--r--. 1 resty resty 0 Mar 20 20:33 2020-03-20_20-33-40_access.log
  16. -rw-r--r--. 1 resty resty 2.8K Mar 20 20:33 2020-03-20_20-33-40_error.log
  17. -rw-r--r--. 1 resty resty 0 Mar 20 20:33 2020-03-20_20-33-50_access.log
  18. -rw-r--r--. 1 resty resty 2.4K Mar 20 20:33 2020-03-20_20-33-50_error.log
  19. -rw-r--r--. 1 resty resty 0 Mar 20 20:33 2020-03-20_20-34-00_access.log
  20. -rw-r--r--. 1 resty resty 2.4K Mar 20 20:34 2020-03-20_20-34-00_error.log
  21. -rw-r--r--. 1 resty resty 0 Mar 20 20:34 2020-03-20_20-34-10_access.log
  22. -rw-r--r--. 1 resty resty 2.4K Mar 20 20:34 2020-03-20_20-34-10_error.log
  23. -rw-r--r--. 1 resty resty 0 Mar 20 20:34 access.log
  24. -rw-r--r--. 1 resty resty 1.5K Mar 20 21:31 error.log

When enable log file compression, log file will be like below.

  1. $ ll logs
  2. total 10.5K
  3. -rw-r--r--. 1 resty resty 1.5K Mar 20 20:33 2020-03-20_20-33-50_access.log.tar.gz
  4. -rw-r--r--. 1 resty resty 1.5K Mar 20 20:33 2020-03-20_20-33-50_error.log.tar.gz
  5. -rw-r--r--. 1 resty resty 1.5K Mar 20 20:33 2020-03-20_20-34-00_access.log.tar.gz
  6. -rw-r--r--. 1 resty resty 1.5K Mar 20 20:34 2020-03-20_20-34-00_error.log.tar.gz
  7. -rw-r--r--. 1 resty resty 1.5K Mar 20 20:34 2020-03-20_20-34-10_access.log.tar.gz
  8. -rw-r--r--. 1 resty resty 1.5K Mar 20 20:34 2020-03-20_20-34-10_error.log.tar.gz
  9. -rw-r--r--. 1 resty resty 0 Mar 20 20:34 access.log
  10. -rw-r--r--. 1 resty resty 1.5K Mar 20 21:31 error.log

Example

Enable plugin

Enable the plug-in log-rotate in conf/config.yaml, then this plugin can work fine. It does not need to be bound in any route or service.

Here is an example of conf/config.yaml:

  1. plugins:
  2. # the plugins you enabled
  3. - log-rotate
  4. plugin_attr:
  5. log-rotate:
  6. interval: 3600 # rotate interval (unit: second)
  7. max_kept: 168 # max number of log files will be kept
  8. enable_compression: false # enable log file compression(gzip) or not, default false

Disable plugin

Remove the plugin log-rotate from conf/config.yaml.