>_ IzzyLab

Hands-on task

This task is step 16 of the course “New server: setup from scratch”

Rotating the orders log

medium~7 min

The shop writes orders to /var/log/shop/orders.log, and after a week of testing the log has already grown. Create a logrotate config /etc/logrotate.d/shop: rotate when the file is bigger than 2M, keep 5 old copies, compress them, and create a new empty log after rotation.

What you'll practise:
  • logrotate
  • size
  • rotate
  • compress
  • create

Topic breakdown

logrotate keeps logs within bounds: when a threshold fires (by size or by schedule), it renames the current file to .1, shifts the old copies, compresses them and deletes the oldest ones. The behaviour for a particular log is described in a file of its own in /etc/logrotate.d/: the path, curly braces and directives — the threshold, the number of copies, compression and creating a fresh file (otherwise the app has nowhere to write). The debug mode -d checks a config without changing anything, and -f forces a rotation.

What's checked

  1. the config /etc/logrotate.d/shop exists
  2. the config keeps 5 copies and compresses them
  3. rotation fires only when the file is bigger than 2M
  4. rotation creates a compressed orders.log.1.gz
  5. there's a fresh orders.log after rotation

What it looks like