log-rotate

Description

The log-rotate Plugin is used to keep rotating access and error log files in the log directory at regular intervals.

You can configure how often the logs are rotated and how many logs to keep. When the number of logs exceeds, older logs are automatically deleted.

Attributes

NameTypeRequiredDefaultDescription
intervalintegerTrue60 60Time in seconds specifying how often to rotate the logs.
max_keptintegerTrue24 7Maximum number of historical logs to keep. If this number is exceeded, older logs are deleted.
max_sizeintegerFalse-1Max size(Bytes) of log files to be rotated, size check would be skipped with a value less than 0 or time is up specified by interval.
enable_compressionbooleanFalsefalseWhen set to true, compresses the log file (gzip). Requires tar to be installed.

Enabling the Plugin

To enable the Plugin, add it in your configuration file (conf/config.yaml):

conf/config.yaml

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

Example usage

Once you enable the Plugin as shown above, the logs will be stored and rotated based on your configuration.

In the example below the interval is set to 10 and max_kept is set to 10. This will create logs as shown:

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

If you have enabled compression, the logs will be as shown below:

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

Disable plugin

To remove the log-rotate Plugin, you can remove it from your configuration file (conf/config.yaml):

conf/config.yaml

  1. plugins:
  2. # - log-rotate