>_ IzzyLab

Hands-on task

This task is step 8 of the course “Running a server”

Set up log rotation

medium~7 min

The application log /var/log/app/app.log grows without bound. Create a logrotate config /etc/logrotate.d/app: rotate when the size is larger than 1M, keep 3 old copies, compress them, and create a fresh empty log after rotation.

What you'll practise:
  • logrotate
  • log rotation
  • compress
  • configuration

Topic breakdown

logrotate keeps logs from growing forever: on a threshold (size or schedule) it renames the current file to .1, .2, and so on, compresses the old ones, and discards the oldest. Its behavior is set by a config in /etc/logrotate.d/: the log path, curly braces, and directives inside — the rotation threshold, the number of copies to keep, compression, and creating a fresh empty file after rotation (otherwise the app keeps writing to the already-renamed descriptor). It's worth looking up the exact directive names in man logrotate or the neighboring configs.

What's checked

  1. config /etc/logrotate.d/app created
  2. the config keeps 3 copies and compresses
  3. rotation kicks in at a size above 1M
  4. old copies are compressed to .gz
  5. a forced rotation runs without errors
  6. a fresh app.log is created after rotation

What it looks like